检查 shell 脚本得到了什么样的参数

Check what kind of parameters shell script got

我的 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.

但我在每种情况下都得到 contains just letters

我也试过 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