LinuxQuestions.org
Visit Jeremy's Blog.
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 06-23-2011, 12:49 PM   #1
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Rep: Reputation: Disabled
Help needed on a script


Hey guys,

I have a problem with a script that I wrote for a one of my classes.

Line 240: unexpected EOF while looking for mathcing '"'
Line 245: syntax error: unexpected end of file

The script is suppose to show that a couple of SQL statements work without the use of extern databases (mysql and stuff).

The script is based on adding/editing of Playboy bunnys. The bunny's name, haircolor and length are the database tables.

#!/bin/bash

PlayboyNaam=/home/ivi/farama02/Playboy/naam
PlayboyHaarkleur=/home/ivi/farama02/Playboy/haarkleur
PlayboyLengte=/home/ivi/farama02/Playboy/lengte

function Startscherm() {
echo $(tput setaf 3) "

Welkom bij Playboy, maak u keuze:
1 - Insert
2 - Select
3 - Update
t - Toon alles

Typ het getal van keuze in a.u.b.: "$(tput sgr0)
read choice

case "$choice" in
"1")
Insert
;;
"2")
Select
;;
"3")
Update
;;
"t")
Toon
;;
esac
}

function Insert() {
echo "Welkom bij het Insert, typ a.u.b. een naam van een Playboy bunny in: "
read naamInsert
echo "Typ het haarkleur in a.u.b. : "
read haarkleurInsert
echo "Typ a.u.b. het lengte in: "
read lengteInsert
echo "Gegevens:"
echo "Naam: $naamInsert"
echo "Haarkleur: $haarkleurInsert"
echo "Lengte: $lengteInsert"
echo "Bevestig?"
echo "1. Ja"
echo "2. Nee"
read correctInsert
if [ $correctInsert -eq 1 ] ; then
if [ -f $PlayboyNaam ] ; then
echo $naamInsert >> $PlayboyNaam
else
echo $naamInsert > $PlayboyNaam
fi
if [ -f $PlayboyHaarkleur ] ; then
echo $haarkleurInsert >> $PlayboyHaarkleur
else
echo $haarkleurInsert > $PlayboyHaarkleur
fi
if [ -f $PlayboyLengte ] ; then
echo $lengteInsert >> $PlayboyLengte
else
echo $lengteInsert > $PlayboyLengte
fi
paste $PlayboyNaam $PlayboyHaarkleur $PlayboyLengte > Playboy/summary
fi
}

function Select() {
echo "Wilt u zoeken of bekijken?"
echo "1. Bekijken"
echo "2. Zoeken"
read keuzeSelect
case "$keuzeSelect" in
"1")
echo "Waar wilt u in kijken?”
echo "1. PlayboyNaam"
echo "2. PlayboyHaarkleur"
echo "3. PlayboyLengte"
read tabelSelect
if [ $tabelSelect -eq 1 ] ; then
cat $PlayboyNaam
else
if [ $tabelSelect -eq 2 ] ; then
cat $PlayboyHaarkleur
else
if [ $tabelSelect -eq 3 ] ; then
cat $PlayboyLengte
else echo "foute invoer"
exit
fi
fi
fi
;;
"2")
SelectWhere
;;
*)
echo "error: foute invoer"
Select
;;
esac
}

function Update() {
echo "Welke gegevens wilt u veranderen?"
echo "1. Playboy bunny namen"
echo "2. Playboy bunny haarkleuren"
echo "3. Playboy bunny lengtes"
read choiceUpdate
case "$choiceUpdate" in
"1")
cat $PlayboyNaam
echo "Typ het naam in van de Playboy bunny die u wilt wijzigen, geef ook het nieuwe naam op a.u.b. "
read naamUpdate1
read naamUpdate2
sed -i s/$naamUpdate1/$naamUpdate2/g $PlayboyNaam
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht
;;
"2")
cat $PlayboyHaarkleur
echo "Typ het oude en het nieuwe haarkleur in a.u.b. "
read haarkleurUpdate1
read haarkleurUpdate2
sed -i s/$haarkleurUpdate1/$haarkleurUpdate2/g $PlayboyHaarkleur
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht
;;
"3")
cat $PlayboyLengte
echo "Typ het oude en het nieuwe lengte van de bunny in: "
read lengteUpdate1
read lengteUpdate2
sed -i s/$lengteUpdate1/$lengteUpdate2/g $PlayboyLengte
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht

esac
}

function Toon() {
paste $PlayboyNaam $PlayboyHaarkleur $PlayboyLengte
}

function SelectWhere() {
echo "Wat zoekt u?”
echo "1. Playboy Bunny’s”
read tabelSelect
case "$tabelSelect" in
"1")
echo "wilt u zoeken op haarkleur,lengte of naam?"
echo "1. Naam"
echo "2. Haarkleur"
echo "3. Lengte"
read choice
if [ $choice -eq 1 ] ; then
cat $PlayboyNaam
echo "Typ het playboy bunny naam in:"
read naam
grep $naam Playboy/overzicht | awk '{print $1}'
else
if [ $choice -eq 2 ] ; then
cat $PlayboyHaarkleur
echo "Typ het playboy bunny haarkleur in:"
read haarkleur
grep $haarkleur Playboy/overzicht | awk '{print $2}'
if [ $choice -eq 3 ] ; then
cat $PlayboyLengte
echo "Typ het playboy bunny lengte in:"
read lengte
grep $lengte Playboy/overzicht | awk '{print $3}'
fi
fi
fi
;;
*)
echo "Dit is geen geldige invoer, probeer het opnieuw"
SelectWhere


esac
}


if [ -z $1 ] ; then

case "$*" in
"--help")
echo $(tput setaf 3) "
echo "Help sectie”
;;
"SELECT ALL FROM Playboy")

cat /home/ivi/farama02/Playboy/overzicht
;;
"SELECT $2 FROM Playboy")
cat /home/ivi/farama02/Playboy/$2
;;
"SELECT $2 FROM student WHERE $6 = $8")
if [ $2 == "naam" ] ; then
grep $8 Playboy/overzicht | awk '{print $1}'
else
if [ $2 == "haarkleur" ] ; then
grep $8 Playboy/overzicht | awk '{print $2}'
else
if [ $2 == "lengte" ] ; then
grep $8 Playboy/overzicht | awk '{print $3}'
fi
fi
fi
;;

"INSERT INTO Playboy $4 VALUES $6")
if [ $4 == "naam" ] ; then
if [ -f $PlayboyNaam ] ; then
echo $6 >> $PlayboyNaam
else
echo $6 > $PlayboyNaam
fi
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht
else
if [ $4 == "haarkleur" ] ; then
if [ -f $PlayboyHaarkleur ] ; then
echo $6 >> $PlayboyHaarkleur
else
echo $6 > $PlayboyHaarkleur
fi
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht

else
if [ $4 == "lengte" ] ; then
if [ -f $PlayboyLengte ] ; then
echo $6 >> $PlayboyLengte
else
echo $6 > $PlayboyLengte
fi
paste $Playboynaam $PlayboyHaarkleur $PlayboyLengte > Playboy/overzicht

fi
fi
fi
;;

"UPDATE student SET $4 = $6 WHERE $8 = ${10}")
echo "${10}"
;;

*)
echo "Wrong variable"
;;
esac
fi

I am hoping someone can help me into the right direction, I cant seem to find what I am doing wrong.

Thanks in advance!
 
Old 06-23-2011, 01:17 PM   #2
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
I think it's a closing quote that's giving you trouble. In function Select():

Quote:
Originally Posted by pedram230389 View Post
echo "Waar wilt u in kijken?”
The opening quote is character 0x22, but the closing quote is character 0xE2. Remove the closing quote and use a regular quote, that is, ASCII 0x22.

After changing that, I can run your script. Gives me "Wrong variable" though.
 
Old 06-23-2011, 01:29 PM   #3
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks mate! I finally got rid of the errors

But yeah I got the same problem as you. I am looking at the codes now to see if why it is not showing the menu. It appereantly just fly's through the script.

Btw, student needs to be Playboy at the end of the line
 
Old 06-23-2011, 01:51 PM   #4
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Quote:
Originally Posted by pedram230389 View Post
Thanks mate! I finally got rid of the errors
Great. I'm glad I could help.


Quote:
Originally Posted by pedram230389 View Post
Btw, student needs to be Playboy at the end of the line
Cool! But I suppose that's your homework, right?
 
Old 06-23-2011, 01:54 PM   #5
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yeah, it's the homework for the whole class, but alot of us students are getting stuck on certain parts A mate of mine has the same problem as I have right now.
 
Old 06-23-2011, 02:05 PM   #6
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Well, just keep at it. Now that you don't have any syntax errors it's just a matter of study and hard work. I'm sure you'll get it right.
 
Old 06-23-2011, 02:07 PM   #7
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I got a question tho,

Does the positon of the code's matter in scripting? I mean like, do things change if I change the places of 2 functions? Select and Insert for example
 
Old 06-23-2011, 02:10 PM   #8
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
No, it doesn't matter where the functions are. However it'll look better and will be easier to read if you follow some structure in you script, eg, put the functions first and the main block of code at the bottom. And use some indentation on the lines, it's easier on the eyes (and the brain) to see the program structure with just a glance.
 
Old 06-23-2011, 02:20 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

And whenever you see an "unexpected end of file" error, it's a sure sign that you're missing a closing quote or tag of some kind. Unfortunately missing quotes are hard ones to deal with because the line reported as the error is often not where the actual problem lies. Mispaired quotes can propagate through the entire file and only the last unpaired one actually results in the error.


A few more comments:
1.)
Numerical conditions like this:
Code:
if [ $correctInsert -eq 1 ] ; then
would be better done inside ((..))
Code:
if (( correctInsert == 1 )) ; then
2.)
To protect against wordsplitting, it's generally recommended to quote variables, especially when inside the old [..] test. Also consider using the newer [[..]] test keyword instead.

3.)
awk and sed both incorporate grep-like ability, so you should almost never need to use both together.
Code:
grep $naam Playboy/overzicht | awk '{print $1}'
...can instead be..

Code:
#first imports the shell variable into an awk variable.
awk -v search="$naam" '{ if ( $0 ~ search ) {print $1} }' Playboy/overzicht

#or a shorter form that unquotes the shell variable to allow expansion:

awk ' /'"$naam"'/ {print $1}' Playboy/overzicht
4.)
You can save your script a few unnecessary subshell openings by defining your color codes once and saving them in variables.
Code:
red="$(tput setaf 3)"
off="$(tput sgr0)"

echo "${red}foobar${off}"
5.)
You might consider using here document to print your longer text blocks, or even use multiple echo commands (one per line), instead of one single quoted multi-line echo.
 
Old 06-23-2011, 03:00 PM   #10
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for you help David! I applied all your suggestions into the script. Really appreciate it!

But I still cant see the menu, the output now is:

foobar
Error

I dont understand why the it is not showing anything except the first and last echo command
 
Old 06-23-2011, 03:08 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Try inserting a set -xv command before where it is going wrong to see what's going on. set +xv turns tracing off.
 
Old 06-23-2011, 03:09 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Show us the code.
 
Old 06-23-2011, 04:33 PM   #13
pedram230389
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Never mind guys, I got it working

The problem was that I didnt call a function to start with.

Thanks for all your help guys!
 
Old 06-23-2011, 06:49 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
BTW, the order does matter slightly. The parser (interpreter) works its way through from the top, so a function definition must appear before it is called, that's why the 'main' section is at the bottom.
On a compiled lang we normally put 'main' at the top because the compiler reads the entire file before trying to compile it.
Here are some useful links for Linux & bash
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 06-23-2011, 07:59 PM   #15
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Quote:
Originally Posted by chrism01 View Post
BTW, the order does matter slightly. The parser (interpreter) works its way through from the top, so a function definition must appear before it is called, that's why the 'main' section is at the bottom.
That is correct. Thanks for clarifying.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Start-Up Script & Shutdown/Kill Script needed guggilamsandeep Red Hat 1 05-11-2011 09:58 AM
serious script help needed bino25 Linux - Newbie 8 02-18-2011 05:15 PM
script help needed: patilvidya Linux - Software 2 08-29-2007 09:53 AM
Script needed ganeshinforum Programming 2 01-05-2007 08:29 AM
little script help needed abisko00 Programming 7 07-11-2005 09:37 AM


All times are GMT -5. The time now is 10:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration