Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Sebastian_Gxxx
Contributor

HowTo Error handling in bash

Hello,

How can I handle the error messages of an API command while I do pipe the output into a file ?

The following command will not work for me:

mgmt_cli login user ${USER} password ${PASS} > error_mesaage.txt

Using the following example I do not see the error message. I just see that an error happened but not why:

mgmt_cli show access-rulebase -s ${login_file} offset 0 limit 40 name "${POLICYNAME}${POLICYTYPE}" | grep -E -B2 "access-rule|access-section" > ${OUTPUT_FILE}

if [ $? -eq 1 ]; then

   echo

   echo "Error listing policy '${POLICYNAME}${POLICYTYPE}'. Aborting!"

   exit 1

fi

As I do grep and pipe the result of an API command, I do not see the error message at all. But I need to know what the error message is.

like :

code: "generic_err_object_not_found"
message: "Requested object [Policy1 Network] not found"

How can I get the error code and message in this case ?

How can I print the error code within the IF-statement ?

0 Kudos
1 Reply
Danny
Champion Champion
Champion

Just redirect stderr to stdout.

command 2>&1

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events