STDIN管道文件进入for循环

时间:2016-09-01 11:51:09

标签: bash

我想知道有没有办法可以进入

./myscript.sh FILENAME

,文件将链接到

for a in $(cat FILENAME) ; do
done

2 个答案:

答案 0 :(得分:0)

$ cat > myscript.sh                        # create myscript.sh
for i in "$(cat $1)" ; do echo "$i" ; done # in the end CTRL-d
$ cat > babynames                          # create babynames
primo
secundo                                    # in the end CTRL-d
$ bash myscript.sh babynames               # execute the script with babynames as parameter
primo
secundo

答案 1 :(得分:0)

使用以下命令调用脚本:

./myscript.sh babynames 

您可以使用read处理文件的每一行:

while read -r line; do
  echo "$line"
done < "$1"