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, 10: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, 11: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, 11: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, 11: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-28-2004, 12:02 AM   #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-28-2004, 12:29 AM   #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-28-2004 at 12:30 AM.
 
Old 04-28-2004, 12:45 AM   #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, 01: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, 05: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 06:03 AM.
 
Old 04-28-2004, 10: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, 11: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 11:33 AM.
 
Old 04-28-2004, 01: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 03:43 AM
simple shell script question. strifel Programming 7 09-13-2003 02:44 PM
simple question on shell script gsbarry Linux - General 2 05-23-2003 05:09 AM
simple shell script question mathfeel Linux - General 12 03-06-2003 12:23 PM
Simple C Shell script is not so simple elconde Programming 2 09-17-2001 12:53 AM


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