【问题标题】:Read 2 file in bash with loop用循环在bash中读取2个文件
【发布时间】:2016-12-17 14:05:38
【问题描述】:

我想在 userlist.txt 中创建 echo 用户名,如果用户名在 chk.txt 中,则检查主循环,并在继续主循环之前暂停 60 秒

filename1="/home/user/userlist.txt"
while read -r  file1
do
    filename2 = "/home/user/chk.txt"
    while read -r file2
    do
      text2 = $file2
      if $file2 == "username"
         pause 60
    done < "filename2"


    text="$file1"
    txt=($file1)
    name=${txt[0]}
    echo $name
    sleep 10

done < "$filename1"

很抱歉解释得很糟糕

【问题讨论】:

  • 但是 filename2 数据每次都会改变,我需要每次在主循环中重新加载 filename2
  • 在我看来你的脚本应该可以工作。有什么问题?

标签: bash loops unix


【解决方案1】:

你有一些基本的语法错误。

分配变量时,= 周围不能有空格。所以应该是:

filename2=/home/user/chk.txt
text2=$file2

if 中,您缺少用于执行测试的[ 命令以及thenfi 关键字。

if [ "$file2" = "username" ]
then
    sleep 60
fi

没有pause 命令,它是sleep(您稍后会在脚本中找到它)。

但是如果你只是想检查一个单词是否在文件中,你不需要循环,你可以使用grep 命令。

if grep -q username $file2
then
    sleep 60
fi

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2012-01-24
    • 2018-12-11
    • 2021-04-16
    相关资源
    最近更新 更多