[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
1,065 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

649 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