Results 1 to 9 of 9

Thread: pls help, what's wrong with my shell script...

  1. #1
    Join Date
    Feb 2011
    Location
    PR.China
    Beans
    4
    Distro
    Ubuntu

    [Solved] pls help, what's wrong with my shell script...

    I read PaulLove and JoeMerlino's Book(<<Beginning Unix>>)
    and copied this script with vi below(1stscript.sh):

    Code:
    #!/bin/bash
    echo "GUESS THE SECRET COLOR"
    
    read COLOR
    if [ $COLOR="PURPLE" ]
    then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
    fi
    then i run it and got this result:
    $ chmod +x 1stscript.sh
    $ ./1stscript.sh
    GUESS THE SECRET COLOR
    ABC
    CORRECT!
    WHAT?!!!
    ....

    is there any thing wrong with my script.
    my platform is Ubuntu Server 64bit.

    pls help, thanx!
    Last edited by koori; February 1st, 2011 at 08:41 AM. Reason: The Problem was solved

  2. #2
    Join Date
    Jan 2007
    Location
    Location: Location:
    Beans
    1,246
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: pls help, what's wrong with my shell script...

    the if statement needs a ; and it's good practice to place your then at the end

    #!/bin/bash
    echo "GUESS THE SECRET COLOR"
    read COLOR
    if [ $COLOR="PURPLE" ] ; then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
    fi

    does that work?
    clear && echo paste url and press enter; read paste; (youtube-dl $paste) | zenity --progress --title="" --text "Downloading, please wait" --auto-close --pulsate && ans=$(zenity --file-selection); gnome-terminal -x mplayer "$ans"

  3. #3
    Join Date
    Feb 2011
    Location
    PR.China
    Beans
    4
    Distro
    Ubuntu

    Re: pls help, what's wrong with my shell script...

    doesn't work correctly, it returened the same result...
    is there any way to solve this problem?...

  4. #4
    Join Date
    Jan 2011
    Beans
    7
    Distro
    Ubuntu

    Re: pls help, what's wrong with my shell script...

    Quote Originally Posted by koori View Post
    I read PaulLove and JoeMerlino's Book(<<Beginning Unix>>)
    and copied this script with vi below(1stscript.sh):

    Code:
    #!/bin/bash
    echo "GUESS THE SECRET COLOR"
    
    read COLOR
    if [ $COLOR="PURPLE" ]
    then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
    fi
    then i run it and got this result:


    WHAT?!!!
    ....

    is there any thing wrong with my script.
    my platform is Ubuntu Server 64bit.

    pls help, thanx!

    what if you define the secret color first



    #!/bin/bash
    secret=PURPLE
    echo "GUESS THE SECRET COLOR"
    read COLOR
    if [ $COLOR = $secret ] ; then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $secret"
    fi

  5. #5
    Join Date
    Jan 2007
    Location
    Location: Location:
    Beans
    1,246
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: pls help, what's wrong with my shell script...

    *facepalm*

    I must be getting sleepy lol
    clear && echo paste url and press enter; read paste; (youtube-dl $paste) | zenity --progress --title="" --text "Downloading, please wait" --auto-close --pulsate && ans=$(zenity --file-selection); gnome-terminal -x mplayer "$ans"

  6. #6
    Join Date
    Feb 2011
    Location
    PR.China
    Beans
    4
    Distro
    Ubuntu

    Re: pls help, what's wrong with my shell script...

    Quote Originally Posted by clasificados View Post
    what if you define the secret color first



    #!/bin/bash
    secret=PURPLE
    echo "GUESS THE SECRET COLOR"
    read COLOR
    if [ $COLOR = $secret ] ; then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $secret"
    fi

    nothing has changed...
    well, does that mean my shell was broken?
    Attached Files Attached Files

  7. #7
    Join Date
    Jun 2006
    Beans
    2,930

    Re: pls help, what's wrong with my shell script...

    You just needed to put a space around the =:
    Code:
    #!/bin/bash
    echo "GUESS THE SECRET COLOR"
    
    read COLOR
    if [ $COLOR = "PURPLE" ]
    then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
    fi
    When doing comparisons, it is a good habit to use quotes around the variable:
    Code:
    if [ "$COLOR" = "PURPLE" ]
    Also why say that the secret color is $COLOR when $COLOR is the users response?
    Code:
    GUESS THE SECRET COLOR
    asdf
    INCORRECT!! THE SECRET COLOR IS asdf


    Also, there is a programming section in the forums:
    Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
    This would be a good place to get help with this sort of stuff.
    Last edited by lavinog; February 1st, 2011 at 07:38 AM.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  8. #8
    Join Date
    Feb 2011
    Location
    PR.China
    Beans
    4
    Distro
    Ubuntu

    Re: pls help, what's wrong with my shell script...

    Quote Originally Posted by lavinog View Post
    You just needed to put a space around the =:
    Code:
    #!/bin/bash
    echo "GUESS THE SECRET COLOR"
    
    read COLOR
    if [ $COLOR = "PURPLE" ]
    then
    echo "CORRECT!"
    else
    echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
    fi
    When doing comparisons, it is a good habit to use quotes around the variable:
    Code:
    if [ "$COLOR" = "PURPLE" ]
    Also why say that the secret color is $COLOR when $COLOR is the users response?
    Code:
    GUESS THE SECRET COLOR
    asdf
    INCORRECT!! THE SECRET COLOR IS asdf


    Also, there is a programming section in the forums:
    Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
    This would be a good place to get help with this sort of stuff.
    Thanx, the problem is solved and thank you for guiding me to the right place
    but there is still one strange thing happened.
    after i entered "./1stscript.sh" and enter an incorrect value it often return an error message below:

    $ ./1stscript.sh
    GUESS THE SECRET COLOR
    BLUE
    ./1stscript: line 7:[: =:] unary operator expected
    YOUR GUESS WAS WRONG. THE SECRET COLOR IS <PURPLE>.
    but if i output this message into file the error message was dissappeared:
    $ ./1stscript.sh > error.log
    GUESS THE SECRET COLOR
    YOUR GUESS WAS WRONG. THE SECRET COLOR IS <PURPLE>.
    why...okay maybe i could bring this question into that place.

    thank you

  9. #9
    Join Date
    Jun 2006
    Beans
    2,930

    Re: pls help, what's wrong with my shell script...

    Can you post the contents of the script you used?
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

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
  •