Shell Scripting 三元运算符获取字符串结果 [英] Shell Scripting Ternary operator to get string result

查看:45
本文介绍了Shell Scripting 三元运算符获取字符串结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 shell 脚本中,我使用的是这样的三元运算符:

In shell scripting, I am using ternary operator like this:

(( numVar == numVal ? (resVar=1) : (resVar=0) ))

我观看了 Derek Banas 的 shell 脚本教程,并在视频的 41:00 获得了上述语法

I watched shell scripting tutorial by Derek Banas and got the above syntax at 41:00 of the video

https://www.youtube.com/watch?v=hwrnmQumtPw&t=73s

当我们为 resVar 分配数字时,上面的代码有效,但是如果我尝试为 resVar 分配一个字符串,它总是返回 0.

The above code works when we assign numbers to resVar, but if I try to assign a string to resVar, it always returns 0.

(( numVar == numVal ? (resVar="Yop") : (resVar="Nop") ))

也试过

resVar=$(( numVar == numVal ? (echo "Yop") : (echo "Nop") ))

那么正确的做法是什么?

So which is the right way to do this?

推荐答案

您没有告诉我们您使用的是什么 shell,但您可能使用了bash 或类似的东西.Bash 中的三元运算符仅适用于数字在 ARITHMETIC EVALUATION 部分下的 man bash 中解释:

You didn't tell us what shell you use but it's possible you use bash or something similar. Ternary operator in Bash works only with numbers as explained in man bash under ARITHMETIC EVALUATION section:

shell 允许计算算术表达式,在某些情况(参见 let 和 declare 内置命令和算术扩展).评估以固定宽度的整数完成没有检查溢出,尽管除以 0 被捕获并且标记为错误.运算符及其优先级,关联性和值与 C 语言中的相同.这以下运营商列表分为几个级别等优先级运算符.级别按顺序列出降低优先级.
(...)

The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for over- flow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
(...)

expr?expr:expr

expr?expr:expr

条件运算符

以及当您使用 "Yop"resVar 被分配 0 的原因或"Nop" 是因为这样的字符串不是 bash 中的有效数字,并且因此它被评估为 0.在 man bash 中也有解释同段:

And the reason that resVar is assigned 0 when you use "Yop" or "Nop" is because such string is not a valid number in bash and therefore it's evaluated to 0. It's also explained in man bash in the same paragraph:

空值的计算结果为 0.

A null value evaluates to 0.

这个维基百科中也有解释文章 如果你找到了更容易阅读:

It's also explained in this Wikipedia article if you find it easier to read:

真正的三元运算符只存在于算术表达式中:

A true ternary operator only exists for arithmetic expressions:

((结果 = 条件 ? value_if_true : value_if_false))

((result = condition ? value_if_true : value_if_false))

对于字符串,只有解决方法,例如:

For strings there only exist workarounds, like e.g.:

result=$([ "$a" == "$b" ] && echo "value_if_true" || echo"value_if_false")

result=$([ "$a" == "$b" ] && echo "value_if_true" || echo "value_if_false")

(其中 "$a" == "$b" 可以是任意条件测试,相应的 [, 可以评估.)

(where "$a" == "$b" can be any condition test, respective [, can evaluate.)

这篇关于Shell Scripting 三元运算符获取字符串结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆