需要简单的脚本/循环/命令来输入命令,在文本文件中执行和输出

假设我在文本文件中有如下命令列表( cat cmdlist.txt ): –

 cut lshw top awk sensors 

现在我想分别通过whatis cutwhatis lshw等获取关于该命令的简单信息,并在文本文件中打印出whatis 这些输出说cmdinfo.txt

所需的cmdinfo.txt输出( cat cmdinfo.txt ): –

 cut (1) - remove sections from each line of files lshw (1) - list hardware top (1) - display Linux processes awk (1) - pattern scanning and text processing language sensors (1) - print sensors information 

如何使用cmdinfo.txt中的命令输出whatis来实现cmdinfo.txt文件?

这只是示例 txt文件。

如果需要,建议简单的脚本。

尽可能简单:

 xargs whatis < cmdlist.txt > cmdinfo.txt 

更简单的一个,

 $ while read -r line; do whatis "$line"; done < cmdlist.txt > cmdinfo.txt cut (1) - remove sections from each line of files lshw (1) - list hardware top (1) - display Linux processes awk (1) - pattern scanning and text processing language sensors (1) - print sensors information 

使用以下命令将结果写入文件。

 while read -r line; do whatis "$line"; done < cmdlist.txt > cmdinfo.txt 

非常简单:

 for i in $(cat cmdlist.txt); do whatis $i ; done > cmdinfo.txt 

这循环遍历$(cat cmdlist.txt)输出中的每个条目( i $(cat cmdlist.txt) ,并在非常条目上运行whatis 。 输出示例:

 cut (1) - remove sections from each line of files lshw (1) - list hardware top (1) - display Linux processes awk (1) - pattern scanning and processing language sensors (1) - print sensors information 

注意 ,尽管在许多例子中你会发现i使用过,你不必使用它 – 你可以使用大多数普通的字母数字字符串 – 例如:

 for jnka127nsdn in $(cat input.txt); do whatis $jnka127nsdn ; done 

要在不解析cat情况下执行此操作:

 while read -ri ; do whatis $i ; done < cmdlist.txt 

像这样的东西:

 #! /bin/bash in_file=$1 out_file=$2 while read command do whatis $command done < $in_file >> $out_file 

我选择不让stderr离开,因为如果你在那里有一个没有whatis信息的命令,你应该知道这一点(如果需要的话,看看其他地方)。

我认为您可以使用以下命令获得正确的结果。

 $ for i in `cat cmdlist.txt`;do whatis $i 2>&1;done | sed "s,: nothing appropriate.,,g" > cmdinfo.txt 

事实上,

 $ for i in `cat cmdlist.txt`;do whatis $i 2>&1;done 

命令,第一个命令的一部分将显示如下输出。

 cut (1) - remove sections from each line of files lshw (1) - list hardware top (1) - display Linux tasks .: nothing appropriate. .: nothing appropriate. .: nothing appropriate. tr (1) - translate or delete characters 

你可以用whatis $(cat cmdlist.txt) ,但它的输出包括以下几行。

 .: nothing appropriate. .: nothing appropriate. .: nothing appropriate. 

上面的sed命令删除一些你不必要的输出行。

问题现在改变了。 如果cmdlist.txt所有cmdlist.txt可以从whatis列出,可以使用以下命令作为简单方法。

 whatis `cat 1.txt` 2>/dev/null > cmdinfo.txt 

如果你只需要从whatis完全列出的行,可以使用以下命令作为simpleway。

 whatis `cat 1.txt` > cmdinfo.txt 

但是你可以选择各种方式。