我想做什么:
如果存在没有" my_user"的任何文件或文件夹,请浏览某个基本文件夹的每个子文件夹。作为所有者。返回false
#!/bin/bash
checkOwnerShipOfFiles () {
files=$1
for f in $files; do
echo "$f"
if [ ! -d "$f" ]; then
owner=$(stat -c %U "$f")
echo "$owner"
if [ "$owner" != "my_user" ]; then
echo "Invalid owner: $owner of file $f"
return 255;
fi
else
check=$(checkOwnerShipOfFiles "$f/*")
echo "check: $check"
if [ "$check" = false ]; then false; fi;
fi
done
return 0
}
response=$(checkOwnerShipOfFiles "/test/base")
echo "response: $response"
if [ "$response" = "0" ];
then
echo "success result"
return 0;
else
echo "error result"
return 255;
fi
Shellcheck.net对此代码没有任何问题,并且运行正常。但不输出我的期望。变量检查似乎返回我用作输入的字符串,但我只返回0或255.检查HAS是否为其中一个值,不是吗?
我的具体问题是@Charles Duffey建议,我使用$(functionCall)返回该函数的echo,而我想要返回值。
最终工作的脚本:
#!/bin/bash
checkOwnerShipOfFiles () {
files=$1
for f in $files; do
if [ ! -d "$f" ]; then
owner=$(stat -c %U "$f")
if [ "$owner" != "my_user" ]; then
echo "Wrong user: $owner, is owner of: $f"
return 255;
fi;
else
if ! checkOwnerShipOfFiles "$f/*"; then
return 255;
fi;
fi
done
return 0
}
if checkOwnerShipOfFiles "/test/base";
then
return 0;
else
#echo "There exists at least 1 file that does not have the correct user permission";
return 255;
fi
答案 0 :(得分:0)
为什么重新发明轮子? {{1}}程序旨在检查目录树的所有权和权限。它遵循Unix哲学,即有一个工具可以做好一件事。