[英]Redirecting standard input / output to substituted command
I am trying to run a command, which can be simplified to this structure: 我正在尝试运行一个命令,可以简化为这种结构:
echo "A,B" | cp $(sed -e 's/A,//') $(sed -e 's/,B//')
But I am struggling getting the input / output of the substituted commands right. 但我正在努力获得替代命令的输入/输出。 Running the code above yields this error message: sed: read error on stdin: Input/output error 运行上面的代码会产生以下错误消息: sed:stdin上的读取错误:输入/输出错误
When I run this instead: 当我运行这个时:
echo "A,B" | cp <(sed -e 's/A,//') <(sed -e 's/,B//')
the command doesn't terminate. 该命令不会终止。
What am I doing wrong here? 我在这做错了什么?
Both instances of sed
are waiting for input on their standard input, which is your terminal. sed
两个实例都在等待标准输入的输入,这是你的终端。 The pipe connects to the standard input of cp
, not to either of the sed
instances. 管道连接到cp
的标准输入,而不是连接到任何一个sed
实例。
Try this instead: 试试这个:
read first second <<< "A,B"
cp "${first}" "${second}"
Or, alternatively: 或者,或者:
var="A,B"
cp "${var%,*}" "${var#*,}"
Try this: 尝试这个:
echo "A,B" | (IN=$(cat); cp $(echo $IN | sed -e 's/A,//') $(echo $IN | sed -e 's/,B//'))
This creates a subshell and saves the stdin input to the $IN
variable. 这将创建一个子shell并将stdin输入保存到$IN
变量中。 Then it runs the 2 sed
commands on the input and uses the outputs for the arguments for cp
. 然后它在输入上运行2个sed
命令,并使用输出作为cp
的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.