Bash判断变量值是否在数组内的随记:修订间差异
来自三线的随记
(创建页面,内容为“=== 数组基本操作 === [root@router ~]# array=( > 123 > 456 > 789 > ) [root@router ~]# echo ${array[*]} 123 456 789 [root@router ~]# echo ${array[#]}…”) |
小无编辑摘要 |
||
第1行: | 第1行: | ||
=== 数组基本操作 === | ===数组基本操作=== | ||
[root@router ~]# array=( | [root@router ~]# array=( | ||
> 123 | > 123 | ||
第9行: | 第9行: | ||
123 456 789 | 123 456 789 | ||
[root@router ~]# echo ${array[ | [root@router ~]# echo ${array[@]} | ||
123 456 789 | 123 456 789 | ||
第16行: | 第16行: | ||
=== 判断变量是否在数组内 === | ===判断变量是否在数组内=== | ||
==== Method 1 ==== | ====Method 1==== | ||
echo "${array[@]}" | grep -wq "$var" && echo "Yes" || echo "No" | echo "${array[@]}" | grep -wq "$var" && echo "Yes" || echo "No" | ||
==== Method 2 ==== | ====Method 2==== | ||
for i in ${array[@]} | for i in ${array[@]} | ||
do | do | ||
第29行: | 第29行: | ||
<br /> | <br /> | ||
==== Method 3 ==== | ====Method 3==== | ||
[[ ${array[@]/${var}/} != ${array[@]} ]] && echo "Yes" || echo "No" | [[ ${array[@]/${var}/} != ${array[@]} ]] && echo "Yes" || echo "No" | ||
Question: diff ? | Question: diff ? |
2020年4月16日 (四) 12:47的版本
数组基本操作
[root@router ~]# array=( > 123 > 456 > 789 > ) [root@router ~]# echo ${array[*]} 123 456 789 [root@router ~]# echo ${array[@]} 123 456 789 [root@router ~]# echo ${#array} 3
判断变量是否在数组内
Method 1
echo "${array[@]}" | grep -wq "$var" && echo "Yes" || echo "No"
Method 2
for i in ${array[@]} do [ "$i" == "$var" ] && echo "yes" done
Method 3
[[ ${array[@]/${var}/} != ${array[@]} ]] && echo "Yes" || echo "No"
Question: diff ?
[[ ${array[@]/${var}} != ${array[@]} ]] && echo "Yes" || echo "No"
What is the principle ??