Shell判断文件是否可在$PATH中找到并且可执行
2017-12-27 22:35
363 查看
#!/bin/bash # 判断一个文件是否是 可执行文件,且判断能否在PATH中找到,有三种结果 # (1) 找到且可执行 0 # (2) 找到且不可执行 1 # (3) 没找到 2 can_find=1 can_run=1 get_result() { res=$(expr $can_find + $can_run) echo $res return $res } in_path() { # 判断是否带了参数 if [ $# -eq 0 ]; then echo "usage : sh $0 fileName." echo "Error 01 : please input a fileName." r=$(get_result) #echo "$r" return $? fi # 判断文件是否在$PATH里找到 oldIFS=$IFS IFS=":" for each_path in $PATH do #echo "$each_path/$1" if [ -e $each_path/$1 ]; then can_find=0 #echo "$1 can find!" if [ -x $each_path/$1 ]; then can_run=0 echo "$each_path/$1" fi fi done IFS=$oldIFS r_chose=$(get_result) case "$r_chose" in 0 ) echo "success find." ;; 1 ) echo "can find,can't be running." ;; 2 ) echo "not found." ;; esac return $? } in_path $1
res=$(get_result) res接收的是echo $res,是get_result标准输出的东西,而不是get_result的返回值.
$? 上一个被调用函数的返回值
举例如下:
“sum=$(fsum 2 5)”这种方式,是将标准输出(echo
出来的东子)传递给主程序的变量,而不是返回值!
另外通过此题目,我也熟悉了case的语法:
Ruby代码
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
esac
Note
例子 使用case
Ruby代码
#!/bin/bash
# 测试字符串范围.
echo; echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[[:lower :]] ) echo "Lowercase letter" ;;
[[:upper :]] ) echo "Uppercase letter" ;;
[0-9] ) echo "Digit" ;;
* ) echo "Punctuation, whitespace, or other" ;;
esac # 允许字符串的范围出现在[中括号]中,
#+ 或者出现在POSIX风格的[[双中括号中.
exit 0
相关文章推荐
- shell 判断文件是否存在 并且 循环读取
- shell实例浅谈之一判断是可执行文件并存在PATH路径中
- shell实例浅谈之一判断是可执行文件并存在PATH路径中
- shell实例浅谈之判断是可执行文件并存在PATH路径中
- Shell脚本执行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell判断hdfs文件目录是否存在
- 程序中判断path中是否有某个文件
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Shell判断文件是否存在
- shell判断文件是否存在
- shell判断文件,目录是否存在或者具有权限
- shell判断hdfs文件目录是否存在
- 判断文件是否存在 shell 脚本
- shell bash使用,包括判断文件或文件夹是否存在举例
- shell判断文件,目录是否存在或者具有权限的代码
- 执行系统命令并且将输出写到指定日志文件的shell脚本(1)
- shell判断hdfs文件目录是否存在
- 如何在shell脚本中判断文件或者文件夹是否存在?
- Shell--判断文件是否存在
- shell判断文件,目录是否存在或者具有权限
- shell bash判断文件或文件夹是否存在