Bash脚本用于查找可执行文件的路径

时间:2014-05-07 02:55:25

标签: linux bash shell unix

编辑:我知道这是冗余的,它是家庭作业,我已经编写了我自己的代码,需要帮助解决问题>

如上所述,我必须编写一个BASH脚本来确定可执行文件是否在用户路径中。

这样如果你输入
    ./findcmd ping它返回/ bin / ping

我写了一些代码,但它没有正常工作,我希望有人可以帮我排除故障。当我键入./findcmd ping它只是返回我的文件不存在。(我尝试的任何其他文件,我知道存在。)

#!/bin/bash
#
# Invoke as ./findcmd command
#
# Check for argument
if [[ $# -ne 1 ]]
 then
    echo 'useage: ./findcmd command'
    exit 1
fi
#
# Check for one argument
if [[ $# -eq 1 ]]
 then
     pathlist=`echo $PATH | tr ':' ' '`
     for d in $pathlist;
          do
             if [[ ! -d $d || ! -x $d || ! -r $d ]]
               then
                   echo 'You do not have read end execute
                          permissions!'
                   exit 2
              fi
              if [[ $(find $d -name $1 -print | wc -l) -ne 0 ]]
                   then
                         echo 'The file does not exist in the PATH!'
                          exit 0

             fi
        done
fi

exit 0
#
#

2 个答案:

答案 0 :(得分:3)

无需使用bash数组,tr ':'' '在for循环中工作正常。

#!/bin/bash
#
# Invoke as ./findcmd command
#
# Check for argument
if [[ $# -ne 1 ]]
 then
    echo 'usage: ./findcmd command'
    exit 1
fi

f=$1

# No need to check the $# again, there's at least one arg and other will be ignored..
# Otherwise you can wrap this in a loop and keep shift'ing args and checking one by one
pathlist=`echo $PATH | tr ':' '\n'`
for d in $pathlist;
    do
      #echo command is your friend
      #echo "Checking for $f in $d"
      path="$d/$f"
      if [[ -f "$path" && -x "$path" ]]; then
         # PATH is not recursive, therefore no need to use find command
         # Simply checking that the file exists and is executable should be enough
         echo "Found $f at '$path'"
         # Note the same filename may be present farther down the PATH 
         # Once the first executable is found, exit
         exit 0
      fi
done

# Getting here means file was not found
echo "$f could not be found"
exit 1

结果如下:

rbanikaz@lightsaber:~$ ./which.sh grep
Found grep at '/usr/bin/grep'
rbanikaz@lightsaber:~$ ./which.sh foo
foo could not be found
rbanikaz@lightsaber:~$

答案 1 :(得分:-1)

which command已经这样做了......

从技术上讲,这是一个解决方案......

#!/bin/bash
which $1

我可能不会将其作为作业提交......

更新

稍微乱一点,我认为以下代码会让你过去当前的错误:

#!/bin/bash
#
# Invoke as ./findcmd command
#
# Check for argument
if [[ $# -ne 1 ]]
 then
    echo 'useage: ./findcmd command'
    exit 1
fi
#
# Check for one argument
if [[ $# -eq 1 ]]
 then
     d=$1
     pathlist=($(echo $PATH | tr ':' ' '))
     echo $pathlist

     i=0
    while read line; do
      a4[i++]=$line
    done < <(echo "$PATH" | tr ':' '\n')

    n=${#a4[@]}
    for ((i=0; i < n; i++)); do
       if [[ ! -d $d || ! -x $d || ! -r $d ]]
       then
          echo 'You do not have read end execute
                          permissions!'
          exit 2
       fi
       if [[ $(find $d -name $1 -print | wc -l) -ne 0 ]]
       then
          echo 'The file does not exist in the PATH!'
          exit 0

       fi
    done
fi

exit 0
#
#

相当多,它使用这个SO question中的解决方案将$ PATH变量拆分成一个数组,然后循环遍历它,应用你在while语句中的逻辑。