0

I have a script that reads a table file and is supposed to look for a string. If it finds the string, it should parse the line the string was found on and do some stuff. The script exits upon the first time it fails to find a string. (first line of the file.) Table file format is:

10.1.1:0.0:lan:hq:3:254:br0:.1.16:.1.1:.1.21:.1.23:hq:eoc:hq

There are variables given definitions before the while loop, ($ip and $ipad) plus a logging and a debugging function. I have pulled all the items that make this hard to read (debugging, logging, notes, etc.) The while loop looks like:

line=$(grep ${ipad} {$net})
    if [ -z ${line}]; then
    base=$(echo ${ip}|awk -F"." '{print $1"."$2}')
    mask=$(echo ${line}|awk -F":" '{print $2}')
    zone=$(echo ${line}|awk -F":" '{print $3}')
    loc=$(echo ${line}|awk -F":" '{print $4}')
    low=$(echo ${line}|awk -F":" '{print $5}')
    high=$(echo ${line}|awk -F":" '{print $6}')
    bri=$(echo ${line}|awk -F":" '{print $7}')
    bftp=$(echo ${line}|awk -F":" '{print $8}')
    ftp=${base}${bftp}
    bgate=$(echo ${line}|awk -F":" '{print $9}')
    gate=${base}${bgate}
    bns=$(echo ${line}|awk -F":" '{print $10}')
    bns1=$(echo ${line}|awk -F":" '{print $11}')
    if [[ ${loc} == "var1" && ${zone} == "var2" ]]; then 
        ns=10.10${bns}
        ns1=10.10${bns1}
    else
        ns=${base}${bns}
        ns1=${base}${bns1}
    fi
    if [[ ${loc} == "var3" && ${zone} == "var2" ]]; then
        ns=10.1${bns}
        ns1=10.1${bns1}
    else
        ns=${base}${bns}
        ns1=${base}${bns1}
    fi
    tld=$(echo ${line}|awk -F":" '{print $12}')
    tld1=$(echo ${line}|awk -F":" '{print $13}')
    com=$(echo ${line}|awk -F":" '{print $14}')
        if [ -f ${iptab} ]; then
            spc=$(cat ${iptab})
            tab=${zone}-${spc}.${loc}
        else
            tab=${zone}.${loc}
        fi
        # If no RAM size is given use 1GB
                if [ -z $ram ]; then
                        ram=1024
                fi
                # If no disk size is given use 10GB
                if [ -z $mem ]; then
                        mem=10
                fi
    #Then Do Some stuff with that info

    else
        log "IP address given does not match any of the IP Network blocks. Please check the address and try again."
        if [ -f ${korg} ]; then 
            mv ${korg} ${ksin}
        fi
        exit 0          
    fi

From the logging output I can see that the while loop doesn't find the string on the first line and exits. If I pass an argument for the script which has the first line string in it, the while loop executes everything and then tries the second line without exiting. Completely baffled.

  • 3
    I'm counting 14 separate invocations of awk per line here! That's massively inefficient. If you could edit your question to include an example of the input to this along with what you expect as output, we can probably make it a fair bit more efficient. – Kusalananda Oct 7 '17 at 7:49
  • 2
    Run your script through bash -vx script.sh. Then you see where it stops. Add that to your question. – Hauke Laging Oct 7 '17 at 7:54
  • 2
    Your(?) indention is terrible. Code on the same level shall have the same indention so that you can easily see which code is on the same level... – Hauke Laging Oct 7 '17 at 8:00
  • Yep. I agree on all comments. Laid in bed tonight thinking about this. Realized that I was super over engineering this. A simple grep <string> <file> gets the job done. Now to figure out how to parse the line without awk ing so dang much. – Matthew Lee Allen-Goebel Oct 7 '17 at 10:57
  • The line you edited into you question is actually two lines. Should the newline at the end of the line go away, or should it be replaced by a :? – Kusalananda Oct 7 '17 at 13:14
0

At the beginning of the loop there is a check whether the line contains certain data:

if echo ${line}|grep ${ipad}; then

The else branch of this if ends with

exit 0

That is where and why the script stops.

  • Yep. . . Sometimes I wonder if I should just go back to driving trucks. – Matthew Lee Allen-Goebel Oct 10 '17 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.