3

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
7
  • If you want to get your current process id, you can use $$ --> echo $$. Regarding the question itself, your if condition checks an integer, so that you want to say if [ "$?" -eq 0 ]"... that is, use -eq to check equality in integers. Commented Jun 23, 2015 at 15:25
  • I don't understand the question here...do you want the output of the command, or the exit status, or both (or what)? Commented Jun 23, 2015 at 15:30
  • @fedorqui - Thanks for the comments relating to the comparison integer. But i also need in my script check if any command return bad status or no (for example somebody who run script are not allowed run other script...). Commented Jun 23, 2015 at 15:34
  • @TomFenech - I want the output of the command and the exit status (both) Commented Jun 23, 2015 at 15:35
  • 1
    You already have them in that case - the output is in $GETPID and the exit status is in $?. What's the problem? Commented Jun 23, 2015 at 15:37

1 Answer 1

2

How about this:

PID=($(pidof bash))
if [[ ${#PID[@]} -gt 0 ]]; then
    echo "status OK"
else
    echo "status not OK"
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.