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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:49:00  来源:igfitidea点击:

Use bash to read a file and then execute a command from the words extracted

linuxbashunixscriptingloops

提问by user191960

FILE:

文件:

hello
world

I would like to use a scripting language (BASH) to execute a command that reads each WORDin the FILEabove and then plugs it into a command.

我想使用脚本语言(BASH)以执行读取每个命令WORDFILE上述,然后将其插入到一个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 WORDfrom FILEabove

首先WORDFILE上面阅读

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 FILEabove.

到达上述结束时终止进程FILE



Result of BASH command onFILEabove:

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