bash 中的逻辑表达式

发布于 2024-11-04 22:26:51 字数 849 浏览 0 评论 0原文

该脚本的目的是检查包含目录名称列表的文件,并将其附加到不存在的其他文件目录名称中。

#!/bin/bash
FILES="list.txt"
2nd_file="2nd_list.txt"
for f in $FILES
do
    # if  backup dir exists, read next dir
if [ /bin/grep -q $f $2nd_file ]
then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no file exists
    echo "$f" >>$2nd_file
done

我在 if 表达式中收到错误。

我理解你的答案并更改了脚本:

 #!/bin/bash
 FILES="list.txt"
 2nd_file="2nd_list.txt"
 for f in $FILES
 do
        # if  backup dir exists, read next dir
 /bin/grep -q $f $2nd_file
 if [ $? -eq 0 ]
 then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no dir exists
     echo "$f" >>$2nd_file
done

但现在脚本不从文件

输出读取目录名称的一个小问题是:

 Skiping list.txt file...

The purpose of the script is go over file contains list of directory names and append to other file directory names that are not exist in.

#!/bin/bash
FILES="list.txt"
2nd_file="2nd_list.txt"
for f in $FILES
do
    # if  backup dir exists, read next dir
if [ /bin/grep -q $f $2nd_file ]
then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no file exists
    echo "$f" >>$2nd_file
done

I getting errors in if expression.

I understood what your answers about and changed the script :

 #!/bin/bash
 FILES="list.txt"
 2nd_file="2nd_list.txt"
 for f in $FILES
 do
        # if  backup dir exists, read next dir
 /bin/grep -q $f $2nd_file
 if [ $? -eq 0 ]
 then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no dir exists
     echo "$f" >>$2nd_file
done

But now a small problem that script doesn't read directory names from FILES

Output is :

 Skiping list.txt file...

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

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

发布评论

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

评论(2

他夏了夏天 2024-11-11 22:26:51

你的 if 表达式应该是:

if  /bin/grep -q $f $2nd_file

不带方括号

编辑帖子时已编辑:

你的脚本应该是:

#!/bin/bash
FILES="list.txt"
2nd_file="2nd_list.txt"
for f in $(cat $FILES) # see the modification
do
    # if  backup dir exists, read next dir
if /bin/grep -q $f $2nd_file # see the modification
then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no file exists
    echo "$f" >>$2nd_file
done

我所做的修改是:

  • 在“for”中:你需要的内容$FILE 存档,而不是字符串“list.txt”($FILES 变量的值),因此您必须使用 cat 或“<”转储这些内容(即:while read f; do ... ; did <$FILES

  • 在 if 语句中:您必须评估命令。命令是 grep 。如果 grep 返回 0 那么你的“if”工作就完成了;任何其他返回值将跳过您的 if 语句。 (这就像对返回状态进行无用的检查,但效率更高)

your if expression should be:

if  /bin/grep -q $f $2nd_file

without square brackets

EDITED as you edited your post:

Your script should be:

#!/bin/bash
FILES="list.txt"
2nd_file="2nd_list.txt"
for f in $(cat $FILES) # see the modification
do
    # if  backup dir exists, read next dir
if /bin/grep -q $f $2nd_file # see the modification
then
    echo "Skiping $f file..."
    continue  # read next file
fi
    # we are hear means no file exists
    echo "$f" >>$2nd_file
done

The modifications I've made are:

  • in the "for": you need the content of $FILE archive, not the string "list.txt" (the value of $FILES variable) so you have to dump those contents with cat or with a "<" (ie: while read f; do ... ; done < $FILES)

  • in the if statement: you have to evaluate a command. The command is you grep. If grep returns 0 then your "if" stuff will be done; any other return value will skip your if statement. (that's like your useless check of the return status but more efficient)

听你说爱我 2024-11-11 22:26:51

[ 是一个命令。如果您尝试使用 grep 作为命令,则不要使用 [

[ is a command. If you're trying to use grep as the command, then don't use [.

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