| | |
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:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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.
Shell Scripting Syntax (Toggle Plain Text)
while read -r line do #processing done < "file"
anyway, here's another way to solve your problem
Shell Scripting Syntax (Toggle Plain Text)
# more file C D # more file1 A B C D E C D C D A # grep -f file file1 | sort | uniq -c 3 C 3 D
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: Finding the processor temp
- Next Thread: Adding version number to builds
| Thread Tools | Search this Thread |





