rich.mac
rich.mac

Reputation: 3

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

Upvotes: 0

Views: 204

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

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

Upvotes: 1

Related Questions