LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2020, 08:50 AM   #1
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Rep: Reputation: Disabled
Script Code Error


Hi
I am learning Linux Shell Scripting from a book (titled: Mastering Shell Scripting, by: Andrew Mallett). On page 40, the author presented following example script (to evaluate students' grades):
Code:
#!/bin/bash
# Script to evaluate grades
# Usage: grade.sh student grade
# Author: @theurbanpenguin
# Date: 1/1/1971
if [ ! $# -eq2 ] ; then
echo "You must provide <student><grade>
exit 2
fi
case $2 in
[A-C]|[a-c]) echo "$1 is a star pupil"
;;
[Dd]) echo "$1 needs to try a little harder!"
;;
[E-F]|[e-f]) echo "$1 could do a lot better next year"
;;
*) echo "Grade could not be evaluated for $1"
esac
However, upon running the script by following command:
Code:
./grade.sh bob b
gives following error:
Code:
grade.sh: line 17: unexpected EOF while looking for matching `"'
grade.sh: line 19: syntax error: unexpected end of file
Can you guide me that whats wrong here with the code?
Thanks
 
Old 01-11-2020, 08:56 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by zetrotrack000 View Post
Hi
I am learning Linux Shell Scripting from a book (titled: Mastering Shell Scripting, by: Andrew Mallett). On page 40, the author presented following example script (to evaluate students' grades):
Code:
#!/bin/bash
# Script to evaluate grades
# Usage: grade.sh student grade
# Author: @theurbanpenguin
# Date: 1/1/1971
if [ ! $# -eq2 ] ; then
echo "You must provide <student><grade>
exit 2
fi
case $2 in
[A-C]|[a-c]) echo "$1 is a star pupil"
;;
[Dd]) echo "$1 needs to try a little harder!"
;;
[E-F]|[e-f]) echo "$1 could do a lot better next year"
;;
*) echo "Grade could not be evaluated for $1"
esac
However, upon running the script by following command:
Code:
./grade.sh bob b
gives following error:
Code:
grade.sh: line 17: unexpected EOF while looking for matching `"'
grade.sh: line 19: syntax error: unexpected end of file
Can you guide me that whats wrong here with the code? Thanks
Sorry, but I find this fairly ironic that you're reading a book on how to write shell scripts that has explanations, examples, etc....and ask us to figure it out what's wrong.

That said, I'd suggest you look at the error message, think about what it means, and then look *CAREFULLY* at what you keyed in. The error tells you what you're missing; this is part of the learning process for programming, and it is very, VERY frustrating. It is also valuable later on, since it'll make things like this much easier to fix in the future.
 
1 members found this post helpful.
Old 01-11-2020, 08:59 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
at first I would suggest you to use www.shellcheck.net.
that will immediately tell you:
Code:
Line 6:
if [ ! $# -eq2 ] ; then
             ^-- SC1035: You are missing a required space here.
but this is only just the first error.
Here you need to write
Code:
if [ ! $# -eq 2 ] ; then
 
1 members found this post helpful.
Old 01-11-2020, 09:07 AM   #4
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Sorry, but I find this fairly ironic that you're reading a book on how to write shell scripts that has explanations, examples, etc....and ask us to figure it out what's wrong.

That said, I'd suggest you look at the error message, think about what it means, and then look *CAREFULLY* at what you keyed in. The error tells you what you're missing; this is part of the learning process for programming, and it is very, VERY frustrating. It is also valuable later on, since it'll make things like this much easier to fix in the future.
The error is fixed, thanks to pan64.
The script contains 18 lines but the error is saying "Line 19". Isn't that strange?
 
Old 01-11-2020, 09:12 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
just try to understand the error messages you got:
Code:
grade.sh: line 17: unexpected EOF while looking for matching `"'  -> there is an unpaired "
grade.sh: line 19: syntax error: unexpected end of file           -> end of file and still not found the pair
by the way, if you want to say thanks jus click on yes
 
1 members found this post helpful.
Old 01-11-2020, 09:45 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
In addition to what was already been said, the following would also have helped:

Code:
bash -x grade.sh bob b
 
Old 01-11-2020, 01:30 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: You have an unfinished "string literal" in your program -- If you use a syntax-highlighting text-editor (such as mcedit), you can find it easily.
 
1 members found this post helpful.
Old 01-11-2020, 02:30 PM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by zetrotrack000 View Post

...

gives following error:
Code:
grade.sh: line 17: unexpected EOF while looking for matching `"'
grade.sh: line 19: syntax error: unexpected end of file
Can you guide me that whats wrong here with the code?
Thanks
Yes. When the shell gripes about not finding a quote, you should begin by looking at all the lines before the line number included in the error messge. What's happening is that the quote in "line 17" is being interpreted as the closing quote for a quote in a previous line. You've forgotten a quote somewhere above. Read your code carefully... I think it'll jump out at you.

I'd also look into using an editor that "understands" shell scripts and automagically indents your code (usually by pressing "Tab"). Vim might do this. Emacs (or one of its close relatives) certainly does this. Either will highlight variables and keywords in your script to help you distinguish between the two. You'll usually need to ensure that the editor knows what language it's dealing with. I do this by doing something like:
Code:
$ echo "#!/bin/bash" > my_new_script
$ emacs my_new_script
Once the editor sees the "shebang" line it knows to employ shell rules for highlighting and indentation. The same goes for other languages you might be using. Believe me, once you start using an editor like that... you'll never go back.

HTH...
 
Old 01-11-2020, 03:26 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by zetrotrack000 View Post
The error is fixed, thanks to pan64.
You mean the website that checked your code FOR YOU, rather than you learning/thinking about what was going on?
Quote:
The script contains 18 lines but the error is saying "Line 19". Isn't that strange?
No...it isn't, probably because you had a blank-line at the end of your script, or an unmatched pair somewhere. Again, referencing the book would teach you why this happens.

If you looked carefully at the error that clearly said you were missing one of (`"') those characters, and then looked carefully at line 7, you would have seen where you were missing one. Again...you would have learned to spot an error on your own, without a website telling you.
 
  


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
Parse error: parse error, unexpected '/' - can anyone see the error in this code? v@ny@ Programming 8 04-03-2010 04:07 PM
dpkg return error :post installation script return an error code (1) grimfold Debian 2 09-10-2009 01:55 PM
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 PM.

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