script - , ( $MYPERMISSIONS, grep, escape- , ). , - ls output. , , "Not My File.txt" - ls | grep | wc "My File.txt Exists", ( , "My File.txt" ). -, , "notroot" - script , root, ls "root". -, , "-rwxr-xr-x" ( , , )...
, , , -e . , stat .
Finally, instead of using redundant commands if(if the file exists ... followed by if the file does not exist ...), use a sentence elsefor one command if. After fixing all of this (and a little minor cleanup, for example by putting the file name in a variable), here is how I rewrote the script:
cd ~/Desktop
filename="My File.txt"
if [ -e "$filename" ];
then
echo "My File.txt Exists"
if [ "$(stat -f "%Sp" "$filename")" = "-rwxr-xr-x" ]; then
echo "Permissions are correct for My File.txt"
else
echo "Permissions are NOT correct for My File.txt"
fi
if [ "$(stat -f "%Su" "$filename")" = "root" ]; then
echo "Owner is correct for My File.txt"
else
echo "Owner is NOT correct for My File.txt"
fi
else
echo "My File.txt does NOT Exist"
fi
source
share