How to fix the error in the bash shell script?

前端 未结 2 1095
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 10:32

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

:: Cr         


        
2条回答
  •  萌比男神i
    2021-01-29 11:24

    Minor issue, you don't need a pipe when re-directing output, so your line to save should be

    ec2-describe-snapshots | grep SNAPSHOT.*$latestdate  > "$EC2_HOME/SnapshotsLatest_$today_date"
    

    Now the main issue here, is that the grep is messed up. I haven't worked with amazon snapshots, but judging by your example descriptions, you should be doing something like

    latestdate=$(ec2-describe-snapshots | grep -oP "\d+-\d+-\d+" | sort -r | head -1)
    

    This will get all the dates containing the form ffffdd-dd-dd from the file (I'm assuming the two dates in each snapshot line always match up), sort them in reverse order (latest first) and take the head which is the latest date, storing it in $latestdate.

    Then to store all snapshots with the given date do something like

    ec2-describe-snapshots | grep -oP "SNAPSHOT(.*?)$lastdateT(.*?)\)" > "$EC2_HOME/SnapshotsLatest_$today_date"
    

    This will get all text starting with SNAPSHOT, containing the given date, and ending in a closing ")" and save it. Note, you may have to mess around with it a bit, if ")" can be present elsewhere.

提交回复
热议问题