Solved

bash Scripting: How to wait or command to complete before starting next?

Posted on 2011-02-21
5
1,049 Views
Last Modified: 2012-05-11
I need help making a bash script wait for consecutive commands to complete.  Here's my code:

#!/bin/bash

#---Declare the VM...
myVM="SYCAMORE"

#---Stop myVM Gracefully...
VBoxManage controlvm $myVM acpipowerbutton
####wait####

#---Copy the myVM Directory to Another Location
cp --recursive --verbose "/home/user/VirtualBox VMs/"$myVM "/home/user/Backups/"$myVM 
####wait####

#---Start the myVM...
VBoxManage startvm $myVM
####wait####

#---Exit...
exit 0

Open in new window


I need specific code to replace ####wait#### with.

Any help is appreciated.

Thanks.
0
Comment
Question by:SqueezeOJ
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:gparrish
ID: 34945678
You could use an IF, THEN, ELSE loop or a while do loop and set a flag before the command runs and then delay and loop until the flag is cleared at which point you could then continue with the script.

   if ...
   then ...
   else if ...
       ...
     fi
   fi

   if ...
   then ...
   else if ...
       ...
     fi
else ....
   fi


   while [ 1 ]
   do
      ls -al <file> ; sleep 60
   done



HTH,
Greg
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 34945684
putting the commands after each other in the script will do that

command1
command2
command3
0
 
LVL 1

Expert Comment

by:gparrish
ID: 34945949
Yea that is the way it should work. I have seen commands execute in order, but sometimes something gets delayed and a later command will have ran and completed while a previous command is still running. This is what I assumed prompted the question here.
0
 

Author Closing Comment

by:SqueezeOJ
ID: 34946657
That was TOO easy!  Thanks...
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 34948331
Welcome
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now