How to find a usable executable file in bash

后端 未结 2 1440
北恋
北恋 2021-01-28 05:44

I have written this little script to find executables that are passed as arguments e.g.

./testexec du ls md 

How do I get the script to not out

2条回答
  •  不要未来只要你来
    2021-01-28 05:55

    If you are using bash, you should use the builtin "type" rather than the external utility "which". The type command will return a non-zero exit status if the command is not found, which makes it easy to use with a conditional.

    for filename in "$@"; do
       if type -P "$filename" >/dev/null; then
           echo "found in PATH: $filename"
       fi
    done
    

提交回复
热议问题