STDOUT & STDERR from previous Command as Arguments for next Command

时间:2017-06-12 16:53:19

标签: bash shell variables stdout stderr

Somehow I don't find a sufficient answer to my problem, only parts of hackarounds.

I'm calling a single "chained" shell command (from a Node app), that starts a long-running update process, which it's stdout/-err should be handed over, as arguments, to the second part of the shell command (another Node app that logs into a DB).

I'd like to do something like this:

updateCommand 2>$err 1>$out ; logDBCommand --log arg err out
  • Can't use > as it is only for files or file descriptors.
  • Also if I use shell variables (like error=$( { updateCommand | sed 's/Output/tmp/'; } 2>&1 ); logDBCommand --log arg \"${error}.\"), I can only have stdout or both mixed into one argument.
  • And I don't want to pipe, as the second command (logCommand) should run whether the first one succeeded or failed execution.
  • And I don't want to cache to file, cause honestly that's missing the point and introduce another asynchronous error vector
  • List item

1 个答案:

答案 0 :(得分:1)

#!/bin/bash中进行一些聊天后,有人建议只使用tpmsf(RAM中保存的文件系统),这是第二种最优雅(但唯一可能)的方法。因此,我可以使用>运算符,并在内存中的单独变量中使用stdoutstderr

command1 >/dev/shm/c1stdout 2>/dev/shm/c1stderr 
A=$(cat /dev/shm/c1sdtout) 
B=$(cat /dev/shm/c1stderr) 
command2 $A $B

(或更短):

A=$(command1 2>/dev/shm/c1stderr ) 
B=$(cat /dev/shm/c1stderr) 
command2 $A $B