shell 脚本中的错误? [英] error in shell scripting?
问题描述
我正在尝试使用 shell 脚本编写代码.当我尝试将代码从批处理脚本转换为 shell 脚本时,出现错误.
I am trying a code in shell script. while I am trying to convert the code from batch script to shell script I am getting an error.
批处理文件代码
:: 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%WorkingSnapshotsLatest_%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"
我想根据日期对快照进行排序,并将最新创建的快照保存在一个文件中.
I want to sort the snapshots according to dates and to save the snapshots that are created in latest date in a file.
ece-describe-snapshots 的示例输出:
SAMPLE OUTPUT OF 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 最新文件应该包含:
the snaphsot latest file should cointain:
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)
感谢任何建议或领导.
推荐答案
这是一种代码味道,您必须运行该命令两次.
Its a code smell that you have to run the command twice.
不清楚您是否只想要最近一天的台词.试试这个:
It was unclear that you wanted just the lines for the most recent day. Try this:
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"
如果你只想要今天的快照,那就更简单了
If you only want today's snapshots, even easier
today=$(date +%F)
ec2-describe-snapshots | sort -rk 5 | awk -v date=$today '
$1 == "SNAPSHOT" && $5 ~ date {print}
' > "$EC2_HOME/SnapshotsLatest_$today"
这篇关于shell 脚本中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!