bash chmod编号条件
问题描述:
我发现如何从以下问题输出文件的数字chmod值。bash chmod编号条件
https://unix.stackexchange.com/questions/46915/get-the-chmod-numerical-value-for-a-file
我将如何检查,如果这个数字比在条件一定的值?
注:
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
答
的问题是,
stat -f "%OLp" $file
是您需要执行与结果比较的命令644
。
所以我们应该使用command substitutions在子shell中运行命令。
if [ $(stat -f "%OLp" $file) -gt 644 ]; then
- 的
$()
运行命令和与所述命令的输出替换它。
测试
$ if [ $(stat -f "%OLp" $file) -gt 644 ]; then
> echo Greater than 644;
> else
> echo Less than 644;
> fi
Less than 644
辉煌!谢谢你,有一个答案和代表。 –
接受的答复。但是,这是一个新帐户。看起来我不能upvote,直到我得到15代表:) –