Page 1 of 1

running Bash file

Posted: Wed Oct 27, 2010 5:43 am
by IGGt
I'm sure that there is a really obvious answer, but as a Linux newbie, it's getting very frustrating. (I come from a windows background, and am therefore more used to batch files)

Essentially I have a process to run, that needs to be run multiple times, against multiple files. I have written a bash script to try and achieve this. It looks like:

[text]
#!/bin/bash
pxsqldump -d mysql -f /mnt/abc_xp/DB/AttachGrp.DB > /mnt/abc_xp/sql3/AttachGrp.sql;
pxsqldump -d mysql -f /mnt/abc_xp/DB/Attachment.DB > /mnt/abc_xp/sql3/Attachment.sql;
pxsqldump -d mysql -f /mnt/abc_xp/DB/AttachSrch.DB > /mnt/abc_xp/sql3/AttachSrch.sql;
. . .
[/text]

Basically it converts the .DB file to a .sql file. I have a copy of Ubuntu running in Virtualbox on my Windows XP machine. I use a shared folder so that the .DB files are on my windows machine, but are showing in Linux at /mnt/abc_xp/DB/

When I double click the file (bash.sh) in Linux it asks if I want to run it in the terminal, or display it etc.

I click run in terminal.

The terminal window opens and closes, but nothing happens.

If I paste the lines individually into a terminal window, they work fine.

I'm sure there must be a simple solution, but so far, I just can't find it.

Re: running Bash file

Posted: Wed Oct 27, 2010 9:34 am
by Doug G
Did you give your script execute permissions?

Re: running Bash file

Posted: Wed Oct 27, 2010 11:17 am
by IGGt
I did yes.

sudo chmod +x bash.sh

Re: running Bash file

Posted: Wed Oct 27, 2010 5:31 pm
by VladSun
0. what happens when you run this script from the terminal (manually - i.e. ./bash.sh)?
1. what's the user that run this script?
2. is PATH set right for that user/script? ( or use absolute paths to solve this)

Re: running Bash file

Posted: Thu Oct 28, 2010 2:42 am
by IGGt
1. If I navigate to that folder e.g.
[text]
$ cd /mnt/abc_xp
[/text]

then try to execute the file
[text]
$ bash.sh
[/text]

I get "command not found"


2. As far as I know there is only one user, which is the user that was set up when I installed Linux

3. I've checked the paths, and the files are all where I say they should be, and I have looked in /bin and there is a file called bash

Re: running Bash file

Posted: Thu Oct 28, 2010 2:56 am
by VladSun
It should be either:

Code: Select all

/path/to/bash.sh
or

Code: Select all

cd /mnt/abc_xp
./bash.sh
because /mnt/abc_xp is not in the PATH.

Code: Select all

whoami
will return the current user.

Re: running Bash file

Posted: Thu Oct 28, 2010 8:37 am
by IGGt
If I type that

Code: Select all

cd /mnt/abc_xp
and press enter, and then I type

Code: Select all

./bash.sh
and press enter, I get
[text]
bash:/ .bash.sh: /bin/bash^M: bad interpreter: No Such file or directory
[/text]


the result of

Code: Select all

whoami
is "ian"

Re: running Bash file

Posted: Thu Oct 28, 2010 8:49 am
by VladSun

Code: Select all

which bash

Re: running Bash file

Posted: Thu Oct 28, 2010 8:55 am
by IGGt
that gives me /bin/bash

Re: running Bash file

Posted: Thu Oct 28, 2010 9:09 am
by VladSun
Rewrite the first line (the so called shebang line) - it contains a ^M special char at its end. Use Linux text editor to do this (I suppose it was added by a Windows editor).

Re: running Bash file

Posted: Thu Oct 28, 2010 9:46 am
by IGGt
Ah ha. that has done it. I now have a load of data scrolling across the screen (not what I wanted, but that's a problem with the files I'm working on that I knew about anyway).

Here's me thinking that a plain text file would be a plain text file, wherever, apparently not.


cheers,

Re: running Bash file

Posted: Thu Oct 28, 2010 3:49 pm
by Doug G
I wouldn't name a script file "bash".anything

Re: running Bash file

Posted: Thu Oct 28, 2010 5:07 pm
by VladSun
Doug G wrote:I wouldn't name a script file "bash".anything
I would have said the same, if it had been named "bash", but not "bash.sh"

Re: running Bash file

Posted: Thu Oct 28, 2010 8:33 pm
by Weirdan
as a side note, probably you could use a shorter script:

Code: Select all

cd /mnt/abc_xp/ && for file in DB/*.DB; do pxsqldump -d mysql -f "${file}" > "sql3/$(basename "${file%DB}sql")"; done

Re: running Bash file

Posted: Fri Oct 29, 2010 2:43 am
by IGGt
cheers, that was my job for today, so you've just saved me a few hours work.