简体   繁体   中英

bash chmod number condition

I found how to output the numeric chmod value of a file from the following question.

https://unix.stackexchange.com/questions/46915/get-the-chmod-numerical-value-for-a-file

How would I check if this number is greater than a certain value in a condition?

Note:

host:path user$ stat -f "%OLp" file
644

# !/bin/bash

file=/path/file

if [ stat -f "%OLp" $file -gt 644 ]; then
    echo Greater than 644
else
    echo Less than 644
fi

Syntax error: ./bash.sh: line x: [: too many arguments

The problem is that

stat -f "%OLp" $file

is a command you need to execute and compare the result with 644 .

So we should be using command substitutions to run the command in a subshell.

if [ $(stat -f "%OLp" $file) -gt 644 ]; then
  • The $( ) runs the command and replaces it with the output of the command.

Test

$ if [ $(stat -f "%OLp" $file) -gt 644 ]; then
>     echo Greater than 644;
> else
>     echo Less than 644; 
> fi
Less than 644

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM