peterko
peterko

Reputation: 533

Return status of command returning data into variable

how can I get status from command, which is assigned into 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

Upvotes: 3

Views: 175

Answers (1)

MYMNeo
MYMNeo

Reputation: 836

How about this:

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

Upvotes: 2

Related Questions