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

标签 linux bash shell glob

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

相关文章:

linux - 由于在 linux shell 中执行程序,我得到 "dquote>"

Linux 删除转义字符

linux - Find 命令在 Linux/Cygwin 下查找某个日期之后创建的目录

java - 如何按列值分组到行和列标题中,然后动态对列求和

shell - KornShell 错误 0403-004 使用此命令指定参数

linux - Bash 脚本搜索和替换

linux - 返回字符串值 * 在调用返回时扩展

linux - 重定向 tail -f + grep/awk 的输出时发生了什么?

bash - subl在终端中工作,但在git中不是默认编辑器

linux - 如何在 200 台服务器上运行脚本并在堡垒实例终端中打印输出?我的 bash 脚本卡在 ssh 到远程服务器上