Search code examples
bashstdoutstderr

Redirect stdout / stderr to bash script


Apologize if this is a really simple question, but I couldn't find anything on google for "redirect stdout and stderr to bash script" and "redirect bash script output to another bash script".

I know this will redirect stdout and stderr to log.txt, but I'm not sure how to redirect it to a bash script.

java -jar build/libs/bot-kt-1.1.3.jar > log.txt 2>&1

Ideally something that behaves like above and is like so

java -jar build/libs/bot-kt-1.1.3.jar > "./script.sh" 2>&1 # script.sh uses the $1 argument for input

Solution

  • You can use xargs to move data from a pipe into an argument:

    java -jar build/libs/bot-kt-1.1.3.jar 2>&1 | xargs ./script.sh
    

    to pass everything as a single argument:

    java -jar build/libs/bot-kt-1.1.3.jar 2>&1 | xargs --null ./script.sh