running Bash file

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

running Bash file

Post 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.
Doug G
Forum Contributor
Posts: 282
Joined: Sun Sep 09, 2007 6:27 pm

Re: running Bash file

Post by Doug G »

Did you give your script execute permissions?
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post by IGGt »

I did yes.

sudo chmod +x bash.sh
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: running Bash file

Post 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)
There are 10 types of people in this world, those who understand binary and those who don't
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: running Bash file

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post 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"
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: running Bash file

Post by VladSun »

Code: Select all

which bash
There are 10 types of people in this world, those who understand binary and those who don't
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post by IGGt »

that gives me /bin/bash
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: running Bash file

Post 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).
There are 10 types of people in this world, those who understand binary and those who don't
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post 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,
Doug G
Forum Contributor
Posts: 282
Joined: Sun Sep 09, 2007 6:27 pm

Re: running Bash file

Post by Doug G »

I wouldn't name a script file "bash".anything
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: running Bash file

Post 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"
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: running Bash file

Post 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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: running Bash file

Post by IGGt »

cheers, that was my job for today, so you've just saved me a few hours work.
Post Reply