LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is something wrong with below code? (https://www.linuxquestions.org/questions/linux-newbie-8/is-something-wrong-with-below-code-4175497900/)

santosh0782 03-12-2014 04:49 AM

Is something wrong with below code?
 
Hi,

Is something wrong with below code, actually i am getting value for only one variable.
Code:

VIP_RCV_SGL_FILE[52]='P0366'
VIP_RCV_SGL_FILE[53]='P1039'
VIP_RCV_SGL_FILE[54]='P1041'
VIP_RCV_SGL_FILE[55]='P1044'
VIP_RCV_SGL_FILE[56]='P1390'




for VIP_CUST_ID in "${VIP_RCV_SGL_FILE[@]}"
do
FILE_STATUS_NOTE=$(grep ${VIP_CUST_ID} "${SCHED_CONFIG}"|awk -F '|' '{ print $5 }')

FILES_RCVD=$(sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -4|tail -1|tr '[<>]' ' '|awk -F ' ' '{print $2}')

FILES_WTNG=$(sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -4|tail -1|awk -F ' ' '{print $2}')


FILES_EXCEPTION=$(sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -4|tail -1|tr '[<>]' ' '|awk -F ' ' '{print $2}')

FILES_COUNT=$(sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -2|tail -1|tr '[<>]' ' '|awk -F ' ' '{print $2}')
echo "FILE_STATUS_NOTE=${FILE_STATUS_NOTE}"
echo "FILES_RCVD=${FILES_RCVD}"
echo "FILES_WTNG=${FILES_WTNG}"
echo "FILES_EXCEPTION=${FILES_EXCEPTION}"
echo "FILES_COUNT=${FILES_COUNT}"
echo "---------------------------"
done

output:
FILE_STATUS_NOTE=BMS - Expected 2 files between 03:00 - 04:30
FILES_RCVD=
FILES_WTNG=
FILES_EXCEPTION=
FILES_COUNT=

chrism01 03-12-2014 04:52 AM

1. set it to use bash
Code:

#!/bin/bash

# and use this to see what's really happening
set -xv

<your code here>

I'd also go one line at a time if you don't see the soln quickly.

pan64 03-12-2014 04:58 AM

I would simplify those sed|grep|head|tail|tr|awk chains, usually you can implement it in awk using only a few line of codes.
It will also speed up that loop

grail 03-12-2014 06:10 AM

I am with pan64, especially if you use the exact same line to be stored in 2 different variables:
Code:

FILES_RCVD=$(    sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -4|tail -1|tr '[<>]' ' '|awk -F ' ' '{print $2}')
FILES_EXCEPTION=$(sed -n "/${VIP_CUST_ID}/,/<\/data_block>/p" "${RTVIEW}"|grep -A4 '${FILE_STATUS_NOTE}' "${RTVIEW}"|head -4|tail -1|tr '[<>]' ' '|awk -F ' ' '{print $2}')

The spaces I placed in the first are superfluous to the output, but I cannot see any difference between these 2 lines except the variables they are assigned to.


All times are GMT -5. The time now is 12:10 PM.