在bash脚本中逐行读取

时间:2023-01-14 19:27:07

I want to do the following, read line by line of a file and use the value per line as params

我要执行以下操作,逐行读取文件,并将每行值用作参数

FILE="cat test"
echo "$FILE" | \
while read CMD; do
echo $CMD
done

but when I do the echo $CMD, it just returns cat :S

但是当我使用echo $CMD时,它只返回cat:S

6 个解决方案

#1


57  

What you have is piping the text "cat test" into the loop.

您所拥有的是将文本“cat test”导入到循环中。

You just want:

你只是想要:

cat test | \
while read CMD; do
    echo $CMD
done

#2


90  

FILE=test

while read CMD; do
    echo "$CMD"
done < "$FILE"

There are some additional improvements that could be made (thanks @RanyAlbegWein):

还有一些可以改进的地方(感谢@RanyAlbegWein):

  • Add IFS= so that read won't trim leading and trailing whitespace from each line.
  • 添加IFS=使read不会减少每行的前导和后导空格。
  • Add -r to read to prevent from backslashes from being interpreted as escape sequences.
  • 添加-r以防止反斜杠被解释为转义序列。
  • Lower case CMD and FILE. The bash convention is only environmental and internal shell variables are uppercase.
  • 小写CMD和文件。bash约定仅是环境变量,而内部shell变量是大写的。
  • Use printf in place of echo which is safer if $cmd is a string like -n which echo would interpret as a flag.
  • 使用printf代替echo,如果$cmd是一个像-n的字符串,echo会把它解释为一个标志,那么它会更安全。

Result:

结果:

file=test

while IFS= read -r cmd; do
    printf '%s\n' "$cmd"
done < "$file"

#3


11  

xargs is the most flexible solution for splitting output into command arguments.

xargs是将输出分解为命令参数的最灵活的解决方案。

It is also very human readable and easy to use due to its simple parameterisation.

由于它的简单的参数化,它也非常易于阅读和使用。

Format is xargs -n $NUMLINES mycommand.

格式是xargs -n $NUMLINES mycommand。

For example, to echo each individual line in a file /tmp/tmp.txt you'd do:

例如,回显文件/tmp/tmp中的每一行。三种你会做的事:

cat /tmp/tmp.txt | xargs -n 1 echo

Or to diff each successive pair of files listed as lines in a file of the above name you'd do:

或者,为了将每一对列成行的文件分割成以上名称的文件,您应该这样做:

cat /tmp/tmp.txt | xargs -n 2 diff

The -n 2 instructs xargs to consume and pass as separate arguments two lines of what you've piped into it at a time.

- n2指示xargs使用并作为单独的参数传递两行每次导入的内容。

You can tailor xargs to split on delimiters besides carriage return/newline.

除了回车/换行外,还可以对分隔符进行分割。

Use man xargs and google to find out more about the power of this versatile utility.

使用man xargs和谷歌了解更多关于这个通用工具的功能。

#4


2  

If you want to use each of the lines of the file as command-line params for your application you can use the xargs command.

如果希望将文件中的每一行作为应用程序的命令行参数,可以使用xargs命令。

xargs -a <params_file> <command>

A params file with:

一个参数文件:

a
b
c
d

and the file tr.py:

和文件tr.py:

import sys
print sys.argv

The execution of

的执行

xargs -a params ./tr.py

gives the result:

给出了结果:

['./tr.py', 'a', 'b', 'c', 'd']

#5


1  

Do you mean to do:

你的意思是:

cat test | \
while read CMD; do
echo $CMD
done

#6


0  

The correct version of your script is as follows;

您的脚本的正确版本如下;

FILE="cat test"
$FILE | \
while read CMD; do
echo $CMD
done

However this kind of indirection --putting your command in a variable named FILE-- is unnecessary. Use one of the solutions already provided. I just wanted to point out your mistake.

然而,这种间接——将命令放入名为FILE的变量中——是不必要的。使用已经提供的解决方案之一。我只是想指出你的错误。

#1


57  

What you have is piping the text "cat test" into the loop.

您所拥有的是将文本“cat test”导入到循环中。

You just want:

你只是想要:

cat test | \
while read CMD; do
    echo $CMD
done

#2


90  

FILE=test

while read CMD; do
    echo "$CMD"
done < "$FILE"

There are some additional improvements that could be made (thanks @RanyAlbegWein):

还有一些可以改进的地方(感谢@RanyAlbegWein):

  • Add IFS= so that read won't trim leading and trailing whitespace from each line.
  • 添加IFS=使read不会减少每行的前导和后导空格。
  • Add -r to read to prevent from backslashes from being interpreted as escape sequences.
  • 添加-r以防止反斜杠被解释为转义序列。
  • Lower case CMD and FILE. The bash convention is only environmental and internal shell variables are uppercase.
  • 小写CMD和文件。bash约定仅是环境变量,而内部shell变量是大写的。
  • Use printf in place of echo which is safer if $cmd is a string like -n which echo would interpret as a flag.
  • 使用printf代替echo,如果$cmd是一个像-n的字符串,echo会把它解释为一个标志,那么它会更安全。

Result:

结果:

file=test

while IFS= read -r cmd; do
    printf '%s\n' "$cmd"
done < "$file"

#3


11  

xargs is the most flexible solution for splitting output into command arguments.

xargs是将输出分解为命令参数的最灵活的解决方案。

It is also very human readable and easy to use due to its simple parameterisation.

由于它的简单的参数化,它也非常易于阅读和使用。

Format is xargs -n $NUMLINES mycommand.

格式是xargs -n $NUMLINES mycommand。

For example, to echo each individual line in a file /tmp/tmp.txt you'd do:

例如,回显文件/tmp/tmp中的每一行。三种你会做的事:

cat /tmp/tmp.txt | xargs -n 1 echo

Or to diff each successive pair of files listed as lines in a file of the above name you'd do:

或者,为了将每一对列成行的文件分割成以上名称的文件,您应该这样做:

cat /tmp/tmp.txt | xargs -n 2 diff

The -n 2 instructs xargs to consume and pass as separate arguments two lines of what you've piped into it at a time.

- n2指示xargs使用并作为单独的参数传递两行每次导入的内容。

You can tailor xargs to split on delimiters besides carriage return/newline.

除了回车/换行外,还可以对分隔符进行分割。

Use man xargs and google to find out more about the power of this versatile utility.

使用man xargs和谷歌了解更多关于这个通用工具的功能。

#4


2  

If you want to use each of the lines of the file as command-line params for your application you can use the xargs command.

如果希望将文件中的每一行作为应用程序的命令行参数,可以使用xargs命令。

xargs -a <params_file> <command>

A params file with:

一个参数文件:

a
b
c
d

and the file tr.py:

和文件tr.py:

import sys
print sys.argv

The execution of

的执行

xargs -a params ./tr.py

gives the result:

给出了结果:

['./tr.py', 'a', 'b', 'c', 'd']

#5


1  

Do you mean to do:

你的意思是:

cat test | \
while read CMD; do
echo $CMD
done

#6


0  

The correct version of your script is as follows;

您的脚本的正确版本如下;

FILE="cat test"
$FILE | \
while read CMD; do
echo $CMD
done

However this kind of indirection --putting your command in a variable named FILE-- is unnecessary. Use one of the solutions already provided. I just wanted to point out your mistake.

然而,这种间接——将命令放入名为FILE的变量中——是不必要的。使用已经提供的解决方案之一。我只是想指出你的错误。