for 循环 shell 脚本中变量的命令结果
如何:将命令结果传递给变量?
for file in find "$1" do
var = $file | cut -d/ -f6-
echo $var
...
done
How to: a command result to a variable?
for file in find "$1" do
var = $file | cut -d/ -f6-
echo $var
...
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几点提示:
在 shell 脚本中,空格确实很重要。您的代码
var = $file
是一个错误,因为不存在知道如何处理参数=
和$file 的可执行文件
。使用var
代替。
捕获
$file | 的输出cut -d/ -f6-
pipeline 您将需要以下语法:在最新版本的
bash
中,您可以使用“here string”代替,并避免echo< /code> 和管道。
我注意到您还尝试处理
find
命令的结果,但语法也不正确。正确的语法是我必须再次询问您“field 6”正在做什么,因为您已经问过类似的问题 之前。
A few pointers:
In shell scripts whitespace really matters. Your code
var = $file
is an error, since no executablevar
exists which knows how to process the arguments=
and$file
. UseInstead.
To capture the output of your
$file | cut -d/ -f6-
pipeline you will need the following syntax:In
bash
of recent version you can use a "here string" instead and avoid the expense ofecho
and the pipe.I note that you are also attempting to process the results of a
find
command, also with incorrect syntax. The correct syntax for this isI must again question you as to what "field 6" is doing, since you've asked a similar question before.
你的问题不太清楚,但
你想要什么?
Your question wasn't all that clear, but is
what you were after?