Chris
Chris

Reputation: 1697

Conditional use of functions?

I created a bash script that parses ASCII files into a comma delimited output. It's worked great. Now, a new file layout for these files is being gradually introduced.

My script has now two parsing functions (one per layout) that I want to call depending on a specific marker that is present in the ASCII file header. The script is structured thusly:

#!/bin/bash

function parseNewfile() {...parse stuff...return stuff...}
function parseOldfile() {...parse stuff...return stuff...}

#loop thru ASCII files array
i=0
while [ $i -lt $len ]; do
    #check if file contains marker for new layout
        grep CSVHeaderBox output_$i.ASC
        #calls parsing function based on exit code
        if [ $? -eq 0 ]
        then
        CXD=`parseNewfile`
        else
        CXD=`parseOldfile`
        fi

    echo ${array[$i]}| awk -v cxd=`echo $CXD` ....
        let i++
done>>${outdir}/outfile.csv
...

The script does not err out. It always calls the original function "parseOldfile" and ignores the new one. Even when I specifically feed my script with several files with the new layout.

What I am trying to do seem very trivial. What am I missing here?

EDIT: Samples of old and new file layouts.

1) OLD File Layout

F779250B
=====BOX INFORMATION=====
Model         = R15-100
Man Date      = 07/17/2002
BIST Version  = 3.77
SW Version    = 0x122D
SW Name       = v1b1645
HW Version    = 1.1
Receiver ID   = 00089787556

=====DISK INFORMATION=====
....

2) NEW File Layout

F779250B
=====BOX INFORMATION=====
Model         = HR22-100
Man Date      = 07/17/2008
BIST Version  = 7.55
SW Version    = 0x066D
SW Name       = v18m1fgu
HW Version    = 2.3
Receiver ID   = 028910170936

CSVHeaderBox:Platform,ManufactureDate,BISTVersion,SWVersion,SWName,HWRevision,RID
CSVValuesBox:HR22-100,20080717,7.55,0x66D,v18m1fgu,2.3,028910170936

=====DISK INFORMATION=====
....

Upvotes: 1

Views: 136

Answers (3)

Chris
Chris

Reputation: 1697

I am bit embarrassed to write this but I solved my "problem".

After gedit (I am on Ubuntu) err-ed out several dozen times about "Trailing spaces", I copied and pasted my code into a new file and re-run my script.

It worked.

I have no explanation why.

Thanks to everyone for taking the time.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246867

This may not solve your problem, but a potential performance boost: instead of

    grep CSVHeaderBox output_$i.ASC
    #calls parsing function based on exit code
    if [ $? -eq 0 ]

use

    if grep -q CSVHeaderBox output_$i.ASC

qrep -q will exit successfully on the first match, so it doesn't have to scan the whole file. Plus you don't have to bother with the $? var.

Don't do this:

awk -v cxd=`echo $CXD`

Do this:

awk -v cxd="$CXD"

Upvotes: 1

leesei
leesei

Reputation: 6070

I'm not sure if this solves the OP's requirement.
What's the need for awk if your function knows how to parse the file?

#/bin/bash

function f1() {
    echo "f1() says $@"
}

function f2() {
    echo "f2() says $@"
}

FUN="f1"
${FUN} "foo"
FUN="f2"
${FUN} "bar"

Upvotes: 0

Related Questions