Why does it not work (Switch Case)?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does it not work (Switch Case)?
# 1  
Old 03-02-2015
Question Why does it not work (Switch Case)?

Hi there,
I'm trying to run a simple script,but i'm always getting this error

Code:
Syntax error: word unexpected (expecting "in")

I have searched many sites,like this,but i dont get it,it looks good so far,but wont work,maybe someone can help me.

Here is the Script
Code:
#!/bin/bash
while :
do
clear
echo " [1] - VNC4Server starten "
echo " [2] - VNC4Server stoppen "
echo " [5] - Beenden "
read yourch
case $yourch in
1)killall Xvnc4 ; vnc4server -geometry 1600x900 -depth 24 ; exit ;;
2)killall Xvnc4 ; exit ;;
5)exit 0 ;;
esac
done

Thank you
# 2  
Old 03-02-2015
Try quoting when using your variables (i.e. "$yourch").

Actually, I can't reproduce your error.

Must be a ^M Smilie

Code:
$ cat -v script

# 3  
Old 03-02-2015
Added ^M to test and got:

Code:
'/t1: line 9: syntax error near unexpected token `in
'/t1: line 9: `case $yourch in

Code:
$ cat -v t1
#!/bin/bash
while :
do
clear
echo " [1] - VNC4Server starten "
echo " [2] - VNC4Server stoppen "
echo " [5] - Beenden "
read yourch
case $yourch in^M
1)echo killall 1;;
2)echo killall 2;;
5)exit 0 ;;
esac
sleep 1
done

Suggestion: Remove ^M characters
# 4  
Old 03-02-2015
Got it,when i create/edit the script on WinSCP,it doesnt work,when i create it on Putty it works, is that what you mean with ^M ? I think WinSCP cannot transfer .sh by default, i thin i have too take a look at the settings.

Sorry for creating this useless post,thank you !
# 5  
Old 03-02-2015
Unix uses a single character (^J - linefeed) to represent a new line. Windows uses two characters (^M + ^J - carriage return + linefeed (CRLF)). When transferring a file from Windows to Unix, that was saved in Windows, the ^M (like a snotty hanky) - comes with it. As a rule, Unix admins find this to be mildly irritating.
# 6  
Old 03-05-2015
Thank you for explaining it.I have to take a look at the upload rules of winscp,normally it converts the files while transfering,but since 4-5 months it doesnt,dont know why.

Thank you for helping me ! This problem is really irritating and frustrating Smilie
# 7  
Old 03-05-2015
Quote:
Originally Posted by JustAnUser
Thank you for explaining it.I have to take a look at the upload rules of winscp,normally it converts the files while transfering,but since 4-5 months it doesnt,dont know why.
SCP almost never does this for you. FTP sometimes does, though (which often causes a lot more problems than it solves).

So I'm guessing you had WinSCP to actually use FTP at some point, then switched to SCP.

Really though, you should not be editing script files on windows. UNIX has many perfectly good text editors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

Error while executing switch case for find and replace specific file. Help Me...

case "$inputs" in sapte) find /pools/home_unix/sapte/work/models/model -name "*.xml" exec rm -i {} \;; ckm1) find /pools/home_unix/ckm1/work/models/model -name "*.xml" exec rm -i {} \;; I am getting error like as below. ./menu1.sh: line 144: syntax error near unexpected token `)'... (4 Replies)
Discussion started by: lathigara
4 Replies

3. Shell Programming and Scripting

Switch Case not working

My switch case is not working properly. When I press 6 or 'r' it prints out all the names of the project and the error message. #!/bin/bash while true; do read -p "Enter project number (1/2/3/10/20/30):" menu #grabs the informations of the project number and stores it into a menu... (1 Reply)
Discussion started by: Rapcher
1 Replies

4. UNIX for Advanced & Expert Users

need help in work case.

Hello, I need you help about the following case: - We have 2 remote site and both of the has very heavy app (Oracle,SAP...). -We need high availability though 2 IP (link aggregation) i.e 2 internet card for each server. *Hope this is understandable: What We want is: in case one of... (7 Replies)
Discussion started by: ahmedamer12
7 Replies

5. Shell Programming and Scripting

Something is wrong with this switch( case statement.

I started writing a script to save the files from a camera I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: while : do ... (2 Replies)
Discussion started by: snakemasterAK
2 Replies

6. Shell Programming and Scripting

Example of switch case in Bash

can anyone post a sample code for a switch case in shell (1 Reply)
Discussion started by: sumit the cool
1 Replies

7. Shell Programming and Scripting

Help needed in Switch Case

Hi Experts team, i wish to use switch case in unix. my req is case $code in 1111) echo "1111" 1112) echo "1112" *) echo "default" esac see eventhough for particular case 1111 , it is always execute the default statement *), if i code like this. How can handle... (2 Replies)
Discussion started by: spkandy
2 Replies

8. Shell Programming and Scripting

Help needed in switch case handling in UNIX

Hi, In below code, i am expecting the output has Bye Bye But i am getting has Bye Hi Code: #!/usr/bin/bash var="Hi" cat txt.txt | while read var1 do next="Bye" case $var in Hi) (1 Reply)
Discussion started by: Balamani
1 Replies

9. Shell Programming and Scripting

Ignore case sensitive in Case Switch

In a Case switch, how to ignore case sensitive in the test: e.g. case "$field" in "TEST) action1;; *) action2;; esac How to go in action1 in case of $field = TEST , or Test , or test or .... without enumerating all possibilities... Thanks,... (1 Reply)
Discussion started by: annelisa
1 Replies

10. UNIX for Dummies Questions & Answers

my case statement doesn't work..

CO UNixware 7.1.1 Hi friends, I have chopped my case statementt out of my .profile and put it in another script called setsid. The case statement works when run from my .profile but not from my setsid file. All that the script does is set an environmental variable based on user input.... (7 Replies)
Discussion started by: sureshy
7 Replies
Login or Register to Ask a Question