LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-12-2013, 06:38 AM   #1
jhouston2010
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Rep: Reputation: Disabled
./menu.txt: line 1: syntax error near unexpected token 'new line'


Hi im a begginer on opensuse, i currently tried to make a menu script and i started by simply sorting the layout it worked once i first completed it but since then when i try to run it i get, ./menu.txt: line 1: syntax error near unexpected token 'new line'
Here is my script
#!/bin/bash
# Menu
while [ answer != "0" ]
do
clear
echo “Main menu”
echo "Select from the following functions"
echo "0 exit"
echo "1. Set File Directory"
echo "2. Text File Management"
read -p " ?" answer
case $answer in
0) break ;;
1) echo "Go up a directory"
echo “Go into a directory”
echo “Set working directory” ;;
2) echo “List (.txt) files”
echo “Create File”
echo “ Delete File” ;;
*) break ;;
esac
echo "press RETURN for menu"
read key
done
exit 0

I dont know how to copy it from opensuse so above was written in word, but that is exactly how i typed it into linux.
 
Old 03-12-2013, 07:09 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
1) Make sure your script has unix ( \n ) newlines and not, for example windows ( \r\n ) newlines. If your file is in windows format, you can convert it by for example

Code:
sed -i 's/\r$//' menu.txt
2) Don't use text processors (as opposed to text editors) to write scripts. Your script is full of fancy double quotes instead of the standard, correct ascii 0x22.

3) Use [code]...[/code] tags when posting code.
 
Old 03-12-2013, 07:10 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Problm is with while condition (marked in red), because it cannot compare answer string with 0:
Code:
#!/bin/bash 
while [ answer != "0" ] 
do 
......
.........
So do it like:
Code:
#!/bin/bash 
# Menu 
echo “Main menu”
echo "Select from the following functions [0) Exit; 1) Set File Directory; 2) Text File Management]: "; read -p answer
case $answer in 
0) break ;; 
1) echo "Go up a directory" 
echo “Go into a directory”
echo “Set working directory” ;;
2) echo “List (.txt) files”
echo “Create File”
echo “ Delete File” ;;
*) echo "Wrong choice entered. Exiting"
break ;; 
esac 
echo "press RETURN for menu" 
read key 
exit 0
 
Old 03-12-2013, 07:27 AM   #4
jhouston2010
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Sorry millgates is there any chance you could explain that in laymans terms to me haha. I am literally a complete begginer i dont know what you mean by \n new lines or \r\n newlines? Shivaa i tried the to code it how you said and it didnt work thanks any way.
Many regards Jhouston2010
 
Old 03-12-2013, 07:52 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
How did you create the script in the first place? In windows or in linux? With what program?
The problem is, the various OSes use diferent encoding for the end of the line.
In linux, the character with code 0x0A (decimal 10), also known as "LINE FEED" or LF, is used to denote the end of the line.
In windows world, on the other hand, a two byte sequence consisting of an ascii 0x0D (decimal 13) (CARRIAGE RETURN, or "CR"), followed by a LF is used instead. So, when you have a script with windows newlines, and you try to run it in linux, the shell will interpret the LF character as the end of the line, but the stray CR character preceding it will "remain there" and cause a syntax error.

Last edited by millgates; 03-12-2013 at 07:53 AM.
 
Old 03-12-2013, 08:04 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Can you invoke the script with set -xv and post it's output, so we can check if there's any problem:
Code:
#!/bin/bash 
set -xv
# Menu 
echo “Main menu”
echo "Select from the following functions [0) Exit; 1) Set File Directory; 2) Text File Management]: "; read -p answer
case $answer in 
0) break ;; 
1) echo "Go up a directory" 
echo “Go into a directory”
echo “Set working directory” ;;
2) echo “List (.txt) files”
echo “Create File”
echo “ Delete File” ;;
*) echo "Wrong choice entered. Exiting"
break ;; 
esac
echo "press RETURN for menu" 
read key 
exit 0
 
Old 03-12-2013, 08:04 AM   #7
jhouston2010
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I created the script in linux.
 
Old 03-12-2013, 08:30 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by shivaa View Post
Problm is with while condition (marked in red), because it cannot compare answer string with 0:
Why not? While there's obviously a missing '$' in $answer, and therefore the expression in the brackets will always be true, it will not cause a syntax error.

OP: This command should tell you whether the file is in unix format:
Code:
grep -q $'\r' menu.txt && echo windows || echo unix
 
Old 03-12-2013, 09:16 AM   #9
jhouston2010
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I typed in what you said and the answer was unix. :/
 
Old 03-12-2013, 09:36 AM   #10
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Can you upload the script exactly as it is? There is obviously some invisible problem that is only present in the original file but not in the code you posted, most likely some stray special character. When I copy the code from your original post and fix the double quotes, the code works as expected.
 
Old 03-12-2013, 01:56 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

bash has a useful select built-in designed specifically for making menus like this:

Code:
#!/bin/bash
# Menu

clear
echo “Main menu”
echo "Select from the following functions"

PS3='Enter a number: '
select answer in "Exit" "Set File Directory" "Text File Management"; do

    echo "You chose "answer"

    case $REPLY in

        1) echo "Exiting the menu"
           break
           ;;

        2) echo "Go up a directory"
           echo "Go into a directory"
           echo "Set working directory"
           ;;

        3) echo "List (.txt) files”
           echo "Create File"
           echo "Delete File"
           ;;

        *) echo "Unknown option.  Please choose again."
           continue
           ;;

    esac

done

echo "press RETURN for menu"
read key

exit 0
In addition, I suggest using functions to modularize the code in the options, particularly if you intend to have sub-menus, as you appear to be doing here. Each menu below the first should have its own function.

As has been explained, Dos/Windows and Unix use different character codes for line endings. If your script or data was created in a Windows-based program, then you'll probably have to convert it before you can use it. A Google search will give you tons of options for doing that.

The file command is perhaps the easiest way to tell which format it's in. If the output says "with CRLF line terminators", it's a dos-style file. Also "cat -A" will show you all non-printing characters in caret notation form. dos files will display a "^M" at the end of every line.


Finally, also beware that Word changes the regular quotes into "smart quotes". These will not be accepted by the script as syntax elements. Only ' and " can be used.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
line 23 :syntax error unexpected end of file detected nipuniitg Linux - Software 8 11-13-2011 02:12 PM
[SOLVED] Repeatedly getting command substitution: line 3: syntax error near unexpected token dontob Linux - General 8 08-31-2010 08:26 AM
line 313: syntax error: unexpected end of file bes Linux - Newbie 3 05-02-2010 03:32 AM
./script.sh: line 2: syntax error near unexpected token `(' n00balert Programming 3 03-11-2010 04:22 PM
syntax error at line 7 param unexpected kais1 Linux - Newbie 5 11-09-2009 07:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration