LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting issue (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-issue-923791/)

izual 01-14-2012 02:02 PM

shell scripting issue
 
hey guys i'm trying to make a script that will run option 1 or 2 as i saw some tut's on the net i figured
that this should work but i get an error with that ...

Code:

#!/bin/bash
 clear
echo "choose something ..."
select yn in "Yes" "No"; do
      case $yn in
            Yes ) xterm;;
            No ) echo "hello world";;
            esac
done

the error is :
line 4: syntax error near unexpected token `"Yes"'
line 4: `select yn "Yes" "No"; do'

ButterflyMelissa 01-14-2012 02:10 PM

Hi,

Let's look at this example:

Quote:

#!/bin/bash
OPTIONS="Hello Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo done
exit
elif [ "$opt" = "Hello" ]; then
echo Hello World
else
clear
echo bad option
fi
done
$opt is the variable that has the entered value as per list $OPTIONS, whereas in your script:

Quote:

select yn in "Yes" "No"
means two options...

Perhaps a look at the tutorial I consulted may help you...

Good luck

Thor

izual 01-14-2012 02:14 PM

thank you very much i will check it out right now :)

Dick Dastardly 01-14-2012 06:27 PM

I don't get any errors from the output of your code?


Is this any help?

Code:

#!/bin/bash
while true; do
    echo -e " 1)Yes\n 2)No"
    read -p "Choose something: " answer
    case $answer in
        1)
            echo "yes" ;;
        2)
            echo "no" ;;
        *)
            break  ;;
    esac
done


grail 01-14-2012 10:31 PM

I too copied your script and it seems to work without issue.

devUnix 01-15-2012 08:44 AM

Quote:

Originally Posted by izual (Post 4574561)
hey guys i'm trying to make a script that will run option 1 or 2 as i saw some tut's on the net i figured
that this should work but i get an error with that ...

Code:

#!/bin/bash
 clear
echo "choose something ..."
select yn in "Yes" "No"; do
      case $yn in
            Yes ) xterm;;
            No ) echo "hello world";;
            esac
done

the error is :
line 4: syntax error near unexpected token `"Yes"'
line 4: `select yn "Yes" "No"; do'

Well, I copied and executed your original script and it is working fine:

Code:

choose something ...
1) Yes
2) No
#? 1
./script.sh: line 6: xterm: command not found
#? 2
hello world
#? ^C
[demo@localhost Bash]$ cat script.sh
#!/bin/bash
 clear
echo "choose something ..."
select yn in "Yes" "No"; do
      case $yn in
            Yes ) xterm;;
            No ) echo "hello world";;
            esac
done
[demo@localhost Bash]$


Note: Did you copy and paste or type the script?

Try this:

Code:

dos2unix scriptName.sh

Refer to this document for details on dos2unix:

http://linux.about.com/od/commands/l...l1_dos2uni.htm

izual 01-15-2012 08:46 AM

i still dont get it why my first script was wrong as i understood from you guys it should work ... :\
Thors helped me out with this but i'm still got question marks above my head.. :\

vp0619520 01-15-2012 09:27 AM

Hi,I copied your script and it seems to work without problem。What is the editor you are using?

izual 01-15-2012 09:48 AM

tried emacs and vim...
got same result
used sh script.sh
or
bash script.sh

colucix 01-15-2012 09:53 AM

What is the output of
Code:

bash -x script.sh
? Could you copy/paste it from your terminal? Thanks.

izual 01-15-2012 10:38 AM

i'm trying 2 copy that using bash -x script.sh > scripterror.txt
but i get some "jeibrish" ( + the echo )
i got the clear command before echo but nothing special same error as before i can take out the 'clear' and make screen shot maybe u will see something i dont ...

colucix 01-15-2012 11:01 AM

It would be enough if you could copy and paste the whole output here (using CODE tags). The output of
Code:

od -c script.sh
would be useful as well, to see if there are some hidden characters that may trigger the error. Again, copy and paste the untouched terminal output would be the best.


All times are GMT -5. The time now is 04:23 AM.