【问题标题】:Clarification regarding bash shell script [closed]关于 bash shell 脚本的说明 [关闭]
【发布时间】:2013-10-21 14:40:48
【问题描述】:

谁能解释使用read i 的部分。 i 是从哪里来的。

scp -i ~/.ssh/id_rsa.sample gaara@stuid.student.com:ready/$2/*.zip ./$2 > slate.out 2>&1

ls -1 $2/* > curr.lst 2>/dev/null

while
read i
do
  if
    test -e ../done/"$i"
  then
      diff "$i" ../done/"$i" >/dev/null 2>&1
    if
      test $? -eq 0
    then
      rm "$i"
    fi
  fi
done < curr.lst

【问题讨论】:

  • 对此的简短回答是否。 有大量可用于 Bash、scp、diff 和该脚本的所有其他部分的文档。如果您对部分语法有具体的问题,请修改您的问题并这样说。
  • 我知道这有点要求,但我被严重卡住了。而使用read i 的部分我不知道i 是从哪里来的。
  • read 命令正在从您在最后指定的文件描述符中读取数据...

标签: bash shell


【解决方案1】:

read 的这种语法通常用于处理文件中的多行。让我们通过省略内部循环来简化事情:

while
read i
do
  # Process i
done < curr.lst

'while x do; done' 语法非常基本且易于理解,但添加 I/O 重定向可能会造成混淆。当您在done 之后添加&lt; curr.lst 时,这意味着“将此文件的内容用作stdin 作为条件。因此,如果您现在省略循环,您会得到:

read i < curr.lst

现在很明显read 正在从curr.lst 获取输入,并将变量i 设置为每一行的内容。因此,该代码块的基本含义是“将curr.lst 的每一行作为变量i 处理,并将代码放在循环内。

【讨论】:

    【解决方案2】:

    “read”,根据手册页(在 shell 中键入 man page),从文件描述符中读取。

    在您的代码中,循环是针对“curr.lst”中的每一行进行的,这些循环放在变量$i

    【讨论】:

    • 所以在最后一行 done &lt; curr.lst 。是 curr.lst 放置在 $i 中的那一行,如果我放置其他文件,将为新文件创建循环。
    • @Dark Matter - 这是一篇关于读取和读取文件的一些要点的有用文章,包括 read line &lt; file.txt 成语:catonmat.net/blog/bash-one-liners-explained-part-one
    • 来自@Darragh 的好文章!还有一些其他的解释:mywiki.wooledge.org/BashFAQ/001
    • man page 不会提供有关read 的任何有用信息。在大多数情况下,man read 也不会,除非通知用户它是 Bash 内置函数。 read 的更好的文档可以在 Bash Beginners GuideAdvanced Bash Guide 中找到。
    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2014-02-27
    • 2011-02-16
    相关资源
    最近更新 更多