Results 1 to 5 of 5

Thread: Read not allowing operators in shell scripts

  1. #1
    Join Date
    Jul 2009
    Beans
    3

    Question Read not allowing operators in shell scripts

    Hello!
    I'm trying to write a small application controlling spotify, running under wine, that I can run from my desktop computer and use via SSH on my mobile phone, and so far I've been quite successful. At least until just recently, when I tried to use the read command to read a single key-press on my phone, and use that input instead of having to type the number for item in the menu I've created.
    I tried to simply use the read command to silently listen to one key-press and output it to the variable opt. To do this I tried:

    read -s -n1 opt

    When I ran this in a shell-script this I got:

    read: 1: Illegal option -s

    Seeing this, I tried to remove the -s operator, which left me with:

    read: 1: Illegal option -n


    I decided to remove all operators and echo the variable opt, leaving me with:

    read opt
    echo "$opt"


    Which worked as expected. It echoed the key that I had pressed. I moved over to my terminal window and tried the read command from there, and it did exactly what it should. It silently recorded one key-press to the variable opt.

    So, now for the question; why isn't read working with operators in shell scripts? Am I missing something obvious here?

  2. #2
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Read not allowing operators in shell scripts

    Quote Originally Posted by Mastermoose View Post
    Am I missing something obvious here?
    Yes. It's a dash vs. bash issue. (Don't fret about; everyone makes this mistake. Once.)

    The standard shell, /bin/sh, points to the dash shell. The "read" syntax is different.

    See the dash(1) man page, and the bash(1) man page.

    Usage for dash: read [-p prompt] [-r] variable [...]
    Usage for bash: read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
    Last edited by gmargo; December 27th, 2010 at 01:26 AM.

  3. #3
    Join Date
    Jul 2009
    Beans
    3

    Re: Read not allowing operators in shell scripts

    Oh, thanks a lot. I ended up running the script with bash instead of sh, which did the trick.
    I'm not surprised it was as simple as that, although I can't say I don't feel quite stupid.

  4. #4
    Join Date
    Oct 2011
    Location
    Sacramento, California
    Beans
    40
    Distro
    Xubuntu 14.04 Trusty Tahr

    Talking Re: Read not allowing operators in shell scripts

    This answer has been very help full for me as well (being as beginner) not knowing why the syntax is wrong on multiple occasions. Thanks for the question as well as the answer (even after 2 years).


  5. #5
    Join Date
    Feb 2008
    Location
    Texas
    Beans
    14,845
    Distro
    Ubuntu 12.04 Precise Pangolin

Tags for this Thread

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
  •