Home > other >  In bash file how to pass the error message
In bash file how to pass the error message

Time:10-13

My script

#!/bin/bash

set -eu

usrFile="${1}"

while read -r usrCMD
do exec -e "${usrCMD}" || echo_fun "${usrCMD}"
done < "$usrFile"

echo_fun is a function that prints the message.

In the echo_fun, I want to pass the error message of command exec -e "${usrCMD}"

Is this possible?

CodePudding user response:

Assign stdout to a variable, as explained in How to store standard error in a variable

Then pass that variable to echo_fun

while read -r usrCMD; do
    error=$(exec -e "$usrCMD" 2>&1 >/dev/null) || echo_fun "$usrCMD $error"
done < "$usrFile"
  • Related