Returns the status of a command that returns data to a variable

how can I get the status from a command that is assigned to a variable?

For example:

#! /bin/bash

### GET PID
GETPID=$(ps aux | grep "bash" | grep -v "grep" | awk '{print $2 }')

if [ "$?" = "0" ]; then
    echo "status OK"
else 
    echo "status NOT OK"
fi

      

+3


source to share


1 answer


How about this:



PID=($(pidof bash))
if [[ ${#PID[@]} -gt 0 ]]; then
    echo "status OK"
else
    echo "status not OK"
fi

      

+1


source







All Articles