我正在尝试在shell脚本中编写代码。当我尝试将代码从批处理脚本转换为外壳脚本时,出现错误。
批处理文件代码
:: Create a file with all latest snapshots FOR /F "tokens=5" %%a in (' ec2-describe-snapshots ^|find "SNAPSHOT" ^|sort /+64') do set "var=%%a" set "latestdate=%var:~0,10%" call ec2-describe-snapshots |find "SNAPSHOT"|sort /+64 |find "%latestdate%">"%EC2_HOME%\Working\SnapshotsLatest_%date-today%.txt"
外壳脚本中的代码
#Create a file with all latest snapshots FOR snapshot_date in $(' ec2-describe-snapshots | grep -i "SNAPSHOT" |sort /+64') do set "var=$snapshot_date" set "latestdate=$var:~0,10" ec2-describe-snapshots |grep -i "SNAPSHOT" |sort /+64 | grep "$latestdate">"$EC2_HOME%/SnapshotsLatest_$today_date"
我想根据日期对快照进行排序,并将最新日期创建的快照保存在文件中。
ece-describe-snapshots的样本输出:
`SNAPSHOT snap-5e20 vol-f660 completed 2013-12-10T08:00:30+0000 100% 109030037527 10 2013-12-10: Daily Backup for i-2111 (VolID:vol-f9a0 InstID:i-2601)`
它将包含这样的记录
snaphsot的最新文件应包含:
SNAPSHOT snap-cdd617f3 vol-f66409a0 completed 2013-12-04T09:24:50+0000 100% 109030037527 10 2013-12-04: Daily Backup for Sanjay_Test_Machine (VolID:vol-f66409a0 InstID:i-26048111) SNAPSHOT snap-c7d617f9 vol-3d335f6b completed 2013-12-04T09:24:54+0000 100% 109030037527 10 2013-12-04: Daily Backup for sachin_test_VPC (VolID:vol-3d335f6b InstID:i-e1c443d6)
任何建议或线索表示赞赏。
它是一种代码气味,您必须两次运行命令。
不清楚您是否只需要最近一天的台词。尝试这个:
ec2-describe-snapshots | sort -rk 5 | awk ' $1 != "SNAPSHOT" {next} NR == 1 { split($5, a /T/); date = a[1]; } $5 ~ date {print} ' > "$EC2_HOME/SnapshotsLatest_$today_date"
如果您只想要 今天 的快照,那就更容易了
today=$(date +%F) ec2-describe-snapshots | sort -rk 5 | awk -v date=$today ' $1 == "SNAPSHOT" && $5 ~ date {print} ' > "$EC2_HOME/SnapshotsLatest_$today"