尝试根据行是否以“!”开头创建条件。
注意:这不是重击
#!/bin/sh
if [ $line = "!*" ] ;
then
echo "$0: $line has !"
else
echo "$0: $line has no !"
fi
最佳答案
在POSIX test
中,=
仅执行精确的字符串比较。
请改用case
语句。
case $line in
"!"*) echo "$line starts with an exclamation mark" ;;
*) echo "$line does not start with an exclamation mark" ;;
esac
如果您真的想将其放在
if
中,则可以执行以下操作:if case $string in "!"*) true;; *) false;; esac; then
echo "$line starts with an exclamation mark"
else
echo "$line does not start with an exclamation mark"
fi
关于sh - 在POSIX sh中检查字符串是否以 "!"开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48913826/