Linux 使用bash读取文件,然后从提取的单词中执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1605232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use bash to read a file and then execute a command from the words extracted
提问by user191960
FILE:
文件:
hello
world
I would like to use a scripting language (BASH) to execute a command that reads each WORD
in the FILE
above and then plugs it into a command
.
我想使用脚本语言(BASH)以执行读取每个命令WORD
在FILE
上述,然后将其插入到一个command
。
It then loops to the next word in the list (each word on new line).
It stops when it reaches the end of the FILE
.
然后循环到列表中的下一个单词(每个单词在新行)。当它到达FILE
.
Progression would be similar to this:
进展将与此类似:
Read first WORD
from FILE
above
首先WORD
从FILE
上面阅读
Plug word into command
将单词插入命令
command WORD > WORD
- which will output it to a text file; with word as the name of the file.
- 它将输出到一个文本文件;以 word 作为文件名。
Repeat this process, but with next to nth WORD
(each on a new line).
重复这个过程,但在 nth 旁边WORD
(每个都在一个新行上)。
Terminate process upon reaching the end of FILE
above.
到达上述结束时终止进程FILE
。
Result of BASH command onFILE
above:
FILE
上面BASH 命令的结果:
hello:
你好:
RESULT OF COMMAND UPON WORD hello
world:
世界:
RESULT OF COMMAND UPON WORD world
采纳答案by Rahul
You can use the "for" loop to do this. something like..
您可以使用“for”循环来执行此操作。就像是..
for WORD in `cat FILE`
do
echo $WORD
command $WORD > $WORD
done
回答by ghostdog74
normally i would ask what have you tried.
通常我会问你试过什么。
while read -r line
do
command ${line} > ${line}.txt
done< "file"
回答by Viswanadh
IFS=$'\n';for line in `cat FILEPATH`; do command ${line} > ${line}; done