execute multipe commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting execute multipe commands
# 1  
Old 10-17-2011
execute multipe commands

I would like to execute multipe commands in a shell script. Please provide the commands for the below scenario:

Execute command1
if command1 is succesfull, then execute command2, command3,command4 in parallel
if command2, command3,command4 are success then run command 5
This User Gave Thanks to p_gautham12 For This Post:
# 2  
Old 10-17-2011
with echo $? , you can get the exit status, if above command is successful, it should be 0.
Code:
command1
if [ $? -eq 0 ] then
  command2;commend3;command4
  another if-then-fi
fi

You can also add the exit status directly in script to control your condition.

Last edited by rdcwayx; 10-17-2011 at 09:44 PM..
# 3  
Old 10-17-2011
Not as clean as rdcwayx but just to show various options and can be handy at the prompt.
You can use logical operator && after each command.
The shell executes the second command -- if and only if -- the first command returns a true (zero) exit status.

Example:
Code:
 command1 && command2 && command3 && command4 && command5

The above states if command1 is successful execute command2 if command2 successful execute command3 and so on.

Regards,
SRG
# 4  
Old 10-18-2011
OP wanted to run jobs in parallel.


I created a dummyjob script like this:
Code:
sleep $1
exit $2

And the used array to store PIDs of the child processes and then waited for each of them and stored first non-zero exit status:

Code:
if ./dummyjob 4 0
then
    ./dummyjob 10 0 &
    j[0]=$!
    ./dummyjob 4 7 &
    j[1]=$!
    ./dummyjob 2 0 &
    j[2]=$!
    FAIL=0
    for PID in ${j[*]}
    do
       wait $PID || FAIL=$?
    done
    echo "Result $FAIL"
else
   echo "First Job failed"
fi

Output appeared 14 seconds after job started:
Code:
Result 7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute telnet commands thru xterm?

I want to launch an xterm telnet window: xterm -e telnet xxx.xxx.xx.xxx Which I can do but needs to also launch commands as well. Please use CODE tags as required by forum rules! (2 Replies)
Discussion started by: shopgirl08
2 Replies

2. Shell Programming and Scripting

Command to execute commands one after other

I am writng a script in which there is an installation file.The installer runs good. But after the installer command there are some files and commands which are based on those installed files. Now problem is the commands get executed before the installer is totally installed. So is there any... (5 Replies)
Discussion started by: sriki32
5 Replies

3. Shell Programming and Scripting

Execute 2 Commands at the same time

Hi @all I have got the following problem: I want my Master-Script to execute 2 Sub-scripts at the same time. How can i realize that? Thx for your help Greez Roger (2 Replies)
Discussion started by: DarkSwiss
2 Replies

4. Shell Programming and Scripting

ssh - to execute set of commands

Hi Can someone help me to figure out Want to execute few cmds in remote host thru ssh Tried below cmd -------------------------------excerpt------------------- RDIR=/data1/logs ---> variable stores rem. server directory TODAY="`date '+%b %d'`" ssh -i $userid@$host "cd... (2 Replies)
Discussion started by: id100
2 Replies

5. Shell Programming and Scripting

execute shell commands with in sftp

Hi All, Please let me know how do I execute some of the shell commands like cat, find ,grep within sftp. Any help in this regard would be greatly appreciated. Thanks, (5 Replies)
Discussion started by: tommy1
5 Replies

6. Shell Programming and Scripting

Execute multiple commands in a find

I am checking that a file is older than a reference file that I build with a touch command before processing it. If it is not old enough, I want to sleep for an hour and check again. My problem is if it is old enough to process, I want to exit when I am done, but I cannot find a way to exit... (2 Replies)
Discussion started by: prismtx
2 Replies

7. UNIX for Advanced & Expert Users

Execute All Commands in A contrl File

Hi , I have a situation i need to write a while loop until the end of control file.In the control file i have a 5 lines which contains commands.how can i execute all with out waiting for the first one to complete. Ex ControlFile: ScripitName Test ScriptName Test1 ScriptName Test2 ... (1 Reply)
Discussion started by: ukatru
1 Replies

8. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

9. Shell Programming and Scripting

how do i get my script to execute multiple commands?

New to shell scripting. I can't get my script to execute multiple commands. Here's the code. It's a menu script. #!/bin/ksh clear print "SDE MENU" PS3="SDE MENU, enter choice:" select clean_menu in "tasdedev instance 5151" "orkindev instance 5155" "tasdetst instance 5157" "orkinsys... (1 Reply)
Discussion started by: hvincent
1 Replies

10. Shell Programming and Scripting

Execute commands parallel in a for loop ?

Hi, please can someone point me in the right direction with a shell scripting problem. I want to execute a command in a for loop and the command should be started not one-by-one meaning the for loop is waiting for the exit code , it should be started in parallel. I have a plain text file... (3 Replies)
Discussion started by: networkfre@k
3 Replies
Login or Register to Ask a Question