bash 中的逻辑表达式
该脚本的目的是检查包含目录名称列表的文件,并将其附加到不存在的其他文件目录名称中。
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 if 表达式应该是:
不带方括号
编辑帖子时已编辑:
你的脚本应该是:
我所做的修改是:
在“for”中:你需要的内容$FILE 存档,而不是字符串“list.txt”($FILES 变量的值),因此您必须使用
cat
或“<”转储这些内容(即:while read f; do ... ; did <$FILES
)在 if 语句中:您必须评估命令。命令是 grep 。如果 grep 返回 0 那么你的“if”工作就完成了;任何其他返回值将跳过您的 if 语句。 (这就像对返回状态进行无用的检查,但效率更高)
your if expression should be:
without square brackets
EDITED as you edited your post:
Your script should be:
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)
[
是一个命令。如果您尝试使用grep
作为命令,则不要使用[
。[
is a command. If you're trying to usegrep
as the command, then don't use[
.