943,974 Members | Top Members by Rank

Ad:
Apr 14th, 2008
0

Issue with echo $list not working in variable

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
skelly16 is offline Offline
34 posts
since Aug 2007
Apr 14th, 2008
0

Re: Issue with echo $list not working in variable

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
skelly16 is offline Offline
34 posts
since Aug 2007
Apr 14th, 2008
0

Re: Issue with echo $list not working in variable

Click to Expand / Collapse  Quote originally posted by skelly16 ...
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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Apr 14th, 2008
0

Re: Issue with echo $list not working in variable

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Finding the processor temp
Next Thread in Shell Scripting Forum Timeline: Adding version number to builds





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC