LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2005, 01:18 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
how to run one bash script from within another?


i've got two small bash scripts and thought it would be nice to try to
make a third to run the first two. the first, however, takes some time
to run so the second never did run. i tried including a sleep command
but that seems awkward. would be much better to actually determine
when the first had finished rather than assigning an arbitrary time
limit, especially since the time could vary a lot depending on how
much material the first script had to deal with. how do you do this?
my script follows.

thanks,
BabaG

#!/bin/bash

exec ./30fps_to_24fps
sleep 60
exec ./renumber_30fps_to_24fps

done
 
Old 04-25-2005, 01:37 PM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Re: how to run one bash script from within another?

Code:
#!/bin/bash

exec ./30fps_to_24fps && exec ./renumber_30fps_to_24fps

done
This will run the second script if the first script exits successfully.
 
Old 04-25-2005, 01:41 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
hey thanks!
 
Old 04-25-2005, 02:16 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The exec builtin command is useless in these scripts, here's a simpler version
Code:
#!/bin/sh
./30fps_to_24fps && ./renumber_30fps_to_24fps
 
Old 04-25-2005, 02:38 PM   #5
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Here's a related by somewhat off-topic question. Is there another way to tell if a program exited successfully in bash? Ie, can you check for a non-zero return value? I have a somewhat complex script that executes a multitude of commands (and checks for various file/directory structures between the commands) so I can't really just put them all in a single line seperated by &&. I've been reading the Advanced BASH scripting guide but haven't been able to find that section yet.
 
Old 04-25-2005, 02:52 PM   #6
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Code:
echo $?
will provide the exit status of the last executed command.
 
Old 04-27-2005, 12:19 AM   #7
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
can someone post an example of how to do this without having the
various scripts on the same line and linked by &&? i'd like to have a
master script containing a bunch of different scripts. would be nice
to be able to keep it flexible, placing a script command on each line
and commenting out the lines for scripts i don't want to run and
uncommenting the lines for the scripts i do want to run. is this
practical?

also, tried ahh's example above but the second script didn't run.

Last edited by babag; 04-27-2005 at 12:29 AM.
 
Old 04-27-2005, 01:25 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
If you just want the script to abort should one of the subscripts fails, here's one way:
Code:
#!/bin/bash

subscript1 || exit
subscript2 arg1 arg2 || exit
# subscript3 || exit # disabled
subscript4 || exit

...

Last edited by jlliagre; 04-27-2005 at 01:26 AM.
 
Old 04-27-2005, 10:01 AM   #9
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
Originally posted by babag
...also, tried ahh's example above but the second script didn't run.
If your second script didn't run, it is because the last command run in your first script failed.

An example:-
If the last command in your first script was
Code:
[ -f /home/me/file.txt ] && echo "file exists"
and the file /home/me/file.txt does not exist, the test will fail and return an exit code of 1. As this is the last command run in your first script the script will return an exit code of 1, thus the second script will not run.

If the file does exist, the test will pass, and the echo command will run. If the echo command is successful it will return an exit code of 0, your first script will exit with an exit code of 0, and the second script will run.

If you are not aware, a successful command returns an exit code of 0, an unsucessful one another number. Simple commands are usually just 0 or 1, but the programmer may designate other numbers to specify why the command was unsuccessful.

If you want a script to exit with a code of 0 whatever happens, simply write
Code:
exit 0
as the last line.

Using the script from jlliagre as another example, you could write
Code:
#!/bin/bash

subscript1 || exit 1
subscript2 arg1 arg2 || exit 2
# subscript3 || exit 3# disabled
subscript4 || exit 4

exit 0
Now if you run the script from a terminal, you can type
Code:
echo $?
after it is done and it will print the exit code. From this you will know if it was successful, or if not, where it failed.
 
Old 04-28-2005, 12:12 AM   #10
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
hey thanks folks! very helpful. i'll be playing with these over the next few days
for sure. being that i'm ganging together multiple scripts performing many
operations, i may be gone for a while to try to figure this out. thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
run-parts gives error on bash script ChoKamir Programming 9 04-11-2017 07:18 AM
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 03:28 PM
bash: run a script at most 3 times simultanously paterijk Programming 4 08-18-2005 02:49 PM
auto run bash script when logged in linuxmandrake Fedora 1 06-11-2005 08:22 PM
Run script at login instead of bash uzi4u Linux - General 2 04-28-2004 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration