Linux ksh 将命令的结果存储到变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14568396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ksh storing result of a command to a variable
提问by user1769538
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.
我想将命令的结果存储到我的 shell 脚本中的一个变量中。我似乎无法让它工作。我想要目录中最新日期的文件。
PRODUCT= 'ls -t /some/dir/file* | head -1 | xargs -n1 basename'
it wont work
它不会工作
回答by Carl Norum
回答by Barton Chittenden
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.
Bash 允许您$()
用于命令替换,但这并非在所有 shell 中都可用。不知道KSH有没有;如果是,它可能并非在所有版本中都可用。
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.
如果该$()
语法在您的 ksh 版本中可用,您绝对应该使用它;它更容易阅读(反引号太容易与单引号混淆);反引号也很难嵌套。
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.
然而,这仅解决了您的命令的问题之一:ls
返回目录和文件,因此如果在指定目录中修改的最新内容是子目录,这就是您将看到的。
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
)
如果你只想查看文件,我建议使用以下的一些版本(我使用的是 Bash,它支持默认变量,你可能不得不使用 的语法$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:
这会在目录上运行 find ,并且只从该目录中提取文件。它格式化所有文件,如下所示:
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.
对列表进行排序,取最后一行,并在第一个空格之前切掉所有内容。
回答by doniyor
you have two options, either $
or backsticks`
.
你有两个选择,要么 要么$
backsticks `
。
1) x=$(ls -t /some/dir/file* | head -1 | xargs -n1 basename)
1) x=$(ls -t /some/dir/file* | head -1 | xargs -n1 basename)
or
或者
2) x=`ls -t /some/dir/file* | head -1 | xargs -n1 basename`
2) x=`ls -t /some/dir/file* | head -1 | xargs -n1 basename`
echo $x
Edit:removing unnecessary bracket for (2).
编辑:删除(2)不必要的括号。
回答by Olivier Dulac
You need both quotes to ensure you keep the name even if it contains spaces, and also in case you later want more than 1 file, and "$(..)" to run commands in background
您需要两个引号以确保您保留名称,即使它包含空格,并且如果您以后需要超过 1 个文件,以及“$(..)”在后台运行命令
I believe you also need the '-1' option to ls, otherwise you could have several names per lines (you only keep 1 line, but it could be several files)
我相信您还需要 ls 的“-1”选项,否则每行可能有多个名称(您只保留 1 行,但可能是多个文件)
PRODUCT="$(ls -1t /some/dir/file* | head -1 | xargs -n1 basename)"
Please do not put space around the "=" variable assignments (as I saw on other solutions here) , as it's not very compatible as well.
请不要在“=”变量赋值周围放置空格(正如我在其他解决方案中看到的那样),因为它也不太兼容。
回答by Yelali
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
- 切换到目录
- 每行列出一个文件名并按时间/日期排序
- 抢第一行