./menu.txt: line 1: syntax error near unexpected token 'new line'
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
./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.
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.
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
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.