Linux 代码在无限循环中运行

Linux 代码在无限循环中运行,linux,shell,amazon-ec2,Linux,Shell,Amazon Ec2,我有一个shell脚本中的代码,如下所示: # 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-snapsho

我有一个shell脚本中的代码,如下所示:

    # 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 
这段代码有时工作正常,有时工作不正常。它进入一个无限循环。我想这样做,我想检查
ec2 descripe snapshot
的输出,如果snaphost处于挂起状态。如果是,则应等待所有快照完成

ec2描述快照的输出是

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)
如果至少有一个挂起的快照,程序将永远循环。通过如下更改脚本,打印哪些是挂起的快照可能会有所帮助:

echo "There are $command snapshots waiting for completion."
ec2-describe-snapshots | grep pending
但这肯定不会无限期地发生。你可能只需要等待。当没有更多挂起的快照时,循环将停止。真的

顺便说一句,这里有一个稍微改进的脚本版本。它与您的相同,只是语法有所改进,删除了一些不必要的内容,并用现代方法取代了旧式书写:

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 

提示,可能在脚本中使用
logger
。这是什么?我没有得到任何东西您的
echo
命令是否显示有非零数量的快照等待完成?或者您是否看到
有0个快照等待完成
并且仍然循环?否显示非零否。就像
有1个快照等待完成。
这确实意味着至少有一个待处理的快照…请参阅问题中我更新的代码。你认为它会起作用吗?你给了我我所写的确切答案。是的,我的代码是
echo“有$command快照等待完成。”sleep 5#重新运行该命令。command=
ec2描述快照| grep pending | wc-l`现在当我运行我的代码(在顶部给出)时,它工作正常!!我应该做什么来说明我添加了完整版本。你会看到一条你没有的线。这就是我的意思,但你没有正确阅读。顺便说一句,您的脚本已经很好了,这里没有问题,您只需等待快照完成,显然,有时候可能需要很长时间。