Issue with echo $list not working in variable

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 34
Reputation: skelly16 is an unknown quantity at this point 
Solved Threads: 0
skelly16 skelly16 is offline Offline
Light Poster

Issue with echo $list not working in variable

 
0
  #1
Apr 14th, 2008
HI All

I have a script which counts how many times a job has failed.
For some reason my echo $list wont work in a variable, it works if i dont put it in a variable, but i need this in a variable so i can do a grep -c against another file.

Any help would be appreciated.

#!/usr/bin/ksh

set -x

cat /apps/tech/testruniquefailures | while read list ; do
JOBNAME=`echo $list`
Num=`grep -c $JOBNAME /apps/tech/testfailures1`
if [ $Num -gt 0 ] ; then
print $JOBNAME has failed $Num times >> /apps/tech/jinfo.txt
fi
done

This is the output with debug

+cat /apps/tech/testruniquefailures
+read list
+JOBNAME=echo
++ grep -c /apps/tech/testfailures1
Num=0
+ [ - -gt 0 ]
+read list
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 34
Reputation: skelly16 is an unknown quantity at this point 
Solved Threads: 0
skelly16 skelly16 is offline Offline
Light Poster

Re: Issue with echo $list not working in variable

 
0
  #2
Apr 14th, 2008
Ok the issue i have worked out is when there seems to be a space on the new line on the file.

i.e.

cat apps/tech/testruniquefailures
<space is here>
batchjob1
batchjob2

How do i make sure the above file does not have a space on the top of it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Issue with echo $list not working in variable

 
0
  #3
Apr 14th, 2008
Originally Posted by skelly16 View Post
Ok the issue i have worked out is when there seems to be a space on the new line on the file.

i.e.

cat apps/tech/testruniquefailures
<space is here>
batchjob1
batchjob2

How do i make sure the above file does not have a space on the top of it.
don't use useless cat with while loop
Shell Scripting Syntax (Toggle Plain Text)
  1. while read -r line
  2. do
  3. #processing
  4. done < "file"
'

anyway, here's another way to solve your problem
Shell Scripting Syntax (Toggle Plain Text)
  1. # more file
  2. C
  3. D
  4. # more file1
  5. A
  6. B
  7. C
  8. D
  9. E
  10. C
  11. D
  12. C
  13. D
  14. A
  15.  
  16. # grep -f file file1 | sort | uniq -c
  17. 3 C
  18. 3 D
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Issue with echo $list not working in variable

 
0
  #4
Apr 14th, 2008
Grepping and sorting should do it for your script.

It looks like the file you have a blank space in is already assumed to exist in this script (doesn't get created in it).

Do you know what other script (or program) creates the file? That might be the easiest way to prevent the blank line from appearing at the top if you don't want it there.

It's probably a good idea, if that happens enough, to include some functionality in your code to strip blank lines before you parse the file.

Otherwise, the above posted answers are both good ways to deal with your issue.

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC