Results 1 to 4 of 4

Thread: Help for shell-script on android

  1. #1
    Join Date
    Sep 2013
    Location
    Illinois
    Beans
    30
    Distro
    Ubuntu

    Help for shell-script on android

    I am trying to make a yes or no shell-script but it doesn't work on my rooted android phone. Right now it works on xubuntu but not on android.
    Here's the script:

    clear
    echo Yes or no
    Read answer

    If [ $answer == yes ]; then
    Echo you chose yes

    elif [ $answer == no ]; then
    echo you chose no

    else
    echo please pick yes or no

    fi

    But after that I get:
    yn.sh[5]: syntax error: 'then' unexpected

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    #!/bin/sh

    What kind of error are you getting when you try to run it?

    There are several typos involving capitalization, and read can be done differently, but the main problem looks like it is missing what it should run (#!)

    Code:
    #!/bin/sh
    clear
    read -p "Yes or No: " answer
    
    if [ "yes" = "${answer}" ]; then
    echo you chose yes
    
    elif [ "no" = "${answer}" ]; then
    echo you chose no
    
    else
    echo please pick yes or no
    
    fi
    Note that /bin/sh is the more standard, portable dash not the tricked out non-standard bash.

    Edit: fixed string comparisons
    Last edited by Lars Noodén; September 13th, 2013 at 12:44 PM.

  3. #3
    Join Date
    Sep 2013
    Location
    Illinois
    Beans
    30
    Distro
    Ubuntu

    Re: Help for shell-script on android

    Now I get
    yn.sh[3] read: -p: no coprocess
    yn.sh[5] syntax error: 'then' unexpected

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    = not ==

    My mistake, I rushed through that. The test comparison for strings in the shell is = not == Also it should be wrapped in quotes. So it should be written like this:
    Code:
    "yes" = "${answer}"
    See these links for more details:

    http://mywiki.wooledge.org/BashPitfalls
    http://mywiki.wooledge.org/FullBashGuide

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
  •