ksh storing result of a command to a variable

后端 未结 5 920
清酒与你
清酒与你 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:22

    The problem that you're having is that the command needs to be surrounded by back-ticks rather than single quotes. This is known as 'Command Substitution'.

    Bash allows you to use $() for command substitution, but this is not available in all shells. I don't know if it's available in KSH; if it is, it's probably not available in all versions.

    If the $() syntax is available in your version of ksh, you should definitely use it; it's easier to read (back ticks are too easy to confuse with single quotes); back-ticks are also hard to nest.

    This only addresses one of the problems with your command, however: ls returns directories as well as files, so if the most recent thing modified in the specified directory is a sub-directory, that is what you will see.

    If you only want to see files, I suggest using some version of the following (I'm using Bash, which supports default variables, you'll probably have to play around with the syntax of $1)

    lastfile () 
    { 
        find ${1:-.} -maxdepth 1 -type f -printf "%T+ %p\n" | sort -n | tail -1 | sed 's/[^[:space:]]\+ //'
    }
    

    This runs find on the directory, and only pulls files from that directory. It formats all of the files like this:

    2012-08-29+16:21:40.0000000000 ./.sqlite_history
    2013-01-14+08:52:14.0000000000 ./.davmail.properties
    2012-04-04+16:16:40.0000000000 ./.DS_Store
    2010-04-21+15:49:00.0000000000 ./.joe_state
    2008-09-05+17:15:28.0000000000 ./.hplip.conf
    2012-01-31+13:12:28.0000000000 ./.oneclick
    

    sorts the list, takes the last line, and chops off everything before the first space.

提交回复
热议问题