LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-08-2020, 04:04 AM   #1
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

Rep: Reputation: 2
Question Syntax error in Bash function 'Unexpected end of file'


Hello all,



I found this Bash-function for quickly creating a backup of a file here: https://unix.stackexchange.com/quest...to-backup-file

Put it in a separate file .bash_functions and sourcing it via my .bashrc


Code:
1 ##Functions
2
3 ##Create Backup of a File and append current Date and Time
4 bu () { 
5 for file in "$@" do
7        local new=${file}.$(date '+%Y-%m-%d_%H.%M.%S')
8       while [[ -f $new ]]; do
9            new+="~";
10        done
11       printf "copying '%s' to '%s'\n" "$file" "$new";
12        \cp -ip "$file" "$new";
13    done
14}
15
16 ##EOF

I have removed the semi-colons in line 5 and 10 ("$@"; do and done;) as apparently this does conform with today's Bash syntax any longer. My shell was complaining about a syntax error. I took this tutorial as a guide: https://www.cyberciti.biz/faq/bash-for-loop/

Now Bash complains about an unexpected end of file. As you can see, I am not an expert regarding Bash scripting.

Code:
bash: $'\r': Kommando nicht gefunden.
bash: /home/paisquy/.bash_functions: Zeile 17: Syntax Fehler: Unerwartetes Dateiende.
Thanks for your hints on how I can fix this issue and tell me what is wrong with the script. I find it bizarre that the shell is complaining about line 17 when there are only 16 lines in the file.
 
Old 03-08-2020, 05:44 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,818

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Before keywords like for do done there must be a newline or a semicolon.
Further, looking at your error message, you have inserted '\r' i.e. "carriage return" characters that happens if you edit it in Microsoft WinDOS. Remove these characters!
 
1 members found this post helpful.
Old 03-08-2020, 02:21 PM   #3
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

Original Poster
Rep: Reputation: 2
Lightbulb

Thanks very much for your hints.

For other viewers of this thread who might be looking for a solution to this problem, "carriage returns" can be easily removed with the following command

Code:
$ dos2unix FILE
To check whether there are any (invisible) carriage returns in a file type the following command in the VI editor, they will be displayed as $

Code:
:set list
 
Old 03-08-2020, 10:37 PM   #4
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by PasBern View Post
I have removed the semi-colons in line 5 and 10 ("$@"; do
The one in line 5 (before the "do") is needed and it being missing is the reason of your error message, the ones in lines 9 and 11 (at the end of the line) are not, they already have a newline after it.
But the keywords "do", "done" "then", "else", "fi" etc always need to be the first WORD on a line, or after a ; to concatenate two lines into one.

Quote:
Now Bash complains about an unexpected end of file.
That normally means it is missing some (reserved) keyword somewhere, in this case it didn't see the "do" in line 5. From the man page of bash
Quote:
for name [ [ in [ word ... ] ] ; ] do list ; done
so the (first) ; can only be omitted when NO "in" keyword is used.
Any ; can always be replaced by a newline, so when "do" or "done" are on its own lines, the preceding ; isn't needed anymore. like
Code:
for name [ in word ... ]
do list
done
Another quote from the man page
Quote:
Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command:

! case do done elif else esac fi for function if in select then until while { } time [[ ]]
So in a "for" the "do" keyword is only recognized when the third word, but there already is an "in" there.


PS: I always put the "do" and the "done" on the same indent level in those cases, so you can see the enclosed block.

Last edited by ehartman; 03-08-2020 at 10:49 PM.
 
1 members found this post helpful.
  


Reply

Tags
bash, syntax



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
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
Error in Bash: line 77: syntax error: unexpected end of file bribon Programming 8 07-13-2011 12:43 PM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 08:12 AM
Bash script -----------syntax error: unexpected end of file ArthurHuang Programming 2 05-01-2009 10:29 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 06:31 AM

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

All times are GMT -5. The time now is 01:15 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