我的 shell 脚本需要一些帮助。我有这个:
#!/bin/bash
for i in $*
do
if [[ $i = *[a-zA-Z] ]]
then echo $i contains just letters.
elif [[ $i = *[a-zA-Z0-9] ]]
then echo $i contains numbers and letters.
else
echo Error.
done
我希望结果是,例如:
$ ./script.sh abCd a9d a-b
abCd contains just letters.
a9d contains numbers and letters.
Error.
但是我在每种情况下都仅包含字母
。
我也尝试过 grep 命令,但没有成功。
最佳答案
你的正则表达式是错误的。请尝试以下操作:
#!/bin/bash
for i in $*
do
if [[ $i =~ ^[a-zA-Z]+$ ]]
then echo $i contains just letters.
elif [[ $i =~ ^[a-zA-Z0-9]+$ ]]
then echo $i contains numbers and letters.
else
echo Error.
fi
done
关于linux - 检查shell脚本得到什么样的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44800880/