Find the answer to your Linux question:
Results 1 to 4 of 4
Code: flamingo:~ joliver$ cat test.sh #!/bin/sh case $UID in 0 ) echo "You are running as superuser, good." ;; [0-9] | [0-9][0-9] | [1-4][0-9][0-9] ) echo "Should you be running ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie jnojr's Avatar
    Join Date
    Sep 2007
    Location
    San Diego, CA
    Posts
    118

    Question Bash on OS X Lion


    Code:
    flamingo:~ joliver$ cat test.sh
    #!/bin/sh
    
    case $UID in
      0 ) echo "You are running as superuser, good." ;;
      [0-9] | [0-9][0-9] | [1-4][0-9][0-9] ) echo "Should you be running as UID < 500?" ;;
      * ) echo "Regular user, proceed?"
          while [ "$ANS" != [yY] ] || [ "$ANS" != [Nn] ]
          do
            read -n 1 -s ANS
            echo $ANS
            case $ANS in
              [yY] ) echo "OK, continuing!" ;;
              [nN] ) echo "OK, exiting"; exit 0 ;;
              * ) echo "Press Y or N please" ;;
            esac
          done ;;
    esac
    
    echo "This is the end!"
    flamingo:~ joliver$ bash -x ./test.sh
    + case $UID in
    + echo 'Regular user, proceed?'
    Regular user, proceed?
    + '[' '' '!=' '[yY]' ']'
    + read -n 1 -s ANS
    + echo y
    y
    + case $ANS in
    + echo 'OK, continuing!'
    OK, continuing!
    + '[' y '!=' '[yY]' ']'
    + read -n 1 -s ANS
    Why is bash telling me 'y' is not equal to 'y'?
    Last edited by jnojr; 10-17-2012 at 05:42 PM.

  2. #2
    Just Joined! ShadowCat8's Avatar
    Join Date
    Sep 2005
    Location
    San Gabriel Valley, CA
    Posts
    10
    Greetings,

    Well, from what you just posted there, it isn't telling you "'y' is not equal to 'y'"...

    It's telling you that "' y '" is not equal to "'[Yy]'".

    From what you posted, it looks like your read is padding $ANS with whitespace.

    HTH. Let us know.

  3. #3
    Linux Newbie jnojr's Avatar
    Join Date
    Sep 2007
    Location
    San Diego, CA
    Posts
    118
    Quote Originally Posted by ShadowCat8 View Post
    Greetings,

    Well, from what you just posted there, it isn't telling you "'y' is not equal to 'y'"...

    It's telling you that "' y '" is not equal to "'[Yy]'".

    From what you posted, it looks like your read is padding $ANS with whitespace.
    It does, doesn't it? Not sure why, as I did enquote the variable...

    Code:
    [ "$ANS" != [yY] ] || [ "$ANS" != [Nn] ]
    Removing the spaces around the variable screws up the logic for sure. And the exact same test works in another script.

  4. #4
    Linux Newbie jnojr's Avatar
    Join Date
    Sep 2007
    Location
    San Diego, CA
    Posts
    118
    I cheated a little:

    Code:
    case $UID in
      0 ) echo "You are running as superuser, good." ;;
      [0-9] | [0-9][0-9] | [1-4][0-9][0-9] ) echo "Should you be running as UID < 500?" ;;
      * ) echo "Regular user, proceed?"
          while true
          do
            read -n 1 -s ANS
            echo $ANS
            case $ANS in
              [yY] ) echo "OK, continuing!"; break ;;
              [nN] ) echo "OK, exiting"; exit 0 ;;
              * ) echo "Press Y or N please" ;;
            esac
          done ;;
    esac

Posting Permissions

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