我想测试我的bash
脚本中的参数是否为5位整数。
if [ "$1" == [0-9][0-9][0-9][0-9][0-9] ]; then
echo "first arg is 5 digits"
else
echo "not 5 digits"
这对我不起作用,有没有更简单的方法来做到这一点,例如使用expr
函数?
man bash
中的CONDITIONAL EXPRESSIONS
部分:if [[ "$1" =~ ^[0-9]{5}$ ]]; then
echo "first arg is 5 digits"
else
echo "not 5 digits"
fi
当测试这种类型的表达式时,您必须使用双括号。
case "$1" in
[0-9][0-9][0-9][0-9][0-9]) echo "first arg is 5 digits" ;;
*) echo "not 5 digits" ;;
esac
==
用于模式匹配。 - glenn jackman