我该如何使用“-” grep 中的字符?

发布于 2024-11-11 02:45:44 字数 966 浏览 1 评论 0原文

我希望使用 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

看起来 grep 不想搜索“-”字符...

有什么方法可以绕过这个吗?

谢谢!

i was hoping to use grep to check and see if permissions have been set correctly on a file...

I am aware that the below situation is probably not ideal & not as concise as it could be but i am after individual properties such as the owners name and the permissions...

I was going to use something like this:

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

it looks like grep does not want to search the "-" character...

any way of bypassing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海的爱人是光 2024-11-18 02:45:44

问题在于“-”用于指示标志。您可以通过使用 grep -e 强制将下一个参数解释为模式而不是标志来解决此问题。

The problem is that '-' is used to indicate flags. You can get around this by using the grep -e to force the next argument to be interpreted as a pattern rather than flags.

挽容 2024-11-18 02:45:44

您的问题不是连字符,而是其他几个语法错误:您需要回显数据才能将其输入 grep;你不需要用反斜杠转义连字符;并且您需要使用克拉 ^ 来锚定您的正则表达式(这也将避免其他人提到的连字符作为参数问题)。

这是一个开始:

if [ `echo "$MYPERMISSIONS" | grep "^-rwxr-xr-x" | wc -l` -eq 1 ]

Your problem is not the hyphen but several other syntax errors: you need to echo your data to get it into grep; you don't need to escape hyphens with backslashes; and you need to anchor your regular expression with a carat ^ (which will also avoid the hyphens-as-arguments problem mentioned by someone else).

Here's a start:

if [ `echo "$MYPERMISSIONS" | grep "^-rwxr-xr-x" | wc -l` -eq 1 ]
巨坚强 2024-11-18 02:45:44

尝试使用以下命令修改 grep 命令:

if [ ls -a | grep 'My File\.txt' | wc -l -eq 0 ]; then echo "My File.txt does NOT Exist" fi

Try modifying your grep commands with:

if [ ls -a | grep 'My File\.txt' | wc -l -eq 0 ]; then echo "My File.txt does NOT Exist" fi
﹏雨一样淡蓝的深情 2024-11-18 02:45:44

我想知道你的问题是不是文件名中有空格?例如,以下内容应该有效——

ls -l | grep "drwxr-xr-x" 

I wonder if your problem is the space in your file name? The following, for instance, should work --

ls -l | grep "drwxr-xr-x" 
心意如水 2024-11-18 02:45:44

该脚本有很多问题,到目前为止只指出了最明显的问题(您需要 echo $MYPERMISSIONS 将其通过管道传递给 grep,您不需要权限字符串中的多余转义符等,并且文件名中需要引号或转义空格)。还有一些更微妙的问题,主要是由于解析 ls 输出的危险。例如,假设您的桌面目录中有一个名为“Not My File.txt”的文件——初始的 ls | 。查询 | wc 将匹配它,并打印“My File.txt Exists”,即使它不存在(如果“My File.txt”和“My File.txt”都存在,事情会更奇怪)。其次,假设该文件存在并且由“notroot”拥有——脚本将认为它由 root 拥有,因为 ls 列表包含“root”。第三,假设它的组是“-rwxr-xr-x”(一个完全合法的组名,至少在某些系统上)...

相反,要测试文件是否存在,请使用测试命令的 -e原始。要检查所有权和权限,stat 会为您提供行为更好的输出。

最后,不要使用冗余的 if 命令(如果文件存在...后跟如果文件不存在...),而是在单个 else 子句上使用 else 子句。代码> if 命令。纠正所有这些(以及一些小的清理工作,例如将文件名放入变量中)后,我将如何重写脚本:

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

There are a number of things wrong with the script, and only the most obvious things have been pointed out so far (you need to echo $MYPERMISSIONS to pipe it to grep, you don't need the excess escapes and such in the permissions string, and you need quote or escape spaces in the filename). There are also some subtler issues, mostly due to the dangers of parsing ls's output. Suppose, for example, there were a file named "Not My File.txt" in your Desktop directory -- the initial ls | grep | wc will match that, and print "My File.txt Exists" even though it doesn't (things are even weirder if both that and "My File.txt" exist). Second, suppose the file exists and is owned by "notroot" -- the script will think it is owned by root, because the ls listing contains "root". Third, suppose its group is "-rwxr-xr-x" (a perfectly legal group name, at least on some systems)...

Instead, to test whether a file exists, use the test command's -e primitive. To check ownership and permissions, stat gives you much better-behaved output.

Finally, rather than using redundant if commands (if file exists... followed by if file doesn't exist...), use the else clause on a single if command. After correcting all of this (and a little minor cleanup like putting the filename in a variable), here's how I'd rewrite 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文