1

I'm doing some examples following this tutorial and I was curious to know if there is this kind of function in shell (var_dump PHP) because I Googled and found nothing.

For following these tutorials I'm trying to do something useful and have the following example:

pids=$(ps -C pidgin -o pid=)
if [ -z $pids ];
     then
         echo "have pids" $pids
     else
         echo "do not have pids" $pids
fi

But I cannot validate the variable 'pids' even with "-n or -z or ! $pids" always prints the number of pidgin pids even in if or else condition Running on terminal show this:

bash: [: 2480: binary operator expected
do not have pids 2480 20700

How can I property make this sentence work for this example?

  • 2
    I'm not sure where to begin, there is so much wrong here. First, your title seems to be about things that aren't even in the question. Second, you point to a tutorial about things that aren't even in the script you posted. You don't say which OS and shell you are using, so I guessed. Then the script seems to have a space that doesn't belong there (in the if). You also need quoting there to make a valid expression. Could you please edit the text to fix at least some of these, and also what you are expecting (rather than what [ was expecting), and maybe someone can help. – MAP Jul 15 '16 at 2:00
  • 2
    To at least make the script run properly, change the first line to pids="$(ps -C pidgin -o pid=)" and change the second line to if [ -z "$pids" ];. – Lawrence Velázquez Jul 15 '16 at 2:07
  • You may want to take a look at the Debugging Bash scripts section of the Bash Guide for Beginners. – user1737246 Jul 15 '16 at 3:05
  • this work too, and sorry about my English, google translate help me. – rafaelphp Jul 15 '16 at 3:23
  • @Lawrence Velázquez your first change (v="$(...)" instead of v=$(...)) makes no difference; it's purely cargo-cult. – pizdelect Jun 28 at 5:45
3

You should quote it like this:

if [ -z "$pids" ]

If you ever find yourself using a variable outside of quote marks, you're probably doing it wrong.

  • Thanks, this work, now I got the real result work, I understand how it works like, was my mistake. thanks. – rafaelphp Jul 15 '16 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.