1

I have a code in shell script as follows:

    # Setup the command.
command=`ec2-describe-snapshots | grep pending | wc -l`

# Check if we have any pending snapshots at all.
if [ $command == "0" ]
then
        echo "No snapshots are pending."
        ec2-describe-snapshots
else
        # Wait for the snapshot to finish.
        while [ $command != "0" ]
        do
                # Communicate that we're waiting.
                echo "There are $command snapshots waiting for completion."
                sleep 5

                # Re run the command.
                command=`ec2-describe-snapshots | grep pending | wc -l`
        done

        # Snapshot has finished.
     echo -e "\n"
        echo "Snapshots are finished."
fi 

This code sometimes work fine, sometimes it dont works fine. It goes to an infinite loop. I want to do something like this i want to check the output of ec2-describe-snapshot that if snaphost are in pending state. if yes it should wait until all the snapshots are completed.

The output of ec2-describe-snapshots is

SNAPSHOT    snap-104ef62e   vol-a8  completed   2013-12-12T05:38:28+0000    100%    109030037527    20  2013-12-12: Daily Backup for i-3ed09 (VolID:vol-aecbbcf8 InstID:i-3e2bfd09)
SNAPSHOT    snap-1c4ef622   vol-f0  pending 2013-12-12T05:38:27+0000    100%    109030037527    10  2013-12-12: Daily Backup for i-260 (VolID:vol-f66a0 InstID:i-2601)
user3086014
  • 4,241
  • 5
  • 27
  • 56

1 Answers1

3

The program will loop forever if there is at least one pending snapshot. Perhaps it will be helpful to print what are those pending snapshots, by changing the script like this:

echo "There are $command snapshots waiting for completion."
ec2-describe-snapshots | grep pending

But surely that doesn't happen really infinitely. You probably just have to wait. When there are no more pending snapshots, the loop will stop. Really.

Btw here's a slightly improved version of your script. It's equivalent to yours, just the syntax is improved to remove some unnecessary stuff and replace old style writing with modern methods:

command=$(ec2-describe-snapshots | grep pending | wc -l)

# Check if we have any pending snapshots at all.
if [ $command = 0 ]
then
        echo "No snapshots are pending."
        ec2-describe-snapshots
else
        # Wait for the snapshot to finish.
        while [ $command != 0 ]
        do
                # Communicate that we're waiting.
                echo "There are $command snapshots waiting for completion."
                ec2-describe-snapshots | grep pending
                sleep 5

                # Re run the command.
                command=$(ec2-describe-snapshots | grep pending | wc -l)
        done

        # Snapshot has finished.
        echo
        echo "Snapshots are finished."
fi 
janos
  • 120,954
  • 29
  • 226
  • 236
  • see my updated code in the question. do you think it will work ? – user3086014 Dec 12 '13 at 06:45
  • you are giving me the exact answer which i have wrote – user3086014 Dec 12 '13 at 06:45
  • yeah my code is `echo "There are $command snapshots waiting for completion." sleep 5 # Re run the command. command=`ec2-describe-snapshots | grep pending | wc -l` – user3086014 Dec 12 '13 at 06:49
  • now when I am running my code ( given at the top) its working fine !! what should i do – user3086014 Dec 12 '13 at 06:51
  • To make it clear I added a full version. You'll see there a line you didn't have. That's what I meant but you didn't read properly. And btw your script was already fine, there is no problem here, you just wait for snapshots to complete, which sometimes may take a long time, apparently. – janos Dec 12 '13 at 06:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43013/discussion-between-user3086014-and-janos) – user3086014 Dec 12 '13 at 07:09
  • I found this updated code interesting, +1 and thanks! Also, previous question http://stackoverflow.com/questions/20515887/how-to-use-while-loop-to-check-condition-in-linux/20516467 – Smurker Dec 12 '13 at 08:48
  • Hi janos: please see this question : http://stackoverflow.com/questions/22678461/how-to-parse-json-with-shell-scripting-with-linux – user3086014 Mar 27 '14 at 06:15