LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-12-2006, 08:03 AM   #1
jordanmc31
LQ Newbie
 
Registered: Aug 2005
Posts: 6

Rep: Reputation: 0
Shell script not functioning properly


I'm struggling to get this shell script working. Basically the first loop reads the input only one time. It just terminates after finishing the inside loop. The names file has multiple entries though.
#! /bin/sh




while read names
do

num=1000
while [ $num -lt 1005 ] ;
do
v2=${num}.eml
v1=${names}@sdaf.com

echo "------$$Q#!@!(*)*&!)!@()*!@&!)@("
echo "This is the Num:" $num
echo "This is the Names:" $names
more $v2 | grep $names | wc -l
sleep 1

echo "*$&*(*&@#*&@!)(*)(*@)!(*!@&^^%#!"
if [ `more $v2 | grep $names | wc -l` -ne 0 ]
then
echo "YAY I FOUND ONE"
sleep 1
mkdir /home/testing/$names
if [ ! -d /$names ]
then
mkdir /home/testing/$names
fi
cd $names
cp /home/testing/$v2 .
cd /home/testing
fi
num=$((num + 1))
done

done < names
 
Old 04-12-2006, 08:36 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Try to size down your problem/script. Start with something very simple like:
Code:
while read names
do
   echo $names
done < names
May be a unexpected entry in the input file is causing that unexpected behaviour.
 
Old 04-12-2006, 10:25 AM   #3
jordanmc31
LQ Newbie
 
Registered: Aug 2005
Posts: 6

Original Poster
Rep: Reputation: 0
I've done what you said including both the loops and can get the proper output. But as soon as I include the IF statements it all goes downhill. Any if statement I put within the second loop kills the program when its done with the retrieved name.
 
Old 04-12-2006, 10:52 AM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Ok. Good to know the problem is related with the if statement.
In fact, take a close look, the code
Code:
if [ `more $v2 | grep $names | wc -l` -ne 0 ]
is a good candidate to be replaced by

Code:
if [ "`cat $v2 | grep $names | wc -l`" -ne "0" ]
or better:
if [ "$(grep $v2 $names | wc -l)" -ne "0" ]
or even better
result="$(grep $v2 $names | wc -l)"
echo "result is xxx${result}xxx" # for debug purposes
if [ "$result" -ne "0" ]
The point here, I believe, is the quotation marks. In tests like that, always use quotation marks to protect both sides; it's a kind of bash idiosyncrasy I think.
Anyway, try to avoid complex constructions, breaking in smaller parts that are easy to debug.

good debug !
 
Old 04-12-2006, 11:18 AM   #5
jordanmc31
LQ Newbie
 
Registered: Aug 2005
Posts: 6

Original Poster
Rep: Reputation: 0
You hit that right on... I used your result= statement and everything is now working flawless. I just couldn't think of something better than my original more statement. I'm sorta a newb in the scripting area.


thanks again.
 
Old 04-12-2006, 11:31 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Quote:
...and everything is now working flawless.
I hope so, but just check again the code I wrote. I make 2 mistakes. The correct is:

Code:
result=$(grep "$names" $v2 | wc -l)
The two mistakes are: 1)I inverted the string to be search by grep and the name of file and 2) You need to put double quotes on "$names" to search for a multi word, otherwise, the second word is interpreted as a filename.

sorry any inconvenience,
 
Old 04-12-2006, 11:46 AM   #7
jordanmc31
LQ Newbie
 
Registered: Aug 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Thats alright I figured that part out.
 
Old 04-12-2006, 04:47 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
if [ "`cat $v2 | grep $names | wc -l`" -ne "0" ]

I am puzzled by the above.

The cat command is to show your text files.

What is 'cat $v2' means?



------------------------------------------------------------------------

The grep command is to find texts on 'text' files. You could write a file using 'vi' editor and use the grep command to find patterns on it.

What is 'grep $names ? What is the file here?
-------------------------------------------------------------------------



The command 'wc' is to count words on a text file. What is 'wc -l' here?

I know 'ne' means not equals. So here it would not equals to zero.

What is 'wc -l' here?
------------------------------------------------------------------------


Lastly the pipe sign ( | ). Does it mean 'or' here?

Last edited by Gins; 04-12-2006 at 04:48 PM.
 
Old 04-12-2006, 04:54 PM   #9
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
Notice the backticks around
Code:
cat $v2 | grep $names | wc -l
that means the code will be executed as though executed in a shell and replaced with the output. So the |'s take the output of the left command and feed it through the input of the right command.

--------

From man wc:
Code:
DESCRIPTION
     The wc utility displays the number of lines, words, and bytes contained
     in each input file, or standard input (if no file is specified) to the
     standard output.  A line is defined as a string of characters delimited
     by a <newline> character.  Characters beyond the final <newline> charac-
     ter will not be included in the line count.
So ultimatly the command returns the number of times the contents of $names is found in the file $v2

----

v2 and names are variables defined earlier in the script.

Last edited by phil.d.g; 04-12-2006 at 04:56 PM.
 
Old 04-12-2006, 05:44 PM   #10
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
I have a file called 'teco7'.

If I wrote 'cat teco7', I would read the file.


cat $v2 | grep $names | wc -l

So what is 'cat $v2'?

You have neatly explained the function of ( | ) pipe.

I can't fathom out 'cat $v2' .


-----------------------------------------

If I wrote ' wc teco7' , I would get the following;


[nissanka@c83-250-104-204 ~]$ wc teco7
57 507 2953 teco7
[nissanka@c83-250-104-204 ~]$ wc teco7 -l
57 teco7
[nissanka@c83-250-104-204 ~]$
 
Old 04-12-2006, 06:43 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
v2 is the var name containing a filename as the value ie $v2, so cat $v2 means cat the value found in the v2 var, which happens to be a filename assigned earlier in the script.
If you read the man page for wc, you'll find that if used as
wc teco7
the results are the num of lines, words, chars and input filename. To get just the num of lines, use '-l' switch
Code:
DESCRIPTION
       Print  newline,  word, and byte counts for each FILE, and a total line
       if more than one FILE is specified.  With no FILE, or when FILE is  -,
       read standard input.

       -c, --bytes
              print the byte counts

       -m, --chars
              print the character counts

       -l, --lines
              print the newline counts

       -L, --max-line-length
              print the length of the longest line

       -w, --words
              print the word counts
 
Old 04-13-2006, 03:56 AM   #12
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
Gins, try this example in your terminal, make sure teco7 is a real file in your cwd (current working directory).
Code:
$ v2="teco7"
$ cat $v2
$v2 is a variable that contains some data, in this case a filename. The same is true of $names, however that may contain data of some other context
 
Old 04-13-2006, 01:43 PM   #13
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
I thank both Chrism01 and Phil.d.g for the replies.
Now this is crystal clear to me.

I am learning Scripting language through the help of this forum. It is a matter of finding time. I am very grateful to you all for the replies. More and more will come from me.

First you made new variable called '' v2 ''. I don't think this variable will last for ever. When I restart the computer, this will disappear. Am I correct?
-------------------------------------------------------------------------

[nissanka@c83-250-104-105 ~]$ v2="teco7"
[nissanka@c83-250-104-105 ~]$ cat $v2


India beat England by nine wickets before tea on the final day to go 1-0 up in the series with one Test to play.

Munaf Patel was the hero in the morning with three quick wickets which sent the tourists tumbling to 181 all out from an overnight position of 112-5. Andrew Flintoff was last man out for 51 but only Steve Harmison (13) offered the captain any meaningful support.

Set 144 to win, India lost ....

[ A portion of the file 'teco7'.]

Last edited by Gins; 04-13-2006 at 01:44 PM.
 
Old 04-14-2006, 02:59 AM   #14
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
It'll disappear as soon as you close the terminal you defined it in
 
Old 04-14-2006, 01:36 PM   #15
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638

Rep: Reputation: 46
I have file called '' The Holy Family's flight to Egypt ''.




[nissanka@c83-250-108-126 ~]$ v8="The Holy Family's flight to Egypt "
[nissanka@c83-250-108-126 ~]$ cat $v8
cat: The: No such file or directory
cat: Holy: No such file or directory
cat: Family's: No such file or directory
cat: flight: No such file or directory
cat: to: No such file or directory
cat: Egypt: No such file or directory
[nissanka@c83-250-108-126 ~]$

Why didn't it work?
 
  


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
ProFTPd installed, but not functioning properly kyletriggs Linux - Software 3 02-20-2006 12:00 PM
My network is not functioning properly procfs Linux - Networking 5 02-16-2006 12:50 AM
TCPIP not functioning properly safescott Linux - Networking 8 06-02-2003 01:13 AM
C++ problem , string.h not functioning properly cyberswami Programming 4 02-18-2003 11:20 PM


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