巴什if语句来检查。如果字符串等几个字符串常量之一 [英] Bash If-statement to check If string is equal to one of several string literals
问题描述
我需要确保的说法我bash脚本或者是 - ,0或+(不含引号)
I need to ensure that the argument to my bash script is either "-", "0", or "+" (without quotes).
所以,我做了如下声明如果:
So, I made the following If statement:
LEVEL=$1
if [ "$LEVEL" -ne "-" ] && [ "$LEVEL" -ne "0" ] && [ "$LEVEL" -ne "+" ]
then
echo "The value of LEVEL must be either -, 0, or +!"
exit 1
fi
但它给我的错误 [: - :整数EX pression有望
,指的是用if语句条件的行
But it's giving me the error [: -: integer expression expected
, referring to the line with the If-statement conditions.
我一直在试图很多不同的语法(例如,双单VS括号,报价VS非引用变量和字符串),但我不明白。
I've been trying lots of different syntaxes (e.g., double vs single brackets, quoting vs non-quoting the variables and string literals), but I can't figure it out.
推荐答案
传统的方法,与POSIX兼容的SH,而不是利用bash化,是使用情况
语句:
The conventional approach, compatible with POSIX sh rather than leveraging bashisms, is to use the case
statement:
case $level in
-|0|+)
echo "Got it!" ;;
*)
echo "Not a valid value" ;;
esac
这是说,如果你想使用测试
,你可以做到这一点:
That said, if you wanted to use test
, you could do that too:
if [ "$LEVEL" != "-" ] && [ "$LEVEL" != "0" ] && [ "$LEVEL" != "+" ]; then
...
fi
!=
是否定的字符串比较操作符和 =
(不是 ==
)与POSIX兼容的正匹配之一。 (猛砸扩展POSIX支持 ==
的测试,但使采用这种扩展将让你陷入困境的习惯,如果你尝试写code为一个纯粹的POSIX后壳)。
!=
is the negating string comparison operator, and =
(not ==
) is the POSIX-compatible positive-matching one. (Bash extends POSIX to support ==
in test, but making a habit of using this extension will get you into trouble if you try to write code for a pure POSIX shell later).
在一个评论,一个跟进的问题是问,是否一组可能的字符可以从变量中读取方面。一般情况下,是的,尽管也有一些注意事项:
In a comment, a follow-up question was asked, in terms of whether the set of possible characters could be read from a variable. In general, yes, though there are some caveats:
possible_levels='-0+'
possible_levels_pattern="[${possible_levels}]"
if [[ $value = $possible_levels_pattern ]]; then
echo "value contains a valid level"
fi
......或者,真正给你,你要问什么,可以的方式,是完全安全的:
...or, to really give you exactly what you're asking for, in a manner that's entirely safe:
possible_levels=' - 0 +'
read -a possible_levels_array <<<"$possible_levels"
check_level() {
local possible_level
local value=$1
for possible_level in "${possible_levels_array[@]}"; do
[[ $value = "$possible_level" ]] && return 0
done
return 1
}
if check_level "$value"; then
echo "$value is a valid level"
else
echo "$value is not a valid level"
fi
显然,这是一个大量的工作,而在一般情况下,只是硬编码比较适当的时候(可能没有安全的丢失)会麻烦您节省了试图让事情更通用比他们真正需要的人。
Obviously, this is a lot of work, and in general, just hardcoding the comparisons when appropriate (and possible without a loss of safety) will save you trouble over trying to make things more generic than they truly need to be.
现在,如果我们能够在变量作为关联数组传递,而不需要支持愚蠢的空格分隔列表的东西,这使得它更短:
Now, if we could pass in the variable as an associative array, without needing to support the silly space-separated-list thing, that makes it much shorter:
declare -A possible_levels=( [-]=1 [+]=1 [0]=1 )
if [[ ${possible_levels[$value]} ]]; then
echo "valid level"
else
echo "invalid level"
fi
这篇关于巴什if语句来检查。如果字符串等几个字符串常量之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!