Bash shell ‘if’比较不同命令的输出的语句
使用改编后的 Sam Ruby 给我的示例,我进行了调整,以便我可以展示我想要实现的目标。
app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
if [$app1 = (someapptwo -flag | grep usefulstuff | cut -c 20-25)]; then
mkdir IPFolder-1
elif ...blah blah
fi
我可以使用上面显示的 grep 还是我找错了树?或者它应该看起来像这样:
app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
app2=$(someapptwo -flag | grep usefulstuff | cut -c 20-25)
if [$app1 = $app2]; then
mkdir IPFolder-1
elif ...blah blah
fi
Using an adapted example given to me by Sam Ruby which I have tweaked so I can show what I'm trying to achieve.
app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
if [$app1 = (someapptwo -flag | grep usefulstuff | cut -c 20-25)]; then
mkdir IPFolder-1
elif ...blah blah
fi
Can I use grep as show above or am I barking up the wrong tree? or should it look a little some thing like this:
app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
app2=$(someapptwo -flag | grep usefulstuff | cut -c 20-25)
if [$app1 = $app2]; then
mkdir IPFolder-1
elif ...blah blah
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要通过在前面添加 $ 来引用表达式的值:
You need to refer to the value of your expression by prepending a $:
至少在其他 shell 中,您需要更加小心空格;方括号是命令名称,需要与前后单词分开。您还需要(同样在经典 shell 中)将变量嵌入双引号中:
您可以在一行中完成所有操作(好吧,两行,因为我尽可能避免水平滚动条):
或者您可以使用两个单独的命令来完成捕获:
更新:
添加了一些额外的引号。也可以引用这些作业:
不会造成任何伤害;对于
bash
来说,这并不是绝对必要的(但对于古老的 Bourne shell 来说,它很可能是必要的)。At least in other shells, you need to be a lot more careful with spaces; the square bracket is a command name and needs to be separated from previous and following words. You also need (again in classic shells for certain) to embed the variables in double quotes:
You could do it all in one line (well, two because I avoid the horizontal scrollbar whenever possible):
Or you could do it with two separate command captures:
Update:
Some extra quotes added. It would be possible to quote the assignments too:
No harm would be done; it isn't strictly necessary with
bash
(but it may well have been necessary with archaic Bourne shell).