Home > Software engineering >  how to get the status of the last command along with its output in linux
how to get the status of the last command along with its output in linux

Time:08-14

i am trying to get the status of the last command also i want the output to be stored in logfile.

{spark_home}/bin/spark-submit .....> 2>&1 | tee -a log1.txt
if [$? -eq 0] ; then
echo " success"
else
echo "fail"
applicationId= $(grep command to get the app id from log1.txt)

but as $? is checking for last status command , its always showing as 0 ie successful as i am writing the output to logfile. can someone help me how to get the status as well as write logs of the spark-submit to log file

CodePudding user response:

When you use pipe with bash, you could read all command statue in PIPESTATUS array:

$ ls | grep spamandegg
$ echo ${PIPESTATUS[@]}
0 1

Here ls is Ok but grep token is not found.

The length of the array is equal to the number of commands in pipe sequence.

For you, the exit status of your spark-submit command is in ${PIPESTATUS[0]}

CodePudding user response:

In bash you can use a process substitution for that:

#!/bin/bash

if spark-submit ... > >(tee -a log1.txt) 2>&1
then
    echo "success"
else
    echo "fail"
fi
  • Related