Solved

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

Posted on 2011-02-21
5
1,053 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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