【问题标题】:How to use while loop to check condition in linux?如何使用while循环检查linux中的条件?
【发布时间】:2013-12-29 05:37:12
【问题描述】:

我必须这样做:

while ( condition)

do
 wait 

我必须这样做是在 linux 中。当快照的状态处于挂起状态时,它应该等待。

ec2-describe 快照的内容是:

SNAPSHOT    snap-c7f3   vol-f6a0    completed   2013-12-04T09:24:50+0000    100%    109030037527    10  2013-12-04: Daily Backup for SaMachine (VolID:vol-f09a0 InstID:i-2604)
SNAPSHOT    snap-c7df9  vol-3f6b    completed   2013-12-04T09:24:54+0000    100%    109030037527    10  2013-12-04: Daily Backup for sa_test_VPC (VolID:vol-3InstID:i-e1c46)

如何做到这一点?我应该如何使用 grep 和所有这些?

#!/bin/bash

# Setting the environmental variables

export EC2_HOME=/opt/ec2/tools
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export SOURCE_REGION="us-west-2"
export DESTINATION_REGION="us-east-1" 


# Set the variable today-date to current date
today_date=`date +%Y-%m-%d`
echo "$today_date"


# Set the variable date_dir to yesterday's date
date_dir=$(date +%Y-%m-%d -d '-1 days')
echo "$date_dir"


#First delete all snapshots older than one day 
#Create a file with all scheduled snapshots

echo "First delete all snapshots older than one day"
echo "Create a file with all scheduled snapshots"
read -rsp $'Press enter to continue...\n'
ec2-describe-snapshots | grep -i "$date_dir">"$EC2_HOME/SnapshotsDOW_$today_date"


#Delete snapshots for older backups

echo "Delete snapshots for older backups"
read -rsp $'Press enter to continue...\n'
for snapshot in $(cat "$EC2_HOME/SnapshotsDOW_$today_date" | awk '{print $2}')
  do
      ec2-delete-snapshot $snapshot
done


#Now create a snapshot for every attached volume to every instance
#Create a file with all attached volumes

echo "Create a file with all attached volumes"
read -rsp $'Press enter to continue...\n'
#ec2-describe-volumes   | grep -i "attached" >"$EC2_HOME/ActiveVolumes_$today_date"


#Create a file with all instances

echo "Create a file with all instances"
read -rsp $'Press enter to continue...\n'
ec2-describe-instances | grep -i "tag" | grep -i "name" >"$EC2_HOME/Instances_$today_date"


#Create snapshots of all attached volumes

echo "Create snapshots of all attached volumes"
read -rsp $'Press enter to continue...\n'
awk '{print $2, $3}' "$EC2_HOME/ActiveVolumes_$today_date" | while read vol_id inst_id; do
    awk '{print $3, $5}' "$EC2_HOME/Instances_$today_date" | while read inst_id2 name; do
        if test "$inst_id" = "$inst_id2"; then
            echo ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
             ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
        fi
    done
done



#Create a file with all latest snapshots

echo "Create a file with all latest snapshots"
read -rsp $'Press enter to continue...\n'
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" > "$EC2_HOME/SnapshotsLatest_$today_date"


#Copy the snapshot across multiple regions.

echo "Copy the snapshot across multiple regions."
read -rsp $'Press enter to continue...\n'
for snapshot in $(cat "$EC2_HOME/SnapshotsLatest_$today_date" | awk '{print $2}') 
do 
  ec2-copy-snapshot -r $SOURCE_REGION -s $snapshot -region $DESTINATION_REGION
done


#Send the latest snapshot details to user using mail command.
echo "Send the latest snapshot details to user using mail command"
read -rsp $'Press enter to continue...\n'
mail -s 'VM Snapshot $today_date' jp.com <  "$EC2_HOME/SnapshotsLatest_$today_date"

read -rsp $'Press enter to exit...\n'

【问题讨论】:

  • 你用什么命令来得到那个输出?
  • ec2-describe-snapshots(一个 ec2-命令行实用程序)
  • 您需要完成所有快照还是只是在寻找特定快照?
  • 不应该完成所有的快照。我正在尝试这样的事情但不工作:while(ec2-describe-snapshots | grep -i "status" !="completed") do echo "sleep" sleep 5 done
  • 等一下,我给你写点东西。

标签: linux shell amazon-ec2


【解决方案1】:

编写while 循环就这么简单:

while ec2-describe-snapshots | grep -q '^SNAPSHOT.*pending'
do
    echo "There are pending SNAPSHOTs, waiting..."
    sleep 10
done
echo "No pending SNAPSHOTs."

【讨论】:

  • 嗨 janos,我又在编写 shell 脚本,需要你的帮助。我发布了一个新问题,请检查。谢谢
  • 请看看我的问题。我卡住了,只有你能帮助我:(\
  • @jaypal 有能力帮助您。但你必须更清楚。清楚多了。请阅读this article。并欣赏它。并遵循它。这很关键。在您完全理解那篇文章之前,您将很难在这里获得答案。您的问题/答案往往有 10 多个 cmets,这在这里不正常,并且对于回答您的人来说是浪费时间。您还需要更好地了解 shell。花时间学习基础知识。我得走了,祝你好运。
【解决方案2】:
until ec2-describe-snapshots | grep -q ' completed '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."

如果您需要完成所有快照,您可以改用:

until ! ec2-describe-snapshots | grep -q ' pending '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."

更新

而且,处理无输出情况:

until [ -n "`ec2-describe-snapshots`" ]
do
    echo "snapshot not yet started, still waiting..."
    sleep 10
done
until ! ec2-describe-snapshots | grep -q ' pending '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."

【讨论】:

  • @macros:错误:猫:ec2-describe-snapshots: No such file or directory snapshot is pending, waiting...
  • 对不起,不要使用'cat'...我在测试后忘记将其删除,您给出的命令输出... ;-) 我已经更正了答案...
  • 即使快照已完成,循环仍在继续。并且即使存储库中没有快照,它仍然可以工作
  • 奇怪,如果命令“ec2-describe-snapshots”输出你给的文本,它对我有用......而且,对不起,我不太明白你的意思是“当存储库中没有快照,即使它正在工作”...当存储库中没有快照时,您可能应该指定命令的输出...
  • 请检查我的上次更新...如果它对您不起作用,了解有关错误的更多详细信息会很有用...
【解决方案3】:

如果你可以把它放在一个 shellscript 中,我会这样做。

编辑

所以在阅读了您当前的代码之后,这就是我要做的。不过,您必须自己进行测试。

# This is where you're creating your snapshots...

# 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 "Snapshots are finished."
fi       

# This is where you're writing snapshots to file...

旧脚本留作参考

#!/bin/sh

waitForSnapshot() {
        # Make sure we actually passed a snapshot to grep for.
        if [ -n "$1" ]
        then
                # Setup the command.
                command=`ec2-describe-snapshots | grep $1 | awk '{print $4}'`

                # Wait for the snapshot to finish.
                while [ $command != "completed" ]
                do
                        sleep 1

                        # Re run the command.
                        command=`ec2-describe-snapshots | grep $1 | awk '{print $4}'`
                done

                # Snapshot has finished.
                echo "Snapshot '$1' has finished."
        else
                echo "No snapshot was passed to us."
        fi

}

waitForSnapshot "snap-c7f3"

【讨论】:

  • 它只适用于单个快照还是命令返回的所有快照?
  • 以及为什么你在一个函数中做到了。请不要将其作为功能。我的脚本中没有函数
  • 这适用于单个快照。我把它变成一个函数的原因是你可以重用相同的代码来等待你选择的任意数量的快照。您可以根据需要多次调用 waitForSnapshot "snapshotName"。
  • 但我必须针对多个快照对此进行测试。我不能手动将值作为参数传递。请提出其他建议
  • 当然,我很乐意帮助你。所以你说你需要多个快照,但你不想手动输入它们。您如何知道要测试哪些快照?是命令 ec2-describe-snapshots 输出的所有快照吗?还是您从另一个命令中获取它们?
猜你喜欢
  • 2017-04-03
  • 2011-09-09
  • 2019-11-23
  • 2011-03-31
  • 2023-03-06
  • 2015-04-04
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多