LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-19-2007, 02:23 PM   #1
BoB4ik
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Rep: Reputation: 0
I need some help with BASH scripts.


I'm new to Linux (i have latest Ubuntu version), i need to write 2 bash scripts for my OS subject in university, but because I'm new to this, i don't know "how to" well.
Situation:
I need 2 scripts for Library, one, which name is "jauns" cad add an entry to the Library file, another, which name "mekle" is a search script, that should be able to search the library in 2 ways - 1) by name, 2)by number of book pages.
The Library file has 5 columns - Book name, Author, Publisher, Year of publishing, Book pages (in my script they are Nosaukums Autors Izdevnieciba Gads Lappuses correspondingly(its in Latvian)).
Here is the 1 script's code - jauns:

Code:
if [ "$#" != "5" ]
then 

echo Nepareizs argumentu skaits.

echo Lai izveidotu ierakstu,  ievadiet argumentus sekojosa seciba:

echo Nosaukums Autors Izdevnieciba Gads Lappuses

echo Piemeeram - Kapitanskaja Doch Pushkin Strela 1886 356

exit

else

echo -e "$1\t$2\t$3\t$4\t$5" >>bibliteka

echo Pievienots ieraksts:

echo -e "$1\t$2\t$3\t$4\t$5\t$6\t$7"

fi
This one seems to work, but it has some minor things i don't understand.
Why if i enter 5 words with space between them, it does nothing, if I enter 4 or 6-7 it puts the entry in the Library file?

The main problem is the 2 script, here it is- mekle:

Code:
if [ "$1" = "1" ]

then

echo Meklesana pec nosaukuma:

grep "^$2" biblioteka

exit

fi

if [ "$1" = "2" ] 

then

echo Meklesana pec lappusem:

grep "^.*	.*	.*	.*	$2" biblioteka

exit

fi

echo Jusu ievade ir nederiga.

echo Pirmais arguments norada pec ka ir jamekle:

echo 1=meklet pec nosaukuma

echo 2=meklet pec lappusem

echo Otrais arguments ir meklejama vertiba

echo Piemeram: mekle 1 Tutepatas
When I run it from Terminal (/path/mekle) it gives me

Code:
./mekle: line 19: syntax error: unexpected end of file
What should i do, what did i do wrong? Please help me, I will have my exam on Friday.
 
Old 12-19-2007, 03:21 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,535

Rep: Reputation: 148Reputation: 148
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-19-2007, 03:29 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by BoB4ik View Post
This one seems to work, but it has some minor things i don't understand.
Why if i enter 5 words with space between them, it does nothing, if I enter 4 or 6-7 it puts the entry in the Library file?
This script worked properly for me.

Quote:
Originally Posted by BoB4ik View Post
When I run it from Terminal (/path/mekle) it gives me

Code:
./mekle: line 19: syntax error: unexpected end of file
What should i do, what did i do wrong? Please help me, I will have my exam on Friday.
This script also worked for me.

Do you have "#!/bin/bash" as your first line? That will probably help a lot.
 
Old 12-19-2007, 03:34 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I copied and pasted your first script and tested it myself. It worked. I tried 5 inputs and they were entered into the file separated with tabs. With 4, the lines you gave were echoed to the screen. I don't understand what your problem is with it.
 
Old 12-19-2007, 03:37 PM   #5
BoB4ik
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Original Poster
Rep: Reputation: 0
The very strange thing that just happened, I still don't know why, though... I've just recopied(last time a did copy/paste from my mekle file to this thread) the source code from my previous thread. Edited the mekle file with Kate editor, the text code ended up there the same as it was, but with a empty line between each line that contains text. And it worked.
Anybody knows why?
Thanks all of you, you made me copy paste it again, and it worked, because of that.
 
Old 12-19-2007, 05:10 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Just FYI, in your if statements you are comparing strings because you've got "value", but to compare numbers as numbers, you use a different set of operators: -eq, -ne ... etc
Bookmark & read this http://www.tldp.org/LDP/abs/html/com...l#EQUALSIGNREF
 
Old 12-20-2007, 01:48 AM   #7
BoB4ik
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks, i'll bookmark that.
 
Old 12-20-2007, 05:23 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
What language is that? I don't mean Bash, but that what is written in your own language.

jlinkels
 
Old 12-20-2007, 07:34 AM   #9
danex50
LQ Newbie
 
Registered: Feb 2004
Location: UK
Posts: 22

Rep: Reputation: 15
Thumbs up

he said that:
Quote:
its in Latvian
 
Old 12-20-2007, 09:29 AM   #10
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by BoB4ik View Post
Anybody knows why?
You probably edited your scripts on a Windows machine and then ran them on a Linux machine. Bash probably saw 0x0Ds in your file and crapped out. When you pasted them here, the 0x0Ds were converted to 0x0As. When you copied your scripts from here and pasted them back, bash saw no 0x0Ds and was able to run your scripts.

The moral of this story is to use a real editor if you are going to create scripts on a Windows machine. Notepad or whatever you are using is brain damaged.
 
Old 12-20-2007, 04:06 PM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Quote:
unexpected end of file
Normally means that there was a block of code that was being opened but wasn't being closed

Maybe an if statement was missing the relating fi closing statement (or the closing fi was contained within a string)

Looking at the code, I can't see any reason for it, and the code works on my system.

However I would suggest using quotes when using echo statements.

I would also suggest using a case statement, example:
Code:
case $1 in
   1) echo "Meklesana pec nosaukuma:"
      grep "^$2" biblioteka;;

   2) echo "Meklesana pec lappusem:"
      grep "^.*	.*	.*	.*	$2" biblioteka;;

   *) echo "Jusu ievade ir nederiga."
      echo "Pirmais arguments norada pec ka ir jamekle:"
      echo "1=meklet pec nosaukuma"
      echo "2=meklet pec lappusem"
      echo "Otrais arguments ir meklejama vertiba"
      echo "Piemeram: mekle 1 Tutepatas";;

esac
This will run different blocks of code based on the value of $1.

To enter a book title with more than one word you would also need to use quotes:

./jauns "Essential Systems Administration 2nd Edition" "AEleen Frisch" OReily 1995 758
 
  


Reply

Tags
linux, ubuntu


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
little help with bash scripts sniff Programming 2 10-26-2005 05:17 PM
bash scripts hoffmanyew Programming 3 08-11-2005 01:27 AM
Need Help With Bash Scripts the_woelf Linux - Software 4 06-30-2004 09:09 AM
Bash Scripts Skute Programming 7 03-12-2004 10:17 AM
Bash scripts? BajaNick Programming 3 07-05-2003 10:13 PM


All times are GMT -5. The time now is 03:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration