0

I have part of a script that i can't figure out. I want the script to check if the app is running. I set parameter to check for the app using a function. SO in my test i would like to see if the app is running it would return with a 0 and complete my IF statement, and if if the app is not running it would return with a 1 and tell me to check again. i'm not sure what is the best approach for something like that. Below is something i had in mind. Sorry for the confusion earlier

function appRunning {
    pgrep Dialog && return 0 || return 
}


if [ x$(appRunning) = '0' ] ; then 
    
    echo " yes app is running"
else 
   echo "no app is not running please check again 

fi 
...
exit 0

`

  • 3
    Put a valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) and paste your script at https://shellcheck.net for validation/recommendation. – Jetchisel Mar 02 '23 at 13:29
  • 3
    You don't have any command before the first `&&` in your `if` statement. `&&` is an infix operator, not a prefix operator. – chepner Mar 02 '23 at 13:36
  • 3
    Also, `&&` and `||` have *equal* precedence. Combining `&&` and `||` in the same list without explicitly grouping with `{...}` is unlikely to work the way you expect. `a && b || c && d` is equivlent to `(( a && b) || c) && d`, not `(a && b) || (c && d)`. – chepner Mar 02 '23 at 13:39
  • 2
    `&& return 0 || return 1` is completely useless; `command && return 1 || return 0` is better written `! command` – tripleee Mar 02 '23 at 13:55
  • 1
    `[ "$loggedInUser" != "_mbsetupuser" ] || [ "$loggedInUser" != "root" ]` will always be true; I'm guessing you meant `[ "$loggedInUser" != "_mbsetupuser" ] && [ "$loggedInUser" != "root" ]` - probably see also https://stackoverflow.com/questions/26337003/why-does-non-equality-check-of-one-variable-against-many-values-always-return-tr – tripleee Mar 02 '23 at 13:59
  • 2
    Stop thinking "true/false". Instead, think "succeed/fail". 0 is success, non-zero is failure. – William Pursell Mar 02 '23 at 14:17
  • what's the question/issue? are you getting an error message (if so, what is the error message)? is the logic not working as expected (if so, provide details)? – markp-fuso Mar 02 '23 at 18:28

1 Answers1

0

You are thinking too hard. How about:

if pgrep Dialog
then
    echo " yes app is running"
else
    echo "no app is not running please check again"
fi

If you insist making it into a function:

function appRunning {
    pgrep Dialog
}

if appRunning
then
    echo " yes app is running"
else
    echo "no app is not running please check again"
fi
Hai Vu
  • 37,849
  • 11
  • 66
  • 93