Hello Luggers
The following function takes 2 arguments, "a filename" and "number of fields". It checks the number of field of each record against the second argument. If it doesn't match, it increase the value of the variable "NM_CNT" by 1. But, once the loop has finished execution, the value of this variable gets reset to "0".
verify_record() { FILE=$1 FIELDS=$2 cat $FILE | while read RECORD do if [ `printf "$RECORD" | awk -F| '{ print NF }'` -ne $FIELDS ]; then NM_CNT=`expr $NM_CNT + 1` printf "NM_CNT=$NM_CNT\n" fi done printf "NM_CNT=$NM_CNT\n" return $NM_CNT }
Here is the output. The maximum value of NM_CNT in the loop is 2, but once it comes out of the loop, its value becomes "0".
$ verify_record temp/a 2 (in loop)NM_CNT=1 (in loop)NM_CNT=2 NM_CNT=0
Can anybody help me to solve this problem? Currently, I redirect the value of this variable in a file for using it outside the function. I run this script under the "sh" environment on SUN Solaris 8.
Regards SV
--------------------------------- Jiyo cricket on Yahoo! India cricket Yahoo! Messenger Mobile Stay in touch with your buddies all the time.
On 3/16/06, Vidyadutt S vidyadutt_s@yahoo.com wrote:
FILE=$1 FIELDS=$2 cat $FILE | while read RECORD do if [ `printf "$RECORD" | awk -F| '{ print NF }'` -ne $FIELDS ]; then NM_CNT=`expr $NM_CNT + 1` printf "NM_CNT=$NM_CNT\n" fi done printf "NM_CNT=$NM_CNT\n" return $NM_CNT
I am a novice at programming but so don't flame me if I am wrong but the problem can be with the line marked in bold. Shouldn't you be returning NM-CNT instead of $NM_CNT?
Sometime Today, m cobbled together some glyphs to say:
if [ `printf "$RECORD" | awk -F\| '{ print NF }'` -ne $FIELDS ]; then NM_CNT=`expr $NM_CNT + 1` printf "NM_CNT=$NM_CNT\n" fi
done printf "NM_CNT=$NM_CNT\n" return $NM_CNT
I am a novice at programming but so don't flame me if I am wrong but the problem can be with the line marked in bold. Shouldn't you be returning NM-CNT instead of $NM_CNT?
I don't see NM-CNT anywhere in his code, so not sure how you came to that. Also, there is nothing bold in your mail (hint, we block HTML on this mailing list).
Philip
Sometime Today, VS cobbled together some glyphs to say:
verify_record() { FILE=$1 FIELDS=$2 cat $FILE | while read RECORD do if [ `printf "$RECORD" | awk -F| '{ print NF }'` -ne $FIELDS ]; then NM_CNT=`expr $NM_CNT + 1` printf "NM_CNT=$NM_CNT\n" fi done printf "NM_CNT=$NM_CNT\n" return $NM_CNT }
Here is the output. The maximum value of NM_CNT in the loop is 2, but once it comes out of the loop, its value becomes "0".
Return value is stored in $? after the function returns, check the value of $?
Also, configure your mail client to wrap long lines at approxy 74 characters.
Philip
On 3/16/06, Vidyadutt S vidyadutt_s@yahoo.com wrote:
The following function takes 2 arguments, "a filename" and "number of fields". It checks >the number of field of each record against the second argument. If it doesn't match, it >increase the value of the variable "NM_CNT" by 1. But, once the loop has finished >execution, the value of this variable gets reset to "0".
This problem is due the piping in the following line ;)
cat $FILE | while read RECORD do if [ `printf "$RECORD" | awk -F| '{ print NF }'` -ne $FIELDS ]; then NM_CNT=`expr $NM_CNT + 1` printf "NM_CNT=$NM_CNT\n" fi done printf "NM_CNT=$NM_CNT\n"
You can avoid it by changing the about code to..
for RECORD in `cat tst|awk '{print NF}'`; do if [ $RECORD -ne $FIELDS ]; then NM_CNT=$(($NM_CNT+1)) printf "NM_CNT=$NM_CNT\n" fi done
-- ___________________________________________
-----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/S C++++ P+ L+++(++++) E++(+++) W++ N+++ w--- e+++ a-- y d- s:++ r-- b+Y+ G+++ ------END GEEK CODE BLOCK------ decode with www.ebb.org/ungeek
Dear Vihang
The script works perfectly after I modified it as you mentioned.
Thanks a lot.
Regards Vidyadutt. You can avoid it by changing the about code to..
for RECORD in `cat tst|awk '{print NF}'`; do if [ $RECORD -ne $FIELDS ]; then NM_CNT=$(($NM_CNT+1)) printf "NM_CNT=$NM_CNT\n" fi done --------------------------------- Jiyo cricket on Yahoo! India cricket Yahoo! Messenger Mobile Stay in touch with your buddies all the time.