简体   繁体   English

Bash 脚本 - 检查文件是否包含特定行

[英]Bash script - Check if a file contains a specific line

I need to check if a file contains a specific line.我需要检查文件是否包含特定行。 This file is written continuously by someone, so I put the check inside a while loop.这个文件是有人连续写的,所以我把支票放在一个while循环中。

FILE="/Users/test/my.out"
STRING="MYNAME"
EXIT=1
while [ $EXIT -ne 0 ]; do 
    if [ -f $FILE ] ; then CHECK IF THE "STRING" IS IN THE FILE - IF YES echo "FOUND"; EXIT=0; fi
done

The file contains text and multiple lines.该文件包含文本和多行。

如果$FILE包含文件名并且$STRING包含要搜索的字符串,那么您可以使用以下命令显示文件是否匹配:

if [ ! -z $(grep "$STRING" "$FILE") ]; then echo "FOUND"; fi

Poll the file's modification time and grep for the string when it changes:轮询文件的修改时间和 grep 更改时的字符串:

while :; do
    a=$(stat -c%Y "$FILE") # GNU stat
    [ "$b" != "$a" ] && b="$a" && \
        grep -q "$STRING" "$FILE" && echo FOUND
    sleep 1
done

Note: BSD users should use stat -f%m注意:BSD 用户应该使用stat -f%m

Try:尝试:

while : ;do
    [[ -f "$FILE" ]] && grep -q "$STRING" "$FILE" && echo "FOUND" && break
done

This will loop continuously without waiting, you may want to add a waiting period (eg 5 seconds in this example):这将连续循环而无需等待,您可能需要添加一个等待时间(例如,在此示例中为 5 秒):

while : ;do
    [[ -f "$FILE" ]] && grep -q "$STRING" "$FILE" && echo "FOUND" && break
    sleep 5
done

A more rigorous check to test if a given line $STRING is contained in a file FILE would be更严格的检查来测试给定的$STRING 是否包含在文件 FILE 中

awk '$0 == "'${STRING}'"' $FILE

This in particular addresses my concerns with @fstab's answer (which also applies to all other previous answers): It checks the complete line and not just for substring presence within a line (as done with the grep solutions from above).这特别解决了我对@fstab 答案的担忧(这也适用于所有其他先前的答案):它检查完整的行,而不仅仅是一行中是否存在子字符串(如上面的 grep 解决方案所做的那样)。

The loop could be done as shown in the other answers.可以按照其他答案中所示完成循环。

I had to add ssh keys to the authorized_keys file from a script.我必须通过脚本将 ssh 密钥添加到authorized_keys文件中。 I've found two options (the bad and the ugly :)):我找到了两个选项(坏的和丑的:)):

for f in "$tmp/keys/"*; do
    line=`cat "$f"`
    dst_line=`fgrep "$line" "$home/.ssh/authorized_keys" || true`
    if [ "$line" != "$dst_line" ]; then
        echo "$line" >> "$home/.ssh/authorized_keys"
    fi
done
for f in "$tmp/keys/"*; do
    cat "$f" >> "$home/.ssh/authorized_keys"
done
cat "$home/.ssh/authorized_keys" | sort | uniq > "$home/.ssh/authorized_keys.new"
chmod 0600 "$home/.ssh/authorized_keys.new"
chown "$user:" "$home/.ssh/authorized_keys.new"
mv "$home"/.ssh/authorized_keys.new \
    "$home"/.ssh/authorized_keys

I thought with uniq it would be more concise, but it turned out that it's not.我以为使用uniq会更简洁,但事实证明并非如此。 And the need to adjust the permissions... And the order of the lines changes... Doesn't look good.并且需要调整权限……而且行的顺序发生了变化……看起来不太好。

Turning this comment into an answer since it seems to do what I expect (from what I understand of the original question): 将此评论转化为答案,因为它似乎符合我的预期(根据我对原始问题的理解):

cat <<EOF | grep -Fx "${search_term}"
hello
world
EOF
  • Searching for hello or world succeeds搜索helloworld成功
  • Searching for ello or worl fails搜索elloworl失败

From the grep's man:来自grep的人:

-q, --quiet, --silent
    Quiet; do not write anything to standard output.  Exit immediately with zero status if any match is found, even if an error was detected.  Also see the -s or --no-messages option.
-F, --fixed-strings
    Interpret PATTERNS as fixed strings, not regular expressions.
-x, --line-regexp
    Select only those matches that exactly match the whole line.  For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.
# Inline
grep -q -F "$STRING" "$FILE" && echo 'Found' || echo 'Not Found'
grep -q -F "$STRING" "$FILE" || echo 'Not Found'

# Multiline
if grep -q -F "$STRING" "$FILE"; then
  echo 'Found'
else
  echo 'Not Found'
fi

if ! grep -q -F "$STRING" "$FILE"; then
  echo 'Not Found'
fi

If you need to check the whole line use -x flag:如果您需要检查整行,请使用-x标志:

grep -q -x -F "$STRING" "$FILE" && echo 'Found' || echo 'Not Found'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM