使用换行符分隔列表作为参数

use*_*028 4 linux command-line bash

有几种情况我有一些输出,例如

thing1
thing2
thing3
Run Code Online (Sandbox Code Playgroud)

但一直想做类似的事情:

*command* thing1
*command* thing2
*command* thing3
Run Code Online (Sandbox Code Playgroud)

一个例子是:假设我想杀死所有屏幕会话。有什么办法可以让我得到一个命令来使用类似的东西:

screen -ls
Run Code Online (Sandbox Code Playgroud)

这可能会导致

88.mine (detached)
22.mine (detached)
Run Code Online (Sandbox Code Playgroud)

我可以做一些会导致所有可能的命令的事情,例如:

screen -S 88.mine test -X
screen -S 88.mine test -X
Run Code Online (Sandbox Code Playgroud)

一举?

Chr*_*own 8

当然,只需使用read

while read -r line; do
    command "$line"
done < file
Run Code Online (Sandbox Code Playgroud)

在更具体的screen示例中,您需要删除剩余的文本:

while read -r session _; do
    screen -S "$session" test -X
done < file
Run Code Online (Sandbox Code Playgroud)