Check if a file is executable

后端 未结 6 826
粉色の甜心
粉色の甜心 2020-11-27 16:11

I am wondering what\'s the easiest way to check if a program is executable with bash, without executing it ? It should at least check whether the file has execute rights, a

6条回答
  •  余生分开走
    2020-11-27 16:49

    To test whether a file itself has ACL_EXECUTE bit set in any of permission sets (user, group, others) regardless of where it resides, i. e. even on a tmpfs with noexec option, use stat -c '%A' to get the permission string and then check if it contains at least a single “x” letter:

    if [[ "$(stat -c '%A' 'my_exec_file')" == *'x'* ]] ; then
        echo 'Has executable permission for someone'
    fi
    

    The right-hand part of comparison may be modified to fit more specific cases, such as *x*x*x* to check whether all kinds of users should be able to execute the file when it is placed on a volume mounted with exec option.

提交回复
热议问题