LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   veprory simple but frustrating for a newbie shell script question (https://www.linuxquestions.org/questions/linux-newbie-8/veprory-simple-but-frustrating-for-a-newbie-shell-script-question-175345/)

marigb 04-27-2004 09:49 PM

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

ToniT 04-27-2004 10:28 PM

echo $numb > $name.ls

marigb 04-27-2004 10:35 PM

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....

ToniT 04-27-2004 10:47 PM

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)

marigb 04-27-2004 11:02 PM

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??

ToniT 04-27-2004 11:29 PM

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.

marigb 04-27-2004 11:45 PM

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

marigb 04-28-2004 12:10 AM

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

ToniT 04-28-2004 04:57 AM

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...

marigb 04-28-2004 09:20 AM

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

ToniT 04-28-2004 10:28 AM

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.

marigb 04-28-2004 12:11 PM

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


All times are GMT -5. The time now is 11:11 AM.