ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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.
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.
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.
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.
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
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638
Rep:
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?
-------------------------------------------------------------------------
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.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,638
Rep:
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 ~]$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.