?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
1,063 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

741 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