0

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%\Working\SnapshotsLatest_%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.

5
  • 2
    What error are you getting? Shell is case sensitive, use for instead of FOR. Commented Dec 10, 2013 at 8:39
  • Describe what you want to do and provide input data and desired output. Commented Dec 10, 2013 at 9:01
  • i have some snapshots made on amazon, i want to find the latest snapshots made on a date and then want to store them in a file. like date 2013-12-10 snapshots made on this date should be stored in file. Contents of snapshotslatest file should be SNAPSHOT snap-c17f3 vol-f69a0 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 sacht_VPC (VolID:vol-3db InstID:i-ed6) Commented Dec 10, 2013 at 9:23
  • @user3086014, please put your sample input and output up in the question where it can be formatted properly. In particular, please show the output of ec2-describe-snapshots Commented Dec 10, 2013 at 21:58
  • @glennjackman: i have added the sample output of ec2-describe-snapshots in the question itself Commented Dec 11, 2013 at 4:44

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.