shell脚本以检查文件中是否存在一行

发布于 2025-01-22 20:24:07 字数 518 浏览 2 评论 0原文

我尝试了堆栈溢出上可用的所有解决方案,但是当我使用条件时,始终会导致结果为true。 我需要在文件中找到一条线,看看它是否没有退出,然后在该文件中插入该行,但始终会导致该行已经存在。 这是我的脚本,

isInFile=$(grep -q '^export' /etc/bashrc)
if [[ $isInFile == 0 ]];
then
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
else
    echo "line is in the file";
fi

总是说

line is in the file

I have tried all the solutions available on stack overflow, but when I use if condition with with it always results true.
I need to find a line in the file and see if it doesn't exit then insert the line in that file, but it always results that the line already exists.
Here is my script

isInFile=$(grep -q '^export' /etc/bashrc)
if [[ $isInFile == 0 ]];
then
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
else
    echo "line is in the file";
fi

It always says that

line is in the file

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

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

发布评论

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

评论(2

最近可好 2025-01-29 20:24:07

如果语句分支基于退出状态它给定的[[只是您可以使用的一个命令,而不是强制性语法。在交互提示下,如果

执行此操作,请输入帮助:

if grep -q '^export' /etc/bashrc
then
    # exit status of grep is zero: the pattern DOES MATCH the file
    echo "line is in the file";
else
    # exit status of grep is non-zero: the pattern DOES NOT MATCH the file
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
fi

The if statement branches based on the exit status of the command it's given. [[ is just one command you can use, it's not mandatory syntax. At an interactive prompt, enter help if

Do this:

if grep -q '^export' /etc/bashrc
then
    # exit status of grep is zero: the pattern DOES MATCH the file
    echo "line is in the file";
else
    # exit status of grep is non-zero: the pattern DOES NOT MATCH the file
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
fi
战皆罪 2025-01-29 20:24:07

我在您的代码中看到2个问题:

  1. 如果[[$ isInfile == 0]]; - 如果条件不应使用;终止。删除。
  2. 您要检查的表达式始终是一个空字符串。尝试echo $ isInfile。您要检查的是命令的输出,而不是其返回值。相反,您应该从grep表达式中删除-Q,并检查输出是否为空。

以下代码应起作用:

isInFile=$(grep '^export' /etc/bashrc)
if [ -z "$isInFile" ]
then
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
else
    echo "line is in the file";
fi

-z检查可变的空虚。

I see 2 issues in your code:

  1. if [[ $isInFile == 0 ]]; --If condition should not terminate with ;. Remove that.
  2. The expression you are checking is always an empty string. Try echo $isInFile. What you are checking is output of the command, not its return value. Instead, you should remove -q from your grep expression and check if the output is empty or not.

Following code should work:

isInFile=$(grep '^export' /etc/bashrc)
if [ -z "$isInFile" ]
then
    echo "line is not present";
    echo "export PROMPT_COMMAND='RETRN_VAL=\$?;logger -p local6.debug \"\$(whoami) [\$\$]: \$(history 1 | sed \"s/^[ ]*[0-9]\+[ ]*//\" )\"'" >> /etc/bashrc;
    source /etc/bashrc;
else
    echo "line is in the file";
fi

-z check for emptiness of variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文