How to fix the error in the bash shell script?

前端 未结 2 1069
被撕碎了的回忆
被撕碎了的回忆 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-29 11:30

    Neither the batch script nor the shell script you posted are a good starting point so let's start from scratch. Sorry, this is too big for a comment.

    You want to find the latest snapshots made on a date and then want to store them in a file.

    What does that mean?

    Do the snapshot files have a timestamp in their name or in their content?

    If not - UNIX does not store file creation timestamps so is a last-modified timestamp adequate?

    Do you literally want to concatenate all of your snapshot files into one singe file or do you want to create a file that has a list of the snapshot file names?

    Post some sample input (e.g. some snapshot file names and contents if that's where the timestamp is stored) and the expected output given that input.

    Update your question to address all of the above, do not try to reply in a comment.

    0 讨论(0)
提交回复
热议问题