LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Very simple BASH scripting question (https://www.linuxquestions.org/questions/linux-newbie-8/very-simple-bash-scripting-question-385911/)

clickster 11-23-2005 02:54 PM

Very simple BASH scripting question
 
I have two commands, command1 and command2. In a script, I want to run command1, then once command1 is complete, I want to run command2. I can't remember what you put between commands to get the 2nd one to wait on the 1st to complete. I have tried the two that I thought it might be, "wait" and "&", with no luck

asb 11-23-2005 03:00 PM

try &&

Tinkster 11-23-2005 03:07 PM

If you only want two to run if one was successful, do
what asb said; if you want it to run regardless of the
outcome of command1, use a semicolon.

command1; command2
as opposed to
command1 && command2


Cheers,
Tink

clickster 11-23-2005 03:13 PM

Thanks. That worked. I knew it was something along those lines. I've been out of scripting for way too long.

alienDog 11-23-2005 03:31 PM

Third way exists too, || can also be used in between the commands.

command1 || command2

in this case command2 gets executed if command1 fails.

clickster 11-23-2005 04:12 PM

I actually needed &&, but I had never heard of || or ;. Those are good to know. I;m sure I'll run into situations where I need them. Thanks everyone.

alienDog 11-23-2005 04:28 PM

It's really just basic boolen logic:

&& = and

Thus command1 && command2 means that is command1 gets executed without failing (i.e. is "true"), then command2 must get executed too.

|| = or

Thus either command1 OR command2 must be executed. Since command1 is tried first, command2 will get executed only if command1 fails (i.e. is "false").

It's the same thing as in bash if.


All times are GMT -5. The time now is 11:04 AM.