Results 1 to 9 of 9

Thread: What is unary operator?

  1. #1
    Join Date
    Jan 2010
    Beans
    34

    What is unary operator?

    I tried to contact s1ightcrazed because I saw his ability to explain things simply, in his posts assign the exit status of a command to a variable. I failed to get in touch with him.

    I wrote a script that when run as daemon checks whether pppd is running, otherwise boots it, iteratively
    Copied in a file pppdchk.sh
    #!/bin/bash
    done=0
    while:
    do
    x=0
    x='pidof pppd'
    if [$x != "\n"]
    then
    sleep 1
    else
    pon dsl-provider
    sleep 1
    fi
    done

    The script runs, I get the result I want, but I get an output ...unary operator expected...
    Why?
    Last edited by bkpsusmitaa; July 20th, 2013 at 10:52 AM.

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: What is unary operator?

    I think 'while' wants something to evaluate (true or false). Try this script

    Code:
    tmp=hello;while [ "$tmp" != "q" ]; do echo "$tmp (q to quit)";read tmp;done
    If you want the script to run forever, use something that will always be true (and quit with ctrl c).

    Compare these two script lines:
    Code:
    tmp=hello;while false ; do echo "$tmp (q to quit)";read tmp;done
    which does nothing and
    Code:
    tmp=hello;while true ; do echo "$tmp (q to quit)";read tmp;done
    which continues until ctrl +c.

    Edit: See this link for a definition of unary operator/operation

    https://en.wikipedia.org/wiki/Unary_operation
    Last edited by sudodus; July 18th, 2013 at 07:43 AM.

  3. #3
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: What is unary operator?

    Quote Originally Posted by bkpsusmitaa View Post
    unary operator expected... Why?
    Because $x is either not defined or (in this case) contains an empty string. When possible, always protect vars with double quotes in bash. Example:
    Code:
    [ "$foo" != "" ] && echo "bar"
    Last edited by prodigy_; July 18th, 2013 at 08:47 AM.

  4. #4
    Join Date
    Jan 2010
    Beans
    34

    Re: What is unary operator?

    Quote Originally Posted by prodigy_ View Post
    When possible, always protect vars with double quotes in bash.
    I have tested with quotes. It ruins the script.
    Thanks, Sudodus, for the concepts of unary operator in unix and the while command in bash.
    Let us brainstorm further. I believe we will arrive at a solution soon.
    Thanks to all of you once again!

  5. #5
    Join Date
    Apr 2012
    Beans
    6,933

    Re: What is unary operator?

    1. if you want to evaluate pidof pppd and store the result, you need backticks (backquotes) not regular single quotes

    Code:
    x=`pidof pppd`
    or in bash you can use

    Code:
    x=$(pidof pppd)
    2. as well as quotes around the variables, you need space around the [ and ] i.e.

    Code:
    if [ "$x" != "\n" ]
    3. a better way to test for an empty return value might be to use the -z (zero length string) or -n (non-zero length string) tests e.g.

    Code:
    $ x=`pidof pppd`
    $ if [ -z "$x" ]; then echo "empty"; else echo "not empty"; fi
    empty
    $ 
    $ x=`pidof firefox`
    $ if [ -z "$x" ]; then echo "empty"; else echo "not empty"; fi
    not empty
    or

    Code:
    $ x=`pidof pppd`
    $ if [ -n "$x" ]; then echo "not empty"; else echo "empty"; fi
    empty
    $ 
    $ x=`pidof firefox`
    $ if [ -n "$x" ]; then echo "not empty"; else echo "empty"; fi
    not empty

  6. #6
    Join Date
    Jan 2010
    Beans
    34

    Re: What is unary operator?

    Quote Originally Posted by steeldriver View Post
    ... you need backticks (backquotes) not regular single quotes...
    I am sorry, I have indeed used backticks in the original script. I just typed wrong here.

    I have to say I have tried the double quote around the variable, and this makes the script useless! You could see for yourself.

    And for the rest of the script, Superb! Brilliant! Thanks.

    I was just reading the man page of bash. I hope I will have to have a printed version of it. Thanks for the help.

    Regards

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: What is unary operator?

    if double quoted variable breaks your script you are doing something wrong. Variables have no business dangling in the open unquoted, because that only buys you whitespace related errors.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Jan 2010
    Beans
    34

    Re: What is unary operator?

    Quote Originally Posted by Vaphell View Post
    if double quoted variable breaks your script you are doing something wrong...
    As I stated earlier, it reported about unary operator: "unary operator expected"
    If you don't have pressing work at hand could you look into this please? Script hat manipulates strings within it

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: What is unary operator?

    not seeing your true code but its version typed by hand, i can't say what you did wrong but you did something wrong.
    [ $x = 'something' ] when $x is whitespace only will expand to [ = 'something' ] and that's a broken condition - and it even might return unary operator expected as it sees = but has only 1 other param.
    [ "$x" = 'something' ] will expand to [ "<some whitespace>" = 'something' ] and that's legit
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •