我如何在grep中使用" - "字符?

Mat*_*tus 3 bash grep

我希望使用grep检查并查看是否已在文件上正确设置权限...

我知道下面的情况可能并不理想,并不尽可能简洁,但我追求的是个人财产,如所有者名称和权限......

我打算用这样的东西:

cd ~/Desktop

if [ `ls -a | grep My File.txt | wc -l` -eq 1 ];
then
echo "My File.txt Exists"

    MYPERMISSIONS=`ls -l My File.txt`

    if [ `$MYPERMISSIONS | grep $"\-rwxr\-xr\-x" | wc -l` -eq 1 ];
        then
        echo "Permissions are correct for My File.txt"
    fi

    if [ `$MYPERMISSIONS | grep $"\-rwxr\-xr\-x" | wc -l` -eq 0 ];
        then
        echo "Permissions are NOT correct for My File.txt"
    fi

    if [ `$MYPERMISSIONS | grep root | wc -l` -eq 1 ];
    then
        echo "Owner is correct for My File.txt"
    fi

    if [ `$MYPERMISSIONS | grep root | wc -l` -eq 0 ];
    then
        echo "Owner is NOT correct for My File.txt"
    fi
fi

if [ `ls -a | grep My File.txt | wc -l` -eq 0 ];
then
echo "My File.txt does NOT Exist"
fi
Run Code Online (Sandbox Code Playgroud)

貌似grep不想搜索" - "字符......

有什么办法绕过这个?

谢谢!

kar*_*aze 5

问题是' - '用于表示标志.你可以通过使用grep -e强制将下一个参数解释为模式而不是标志来解决这个问题.