Solved

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

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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 dynamically set the form action using jQuery.

911 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

22 Experts available now in Live!

Get 1:1 Help Now