Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

linux - error in shell scripting?

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.

BATCH FILE CODE

:: 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"

CODE IN SHELL SCRIPT

#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.

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)`

It will contain records like this

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)

Any suggestion or lead is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...