Results 1 to 7 of 7

Thread: string to be compared treated as command, why?

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Beans
    615

    string to be compared treated as command, why?

    when I run this script:

    Code:
    trythisvar=$(sudo ufw status verbose | tr -d -c [:alpha:])
    echo $trythisvar
    if "$trythisvar" = "some dumb string"
        then echo "The damn thing's alive!"
        else echo "No, it doesn't equal that."
    fi
    in a terminal I get this output:

    Code:
    me@me-dt:~$ /home/me/0_my_stuff/scripts/ufw-testing-UNFINISHED/trythis.sh 
    Statusinactive
    /home/me/0_my_stuff/scripts/ufw-testing-UNFINISHED/trythis.sh: line 3: Statusinactive: command not found
    No, it doesn't equal that.
    me@me-dt:~$
    which seems to indicate that in line 3 the expansion of trythisvar is treated as a command despite being enclosed by double quotes and appearing in the context of a string comparison. Would someone mind giving me a hint why this is so? Thanks for your time to read this, even if you are as puzzled as I am, which isn't likely.

  2. #2
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: string to be compared treated as command, why?

    ignore this, Im an idiot mixing up differing things, one sec Ill fix your script
    Last edited by HiImTye; March 19th, 2012 at 01:41 AM.

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

    Re: string to be compared treated as command, why?

    put condition inside square brackets

  4. #4
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: string to be compared treated as command, why?

    here you are:
    Code:
    #!/bin/bash
    trythisvar=$(sudo ufw status verbose | tr -d -c [:alpha:])
    if [ "$trythisvar" = "Statusinactive" ] ; then
      echo "UFW Inactive!"
    else
      echo $trythisvar
    fi
    sorry for fixing your indenting and style, it bothers me when my scripts all don't look pretty

    edit: made it actually functional
    Last edited by HiImTye; March 19th, 2012 at 01:50 AM.

  5. #5
    Join Date
    Sep 2011
    Beans
    615

    Re: string to be compared treated as command, why?

    Aha!
    Thank you, HiImTye & Vaphell!

    HiImTye, my indentions are merely the chaotic result of random cosmic rays and thermal noise in my brain so it's no loss.

  6. #6
    Join Date
    Sep 2011
    Beans
    615

    Re: string to be compared treated as command, why?

    For the benefit of those who might read this thread because they are looking for something similar:

    Note that the conditional is wrapped not just in square brackets but also by spaces. So it is bracket space condition space bracket, as in HiImTye's example. Just reading the code it is easy to not notice the spaces.

  7. #7
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: string to be compared treated as command, why?

    Hi

    In case $trythisvar is empty (trythisvar=$(sudo ufw status verbose | tr -d -c [:alpha:]) return an empty string for some reason)

    Code:
    if [ x"$trythisvar" = x"Statusinactive" ] ; then
    Otherwise you get (when interpreted by the shell)

    Code:
    if [  = "Statusinactive" ] ; then
    and this will error.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

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
  •