LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-27-2004, 09:49 PM   #1
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Rep: Reputation: 15
veprory simple but frustrating for a newbie shell script question


ok im writting this script for a class that I am taking and I am stuck. Its basically a "sophisticated address book"(something that I would think a shell script is not for primarily..anyways) when i get the information for the user ...my idea was to create a file with the name of the person and then in it put the phone number...something like this:

result=0
result= grep $fname??? | wc -l

if [ $result==0 ]
then

if [ $result==0 ]
then
echo "Enter Name"

read name
touch $name.ls
echo "Enter Number"
read numb

else
echo Entry Already Made : $result
fi
;;
PROBLEM cant seem to find a way to "pipe" a way to send the content of $numb inside the $name.ls file. I have tried to cat>$name.ls but when I run it waits for me to input ...
HELP i know this is very .... but im am a newbie...also I am having problems when i grep to find the file name...also waits for input ..is there something that i am missing


Thanks for your time
marigb
 
Old 04-27-2004, 10:28 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
echo $numb > $name.ls
 
Old 04-27-2004, 10:35 PM   #3
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
YAYYYYYYYY
Thank you thats one less "thing" to worry about....
THANK YOU THANK YOU FOR YOUR TIME
now if i could get that grep to work....
 
Old 04-27-2004, 10:47 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
What are you trying to grep?

Want number of lines in a file that's name is stored in variable fname?
Code:
wc -l < $fname
Want to store that into a variable?
Code:
result=$(wc -l < $fname)
 
Old 04-27-2004, 11:02 PM   #5
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
ok well i have made adjustments to the code...I am just looking if that "person" in this case file exists..aka there is one entry already
I tried the following:
result=0
result=$( grep $testname.ls|wc -l)

if [ $result==0 ]
then
echo "Enter Name"
echo $result
read name
touch $name.ls
echo "Enter Number"
read numb
echo $numb >$name.ls

else
echo Entry Already Made : $result
fi

but when i run it after i enter the first name the scripts just sits there as if expecting some input.. I am thinking its because of the grep??
 
Old 04-27-2004, 11:29 PM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Do you want to look inside the files?
replace your 'if [ $result==0 ]' (and all above it)
with
Code:
if grep -q $testname $fname
This checks if there is a line containing string of the variable $testname in the file $fname

Or do you want to check if a file $testname.ls exists?
Use line
Code:
 if [ -e $testname.ls ]
as your if statement.

See 'conditional expressions' in man bash for more details.

Last edited by ToniT; 04-27-2004 at 11:30 PM.
 
Old 04-27-2004, 11:45 PM   #7
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
well my goal is to check for the existance of the file ...but nothing has worked
i know it may be something to do with the if .... anyways thank you for the ideas
 
Old 04-28-2004, 12:10 AM   #8
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
well after some changing around and with some other examples i have this now:

1)echo "Enter Name"
read testname


if [ f $testname.ls ]
then
echo "File Already Exists"
else
touch $testname.ls
echo "Enter Number"
read numb
echo $numb >$name.ls

fi
;;
notice the f...and the space..I had a look at one example that did it this way ...when i run it ...i get the error in the if statement :unary operator expected but if i remove the space...then it will just take the if as true and always go through there
 
Old 04-28-2004, 04:57 AM   #9
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
no.

You didn't read the conditional expressions in man bash *and* you didn't read
my previous post. This is boring..

not 'if [ f foo ]', but 'if [ -f foo ]', as in my example.
Why -f this time, and -e on the previous? See the 'conditional expressions', in bash manual page.

What does the last sentence mean?
Type: 'man bash' and look for the section that has a title 'CONDITIONAL EXPRESSIONS'.
Or read an online version.


EDIT:
.. or maybe you did read those. I'm just too tired right now and go back to sleep...

Last edited by ToniT; 04-28-2004 at 05:03 AM.
 
Old 04-28-2004, 09:20 AM   #10
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
well i am sorry if this is boring ... I DID read the man's but it doesnt make sense to me ... Thank you for all your help but this is hard stuff for someone who has basically has self taught LInux in less than 2 months and is expected to write a shell script ..I have no books just the man and online resources...Anyways thanks for your help...I will try to figure it out
 
Old 04-28-2004, 10:28 AM   #11
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
You are progressing quite well for studying all in two months.

Sorry for being aggressive. I just were too tired last night. It is just sometimes hard to distinquish unwilingness to read a manual and situation where manual is read, but it wasn't helpful from eachother.

My mistake. This is a newbie forum afterall.

Last edited by ToniT; 04-28-2004 at 10:33 AM.
 
Old 04-28-2004, 12:11 PM   #12
marigb
Member
 
Registered: Apr 2004
Location: Puerto Rico
Distribution: Fedora Core 4
Posts: 33

Original Poster
Rep: Reputation: 15
update:
got everything to work...just need to fix a few things here and there..mostly pretty things up.. but everything seems to be working..I do appreciate your help and patience....
marigb
 
  


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
shell script newbie question yoshi95 Programming 1 03-11-2004 02:43 AM
simple shell script question. strifel Programming 7 09-13-2003 01:44 PM
simple question on shell script gsbarry Linux - General 2 05-23-2003 04:09 AM
simple shell script question mathfeel Linux - General 12 03-06-2003 11:23 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

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

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