Bash shell ‘if’比较不同命令的输出的语句

发布于 2024-12-10 23:21:06 字数 598 浏览 0 评论 0原文

使用改编后的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2024-12-17 23:21:07

您需要通过在前面添加 $ 来引用表达式的值:

...
if [ "$app1" = "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]; then
...

You need to refer to the value of your expression by prepending a $:

...
if [ "$app1" = "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]; then
...
烧了回忆取暖 2024-12-17 23:21:06

至少在其他 shell 中,您需要更加小心空格;方括号是命令名称,需要与前后单词分开。您还需要(同样在经典 shell 中)将变量嵌入双引号中:

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
then : do this instead...
fi

您可以在一行中完成所有操作(好吧,两行,因为我尽可能避免水平滚动条):

if [ "$(someapp    -flag | grep usefulstuff | cut -c  5-10)" = \
     "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]
then mkdir IPFolder-1
elif ...blah blah
then : do this instead...
fi

或者您可以使用两个单独的命令来完成捕获:

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
then : do this instead...
fi

更新:
添加了一些额外的引号。也可以引用这些作业:

app1="$(someapp -flag | grep usefulstuff | cut -c  5-10)"

不会造成任何伤害;对于 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:

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
then : do this instead...
fi

You could do it all in one line (well, two because I avoid the horizontal scrollbar whenever possible):

if [ "$(someapp    -flag | grep usefulstuff | cut -c  5-10)" = \
     "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]
then mkdir IPFolder-1
elif ...blah blah
then : do this instead...
fi

Or you could do it with two separate command captures:

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
then : do this instead...
fi

Update:
Some extra quotes added. It would be possible to quote the assignments too:

app1="$(someapp -flag | grep usefulstuff | cut -c  5-10)"

No harm would be done; it isn't strictly necessary with bash (but it may well have been necessary with archaic Bourne shell).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文