Bash遍历每行并使用每行的第一个单词和第二个单词运行命令 [英] Bash iterate over each line and run a command utilizing the first word and second word of each line
本文介绍了Bash遍历每行并使用每行的第一个单词和第二个单词运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个解析文件的函数,并将其分解为存储在变量中的以下输出
I have a function that parses a file and got it down to the following output which is stored in a variable
option1 value1
option2 value2
option3 value3
etc...
我需要遍历每行并在每行上运行使用两个值的命令.以下是我的想法
I need to loop through each line and run a command that uses both values on each line. Something like the following is what I'm thinking
for each line in "$var"; do
command "$first_word $second_word"
done
我该怎么做?
推荐答案
每 BashFAQ#1 ,而边读
循环则是适合该作业的工具:
Per BashFAQ #1, a while read
loop is the appropriate tool for the job:
while read -r first_word second_word rest; do
your_command "$first_word" "$second_word"
done <<<"$var"
请注意 rest
的使用-具有占位符( _
也很常见)意味着第二个之后的单词也不会放置在 second_word中
变量.
Note the use of rest
-- having a placeholder (_
is also common) means that words after the second one are not also placed in the second_word
variable.
这篇关于Bash遍历每行并使用每行的第一个单词和第二个单词运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文