Bash Shell找出UNIX/Linux系统上是否存在命令($PATH)
时间:2020-01-09 10:46:04 来源:igfitidea点击:
如何从bash shell脚本中查找在UNIX上是否存在命令。
如果$PATH中不存在命令,那么如何在屏幕上显示错误消息。
如何确定Posix系统上是否存在命令?
您可以使用以下命令:
command
命令您可以执行一个简单命令或者使用名为command的命令显示有关命令的信息。对于所有Posix系统,建议使用此方法。type
命令推荐给bash用户。 -P选项强制对每个命令名称进行PATH搜索,即使它是别名,内置函数或者函数,也返回将要执行的磁盘文件的名称。
Posix命令示例
语法如下:
command -v command1 >/dev/null && echo "command1 Found In $PATH" || echo "command1 Not Found in $PATH"
#!/bin/bash # POSIX command lookup example CMDS="tar /usr/bin/mysqldump /path/to/other/command" for i in $CMDS do # command -v will return >0 when the $i is not found command -v $i >/dev/null && continue || { echo "$i command not found."; exit 1; } done # add rest of the script as we found all bins in $PATH echo "Starting backup...."
type -P命令示例
语法如下:
type -P command1 &>/dev/null && echo "Found" || echo "Not Found"
这是一个示例脚本:
#!/bin/bash # Use Bash builtin called type to determine whether a command exists or not # Use full path if possible... CMDS="tar /usr/bin/mysqldump ssh rsync" for i in $CMDS do type -P $i &>/dev/null && continue || { echo "$i command not found."; exit 1; } # Add rest of the script as we found all bins in $PATH echo "Starting backup...." /bin/tar ....
您也可以通过以下命令使用退出状态:
which tar1 &>/dev/null [ $? -eq 0 ] || echo "tar1 command not found."
出于安全原因,我强烈建议对所有系统管理员活动使用完整路径。
在此示例中,我在/usr/local/theitroad/redhat.paths.ini文件中定义了完整路径:
_path_php_cgi="/usr/bin/php-cgi" _path_chroot="/usr/sbin/chroot" _path_lighttpd="/usr/sbin/lighttpd" _path_mknod="/bin/mknod" _path_spawn_fcgi="/usr/bin/spawn-fcgi" _path_cp="/bin/cp" _path_mv="/bin/mv"
您可以使用以下代码来验证所有路径是否存在,然后再调用这些硬编码部分:
#!/bin/bash source /usr/local/theitroad/redhat.paths.ini # get those path vars paths=$(set | grep ^_path_*) # verify those paths for p in $paths do type -P ${p##*=} &>/dev/null || { echo "${p##*=} not found"; exit 1; } done # Alright, we got all binaries echo "Starting chroot() for ${_path_lighttpd}..."
这是使用-P类型的示例脚本:
#!/bin/bash RRDTOOL=/usr/bin/rrdtool OUTDIR=/var/www/html/lighttpd.rrd INFILE=/home/lighttpd/rrd OUTPRE=lighttpd-traffic WIDTH=400 HEIGHT=100 # make sure rrdtool exists; else die with an error message type -P $RRDTOOL &>/dev/null || { echo "$RRDTOOL not found. Set $RRDTOOL in ##代码##"; exit 1; } # make sure dir exits; else create it [ -d $OUTDIR ] || mkdir -p $OUTDIR # make sure input file exits; else die with an error message [ -f $INFILE ] || { echo "$INFILE input file not found. set $INFILE in ##代码##"; exit 2; } DISP="-v bytes --title TrafficWebserver \ DEF:binraw=$INFILE:InOctets:AVERAGE \ DEF:binmaxraw=$INFILE:InOctets:MAX \ DEF:binminraw=$INFILE:InOctets:MIN \ DEF:bout=$INFILE:OutOctets:AVERAGE \ DEF:boutmax=$INFILE:OutOctets:MAX \ DEF:boutmin=$INFILE:OutOctets:MIN \ CDEF:bin=binraw,-1,* \ CDEF:binmax=binmaxraw,-1,* \ CDEF:binmin=binminraw,-1,* \ CDEF:binminmax=binmaxraw,binminraw,- \ CDEF:boutminmax=boutmax,boutmin,- \ AREA:binmin#ffffff: \ STACK:binmax#f00000: \ LINE1:binmin#a0a0a0: \ LINE1:binmax#a0a0a0: \ LINE2:bin#efb71d:incoming \ GPRINT:bin:MIN:%.2lf \ GPRINT:bin:AVERAGE:%.2lf \ GPRINT:bin:MAX:%.2lf \ AREA:boutmin#ffffff: \ STACK:boutminmax#00f000: \ LINE1:boutmin#a0a0a0: \ LINE1:boutmax#a0a0a0: \ LINE2:bout#a0a735:outgoing \ GPRINT:bout:MIN:%.2lf \ GPRINT:bout:AVERAGE:%.2lf \ GPRINT:bout:MAX:%.2lf \ " $RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT OUTPRE=lighttpd-requests DISP="-v req --title RequestsperSecond -u 1 \ DEF:req=$INFILE:Requests:AVERAGE \ DEF:reqmax=$INFILE:Requests:MAX \ DEF:reqmin=$INFILE:Requests:MIN \ CDEF:reqminmax=reqmax,reqmin,- \ AREA:reqmin#ffffff: \ STACK:reqminmax#00f000: \ LINE1:reqmin#a0a0a0: \ LINE1:reqmax#a0a0a0: \ LINE2:req#00a735:requests" $RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT $RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT