Linux 为什么通配符在if子句bash中不起作用
下面是我在shell脚本中的代码 for i in "${status_array[@]}" do if [[ $i == "*start*" ]]; then echo "$i" echo "${service_array[$count]} $i " fi count=`expr $count + 1` isStarted="" done for j in "${status_array[@]}" do echo "$j" done 对于“${status_array[@]}”中的i 做 如果[[$i==“*开始*”];然后 回音“$i” echo“${service_数组[$count]}$i” fi count=`expr$count+1` isStarted=“” 完成 对于“${status_array[@]}”中的j 做 回声“$j” 完成Linux 为什么通配符在if子句bash中不起作用,linux,bash,shell,glob,Linux,Bash,Shell,Glob,下面是我在shell脚本中的代码 for i in "${status_array[@]}" do if [[ $i == "*start*" ]]; then echo "$i" echo "${service_array[$count]} $i " fi count=`expr $count + 1` isStarted="" done for j in "
在下面的j循环脚本中,$j很少显示stop.waiting值,很少显示start/running值。但是对于上面的循环,控制将转到if子句块。在这里,$i将每个值显示为start/running,那么为什么会出现这种情况。您应该将
“*start*”
更改为*“start”*
。如果在引号周围放置glob模式,它将以文本形式匹配。使用[[]]
时,不需要将字符串作为
不会对单词执行单词拆分和路径名扩展
在[[和]]之间;波浪形展开、参数和变量
展开、算术展开、命令替换、进程执行替换和删除报价 和引用禁用您的glob模式 解决方案 因此删除
[[]]
测试中的引号:
if[[$i==*start*];然后
回音“$i”
fi
测验
让我们看看bash对set-x
引用
set-x;i=起动器;[[$i==“*开始*”]&&echo“是”| | echo“否”;集合+x
+集合x
+i=起动器
+[[starter==\*\s\t\a\r\t\*]]
+回声号
不
查看所有内容,即使是*
也会转义
无报价
set-x;i=起动器;[[$i==*start*]&&echo“是”| | echo“否”;集合+x
+集合x
+i=起动器
+[[starter==*start*]]
+回声是的
对
呻吟
在旁注中,不要在bash中使用expr
((count++)
测试值是一个单词,他根本不需要引号。@ÉdouardLopez是的,它只是可选的。