如何在鱼中的连接条件

发布于 2025-02-02 16:25:15 字数 600 浏览 2 评论 0原文

我正在尝试使用两个条件在鱼中使用IF块。这个想法很简单: 我想检查图像文件是否太宽,太高或两者兼而有之。 所以我写了以下脚本。

#!/bin/fish

set bla (identify $argv[1])
set blabla (string split ' ' $bla)
set siiz (string split x $blabla[3])

set xsiz $siiz[1]
set ysiz $siiz[2]


if test (test $xsiz -gt 1600) -a (test $ysiz -gt 900)
    echo "both $xsiz $ysiz"
else if test $ysiz -gt 900
    echo "tall"
else if test $xsiz -gt 1600
    echo "wide"
end

我已经进行了检查,以查看变量XSIZ和YSIZ具有正确的 值。 但是,无论我将脚本传递给脚本,它总是返回“两个”(甚至使用20 x 20像素的图像)´ 如果我尝试在没有括号或内部test命令的情况下传递test命令,Fish将抱怨未识别的命令。 这些文档没有关于这样的组成条件的例子。

I am trying to use a if block in fish using two conditions. The idea is simple:
I want to check if an image file is too wide, too tall, or both.
So I wrote the following script.

#!/bin/fish

set bla (identify $argv[1])
set blabla (string split ' ' $bla)
set siiz (string split x $blabla[3])

set xsiz $siiz[1]
set ysiz $siiz[2]


if test (test $xsiz -gt 1600) -a (test $ysiz -gt 900)
    echo "both $xsiz $ysiz"
else if test $ysiz -gt 900
    echo "tall"
else if test $xsiz -gt 1600
    echo "wide"
end

And I already performed check to see that the variables xsiz and ysiz have the right
values.
But no matter what I pass to the script, it always returns "both" (even using images of 20 by 20 pixels)´
If I try to pass the test command without the parenthesis or the inner test commands, fish will complain about not recognized commands.
The docs have none examples about a composed condition like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挖个坑埋了你 2025-02-09 16:25:15

您使它比需要的要复杂。请注意,命令替代命令除set命令的特殊情况外。您的版本等同于此:

if test (false) -a (true)
    echo yes
end

总是回荡“是”(为什么false不会导致测试评估为false是应该修复的语言的怪癖,恕我直言)。

只需进行一次测试:

if test $xsiz -gt 1600 -a $ysiz -gt 900

You're making it more complicated than it needs to be. Note that command substitution doesn't capture the exit status of the command except for the special case of the set command. Your version is equivalent to this:

if test (false) -a (true)
    echo yes
end

That always echoes "yes" (why the false doesn't cause the test to evaluate to false is a quirk of the language that should be fixed, IMHO).

Just do a single test:

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