LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-28-2021, 06:09 PM   #1
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Rep: Reputation: Disabled
Bash Syntax


All,

Quick Q:

Have this code reading file listings from a source file:

Code:
i=0;
while read -r file; do
	nfil=${file:7}         # Eliminate 1st 7 chars "file://"
	if {grep -i '/etc/' <<< $nfil} then; continue; fi
	array[ $i ]="${nfil}"
	(( i++ ))
	echo "N=> $i F=> $nfil"
done < "${src_fil}"
The "grep" line is to eliminate any "/etc/" files from processing.

Always getting these 2 error lines but no HOWTO explains these errors:

Code:
script.sh: line 34: syntax error near unexpected token `fi'
script.sh: line 34: `      if grep -i '/etc/' <<< $nfil then; continue; fi'
So why does it think the "fi" which is mandatory, unexpected?

Do you know why or is it actually something else, which BASH lies about constantly?

Cheers!

TBNK
 
Old 03-28-2021, 06:18 PM   #2
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
All,

OK Bash was lying, it was the ";" after the "continue" cmd that it was barfing about and now it is just barfing about the "done" line, but really thinking the error is in one of the 2 lines before "done"!

Cheers!
 
Old 03-28-2021, 06:32 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
I’d use grep -vi instead. Wrap all the code in an if or while grep -vi .... Different styles for different folks.

And those aren’t “lies”, they are reports of syntax errors. All languages do that... not just bash. They won’t happen when the syntax is correct.
 
1 members found this post helpful.
Old 03-28-2021, 06:36 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by TBotNik View Post
Code:
i=0;
while read -r file; do
	nfil=${file:7}         # Eliminate 1st 7 chars "file://"
	if {grep -i '/etc/' <<< $nfil} then; continue; fi
	array[ $i ]="${nfil}"
	(( i++ ))
	echo "N=> $i F=> $nfil"
done < "${src_fil}"
The syntax error is in the use of curly braces. To be syntactically correct, this line would have to be
Code:
if { grep -i '/etc/' <<< $nfil; } then; continue; fi
The space after the first "{" is important. The space before the closing "}" is just for appearance.

But in reality, the curly braces are unnecessary.
Code:
if grep -iq '/etc/' <<< $nfil; then; continue; fi
Note that I have added the "-q" option to the grep command. I doubt that you want to see those lines displayed on the terminal.

Last edited by rknichols; 03-28-2021 at 06:39 PM.
 
2 members found this post helpful.
Old 03-28-2021, 07:56 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
This can also be done using the bash =~ binary operator rather than grep.
Code:
shopt -s nocasematch
i=0;
while read -r file; do
  nfil=${file:7}         # Eliminate 1st 7 chars "file://"
  if ! [[ $nfil =~ .*/etc/.* ]]; then
    array[ $i ]="${nfil}"
    (( i++ ))
    echo "N=> $i F=> $nfil"
  fi
done < "${src_fil}"
shopt -u nocasematch
 
1 members found this post helpful.
Old 03-29-2021, 01:30 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by TBotNik View Post
All,

OK Bash was lying, it was the ";" after the "continue" cmd
No, again no. bash is not lying, just you use incorrect syntax again and was not understood.
As it was already suggested please use shellcheck:
Code:
Line 5:
        if {grep -i '/etc/' <<< $nfil} then; continue; fi
        ^-- SC1009: The mentioned syntax error was in this if expression.
           ^-- SC1073: Couldn't parse this brace group. Fix to allow more checks.
            ^-- SC1054: You need a space after the '{'.
                                     ^-- SC1083: This } is literal. Check expression (missing ;/\n?) or quote it.
                                       ^-- SC1010: Use semicolon or linefeed before 'then' (or quote to make it literal).
                                                       ^-- SC1056: Expected a '}'. If you have one, try a ; or \n in front of it.
                                                         ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.
You have to know the language if you want to use it. Bash (and the computer in general) cannot lie, will just try understand the text file you wrote.
When you brake the rules [of the language] the computer will try the best (and guess), but obviously has no chance to know what was the initial wish.....

So again: if you think bash is lying you ought to check which rule did you ignore.
 
Old 03-29-2021, 01:46 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
All of the following is correct
Code:
if { grep -i '/etc/' <<< "$nfil"; }; then continue; fi
Code:
if grep -i '/etc/' <<< "$nfil"; then continue; fi
Code:
grep -i '/etc/' <<< "$nfil" && continue
Code:
[[ $nfil =~ /[eE][tT][cC]/ ]] && continue
Code:
[[ $nfil == */[eE][tT][cC]/* ]] && continue
 
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
httpd: Syntax error on line 210 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 iswarya Linux - Newbie 1 01-25-2012 01:28 PM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
[SOLVED] "Error: syntax before '@' token and Error: syntax at 'OTHER' token" bullrider Programming 2 07-27-2009 08:00 AM
Starting httpd: httpd: Syntax error on line 209 of /etc/httpd/conf/httpd.conf: Syntax sethukpathi Linux - Networking 6 04-12-2008 11:26 AM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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