shell笔记

1. 检测命令是否存在

if ! hash some_exec 2>/dev/null;then
     echo "'some_exec' was not found in PATH"
 fi
 ​
 if ! type some_exec >/dev/null;then
     echo "'some_exec' was not found in PATH"
 fi

不使用whichwhich不是内建命令,可能不同的平台实现不一样。 

2. 检测元素是否在数组里

 arr=(apple,banana,orange)
 exist=0
 for i in $arr;do
   if [[ $i == "orange"]];then
     exist=1
   fi
 done
 if [[ $exist == "1"]];then
   echo "exist"
 fi

  

 

posted @ 2020-01-08 11:08  闲云-野鹤  阅读(126)  评论(0编辑  收藏  举报