我可以在 UNIX shell 中执行嵌套或链接命令吗?
我可以在 UNIX shell 中的另一个命令中执行命令吗?
如果不可能,我可以使用上一个命令的输出作为下一个命令的输入,如:
command x
then command y
,
其中command y< /code> 我想使用
command x
的输出?
Can I execute command within another command in UNIX shells?
If impossible, can I use the output of the previous command as the input of next command, as in:
command x
then command y
,
where in command y
I want use the output of command x
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为此使用反引号。
例如,这将捕获 file.txt
这将打印日期
反引号之间的代码将被执行并被其结果替换。
You can use the backquotes for this.
For example this will cat the file.txt
And this will print the date
The code between back-quotes will be executed and be replaced by its result.
你可以做类似的事情;
这里
dirname "$path"
将首先运行,其结果将被替换,然后 grep 将运行,在file
dirname 的结果>You can do something like;
here
dirname "$path"
will run first and its result will be substituted and then grep will run, searching for the result ofdirname
in thefile
你到底想做什么?从您正在执行的命令中尚不清楚。也许如果您描述您正在寻找的东西,我们可以为您指明正确的方向。如果要对“find”命令返回的一系列文件(或目录)名称执行命令,Colin 是正确的,您需要查看“find”的“-exec”选项。如果您希望对文件中列出的一堆参数或来自标准输入的参数执行命令,则需要查看“xargs”命令。如果您想将单个命令的输出放到另一个命令的命令行上,那么使用“$(command)”(或“command”[用反引号替换“”)即可完成这项工作。有很多方法可以做到这一点,但是如果不知道您正在尝试的是什么,就很难提供更多帮助。
What exactly are you trying to do? It's not clear from the commands you are executing. Perhaps if you describe what you're looking for we can point you in the right direction. If you want to execute a command over a range of file (or directory) names returned by the "find" command, Colin is correct, you need to look at the "-exec" option of "find". If you're looking to execute a command over a bunch of arguments listed in a file or coming from stdin, you need to check out the "xargs" commands. If you want to put the output of a single command on to the command line of another command, then using "$(command)" (or 'command' [replace the ' with a backquote]) will do the job. There's a lot of ways to do this, but without knowing what it is you're trying it's hard to be more helpful.
这是我使用嵌套系统命令的示例。
我在 find 命令之上运行了“ls -ltr”。它执行
它连续地出现在查找输出上。
ls -ltr $(find .-name "srvm.jar")
Here is an example where I have used nested system commands.
I had run "ls -ltr" on top of find command. And it executes
it serially on the find output.
ls -ltr $(find . -name "srvm.jar")