有没有办法将其反转,以便先检查它是否无效?
if expr "$string" : '-\?[0-9]\+$' >/dev/null
then
echo "String is a valid integer."
else
echo "String is not a valid integer."
fi
Run Code Online (Sandbox Code Playgroud)
是的,我同意@nobody和@BenJackson,你只需要添加'logical NOT'运算符,即
if ! expr "$string" : '-\?[0-9]\+$' >/dev/null ; then
#--^ right there ;-)
echo "String is not a valid integer."
else
echo "String is a valid integer."
fi
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.
PS,因为您似乎是一个新用户,如果您得到一个帮助您的答案,请记住将其标记为已接受,和/或给它+(或 - )作为有用的答案.