相关疑难解决方法(0)

条件管道

假设我有以下管道:

cmd1 < input.txt |\
cmd2 |\
cmd4 |\
cmd5 |\
cmd6 |\
(...) |\
cmdN > result.txt
Run Code Online (Sandbox Code Playgroud)

在某些条件下,我想cmd3cmd2和之间添加一个cmd4。有没有办法创建一种条件管道而不将结果保存cmd2到临时文件中?我会想到这样的事情:

cmd1 < input.txt |\
cmd2 |\
(${DEFINED}? cmd3 : cat ) |\
cmd4 |\
cmd5 |\
cmd6 |\
(...) |\
cmdN > result.txt
Run Code Online (Sandbox Code Playgroud)

shell pipe shell-script

51
推荐指数
6
解决办法
2万
查看次数

有条件地在 bash 脚本中包含管道阶段

我有一个像下面这样的脚本:

flag=false
# Do a bunch of stuff that might set flag to true.
if [[ "$flag" == "true" ]]; then
   command \
       | pipe_command_a \
       | pipe_command_b \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
else
   command \
       | pipe_command_a \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
fi
Run Code Online (Sandbox Code Playgroud)

唯一的区别flagtruefalse品牌是pipe_command_b可能无法运行。有没有办法折叠它,这样我就不必重复所有常见的东西?

bash shell-script

5
推荐指数
1
解决办法
1334
查看次数

标签 统计

shell-script ×2

bash ×1

pipe ×1

shell ×1