-1

I am making a simple confirmation prompt, here is the code

read -r -p "Are You Sure? [Y/n] " input

case $input in
    [yY][eE][sS]|[yY])
        echo "Yes"
        ;;

    [nN][oO]|[nN])
        echo "No"
            ;;

    *)
    echo "Invalid input..."
    exit 1
    ;;
esac

But when I answer anything whether it is valid or not, it will show errors

Quang-Anhs-iPhone-3GS:~ root# /var/mobile/Testarea/confirm.sh
Are You Sure? [Y/n] y
/var/mobile/Testarea/confirm.sh: line 1: read: `input
': not a valid identifier
/var/mobile/Testarea/confirm.sh: line 2: 
: command not found
/var/mobile/Testarea/confirm.sh: line 3: syntax error near unexpected token `in
'
/var/mobile/Testarea/confirm.sh: line 3: `case $input in
'

So what am I doing wrong?

13
  • 3
    Windows line terminators?
    – choroba
    Jul 21, 2016 at 15:16
  • What? I'm doing on jailbroken iPhone Jul 21, 2016 at 15:17
  • How did you create the script? Can you add a hexdump of the first few lines?
    – choroba
    Jul 21, 2016 at 15:21
  • Can you provide more details? iOS also use UNIX so it will function like Terminal on Mac OS X Jul 21, 2016 at 15:22
  • 1
    I get the same error when I add Ctrl-M at the end of each line.
    – choroba
    Jul 21, 2016 at 15:47

3 Answers 3

0

Since I don't know what's wrong with iOS but I found an alternative. This alternative works like a selection menu

function yes {
    echo "Yes"
}

all_done=0
while (( !all_done )); do
    options=("Yes" "Exit")

    echo "Are you sure "
    COLUMNS=12
    select opt in "${options[@]}"; do
        case $REPLY in
            1) yes; break ;;
            2) all_done=1; break ;;
            *) echo "Invalid input..." ;;
        esac
    done
done

echo "Exiting"
0

It is more simple that you think, first guarantee you are using bash and just try this:

#!/bin/bash
read -r -p "Are You Sure? [Y/n] " input

input=$(echo "$input" | tr "[:lower:]" "[:upper:]")

case $input in
    Y*|S*|E*)
        echo "Yes"
        ;;

    N*|O*)
        echo "No"
            ;;

    *)
    echo "Invalid input..."
    exit 1
    ;;
esac
0

The problem is that the Mobile Terminal on cydia have no builtin function read, so this script couldn't be started.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .