How to find a usable executable file in bash

后端 未结 2 1410
北恋
北恋 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条回答
  • 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
    
    0 讨论(0)
  • 2021-01-28 06:08

    Just redirect the error message (coming from stderr) into /dev/null:

    which $filename 2>/dev/null
    
    0 讨论(0)
提交回复
热议问题