如何使用whil​​e循环来检查条件在Linux? [英] How to use while loop to check condition in linux?

查看:183
本文介绍了如何使用whil​​e循环来检查条件在Linux?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做这样的事情:

 而(条件)

做
 等待
 

我必须做的,这是在Linux操作系统。虽然快照的状态,正在等待它应该等待。

的EC2-描述快照内容是:

 快照管理单元c7f3卷-f6a0完成2013-12-04T09:24:50 + 0000 100%109030037527 10 2013年12月4日:每日备份的SaMachine(VolID:VOL- f09a0 InstID:I-2604)
快照管理单元c7df9卷-3f6b完成2013-12-04T09:24:54 + 0000 100%109030037527 10 2013年12月4日:每日备份的sa_test_VPC(VolID:卷-3InstID:I-e1c46)
 

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

 #!/斌/庆典

#设置的环境变量

出口EC2_HOME = /选择/ EC2 /工具
出口AWS_ACCESS_KEY_ID =
出口AWS_SECRET_ACCESS_KEY =
出口SOURCE_REGION =美西2
出口DESTINATION_REGION =美东1


#设置变量今日最新为当前日期
today_date =`日期+%Y-%M-%D`
回声$ today_date


#设置变量date_dir到昨天的日期
date_dir = $(日期+%Y-%M-%D -d'-1天)
回声$ date_dir


#First删除所有快照超过一天
#Create文件与所有计划的快照

回声首先删除所有快照超过一天
回声与所有计划的快照创建一个文件
阅读-rsp $'preSS回车键继续...... \ N'
EC2-描述,快照| grep的-i$ date_dir>中$ EC2_HOME / SnapshotsDOW_ $ today_date


对于旧的备份#DELETE快照

回声为删除旧的备份快照
阅读-rsp $'preSS回车键继续...... \ N'
快照在$(猫$ EC2_HOME / SnapshotsDOW_ $ today_date| awk的'{打印$ 2})
  做
      EC2-删除快照$快照
做完


#Now每一个连接卷每个实例创建快照
#Create文件和所有挂接的卷

回声其所附的卷创建一个文件
阅读-rsp $'preSS回车键继续...... \ N'
#EC2-描述,体积| grep按附加>中$ EC2_HOME / ActiveVolumes_ $ today_date


#Create所有实例的文件

回声的所有实例创建一个文件
阅读-rsp $'preSS回车键继续...... \ N'
EC2-描述-实例| grep的-i标签| grep按名>中$ EC2_HOME / Instances_ $ today_date


所有附加卷#Create快照

回声创建连接的所有卷的快照
阅读-rsp $'preSS回车键继续...... \ N'
awk的'{打印$ 2,$ 3}'$ EC2_HOME / ActiveVolumes_ $ today_date|而读vol_id INST_ID;做
    awk的'{打印$ 3,$ 5}'$ EC2_HOME / Instances_ $ today_date|而读inst_id2名;做
        如果测试$ INST_ID=$ inst_id2;然后
            回声EC2创建快照$ vol_id-d$ today_date:每日备份为$ INST_ID(VolID:$ vol_id InstID:$ INST_ID)
             EC2创建快照$ vol_id-d$ today_date:每日备份为$ INST_ID(VolID:$ vol_id InstID:$ INST_ID)
        科幻
    做完
做完



#Create所有最新的快照文件

回声创造了所有最新的快照文件
阅读-rsp $'preSS回车键继续...... \ N'
latestdate = $(EC2-描述,快照| grep的^快照|排序-rk 5 | awk的'{打印SUBSTR($ 5,1,10);}退出)
EC2-描述,快照| grep的^快照* $ latestdate。> $ EC2_HOME / SnapshotsLatest_ $ today_date


#Copy在多个地区的快照。

回声复制快照在多个地区。
阅读-rsp $'preSS回车键继续...... \ N'
快照在$(猫$ EC2_HOME / SnapshotsLatest_ $ today_date| awk的'{打印$ 2})
做
  EC2复制快照-r $ SOURCE_REGION -s $快照-region $ DESTINATION_REGION
做完


#Send使用mail命令最新的快照细节的用户。
回声使用mail命令发送最新的快照细节,以用户
阅读-rsp $'preSS回车键继续...... \ N'
邮件-s虚拟机快照$ today_date'jp.com< $ EC2_HOME / SnapshotsLatest_ $ today_date

阅读-rsp $'preSS进入退出... \ N'
 

解决方案

写了,而循环很简单,只要这样的:

 ,而EC2-描述,快照| grep的-q'^快照。*挂起'
做
    回声未决的快照,等待着......
    睡眠10
做完
回声无待快照。
 

I have to do something like this :

while ( condition)

do
 wait 

I have to do this is in linux. While the state of snapshot is pending it should wait.

Content of ec2-describe snapshot is :

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)

How to do this? How should I use grep and all that ?

#!/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'

解决方案

Writing that while loop is as simple as this:

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

这篇关于如何使用whil​​e循环来检查条件在Linux?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆