ksh storing result of a command to a variable

后端 未结 5 884
清酒与你
清酒与你 2021-01-13 15:55

I want to store the result of a command to a variable in my shell script. I cant seem to get it to work. I want the most recently dated file in the directory.



        
5条回答
  •  再見小時候
    2021-01-13 16:33

    I would do something like:

    Your version corrected:

    PRODUCT=$(ls -t /some/dir/file* | head -1 | xargs -n1 basename)
    

    Or simpler:

    PRODUCT=$(cd /some/dir && ls -1t file* | head -1)
    
    • change to the directory
    • list one filename per line and sort by time/date
    • grab the first line

提交回复
热议问题