Michael Bevilacqua on 16 Sep 2010 11:40:25 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] Building a command from variables in BASH


I have an ugly line that looks like this:

#cat $FILE1 | sed -e 's/            akrdcrc01<->cdp1crc02/     100003 akrdcrc01<->cdp1crc02/g' | sed -e 's/            ausrcrc01<->cdc1crc01/     100005 ausrcrc01<->cdc1crc01/g' | sed -e 's/            cindcrc01<->cdc1crc02/     100002 cindcrc01<->cdc1crc02/g' | sed -e 's/            daldcrc01<->cdp1crc01/     100004 daldcrc01<->cdp1crc01/g' | sed -e 's/            orarcrc01<->cdp1crc02/     100000 orarcrc01<->cdp1crc02/g' | sed -e 's/            manrcrc01<->cdp1crc01/     100007 manrcrc01<->cdp1crc01/g' | sed -e 's/            sdgdcrc01<->cdc1crc02/     100001 sdgdcrc01<->cdc1crc02/g' | sed -e 's/            syrrcrc01<->cdc1crc01/     100006 syrrcrc01<->cdc1crc01/g' | sed -e 's/            cdc1crc01<->cdp1crc01/     100014 cdc1crc01<->cdp1crc01/g' | sed -e 's/            cdc1crc02<->cdp1crc02/     100015 cdc1crc02<->cdp1crc02/g' > $FILE2


So, to make it more portable, I did the following:


function makeVCID()
{
        for i in "100000 orarcrc01<->cdp1crc02" "100001 sdgdcrc01<->cdc1crc02" "100002 cindcrc01<->cdc1crc02" "100003 akrdcrc01<->cdp1crc02" "100004 daldcrc01<->cdp1crc01" "100005 ausrcrc01<->cdc1crc01" "100006 syrrcrc01<->cdc1crc01" "100007 manrcrc01<->cdp1crc01" "100014 cdc1crc01<->cdp1crc01" "100015 cdc1crc02<->cdp1crc02" ;

                do echo -n '| sed -e 's/            `echo $i | cut -d" " -f2`/     `echo $i | cut -d" " -f1` `echo $i | cut -d" " -f2`/g' ' ;

        done
}

VCIDS=`makeVCID`



To test, I do:

echo $VCIDS

and I get exactly what I want in the string:

| sed -e s/ orarcrc01<->cdp1crc02/ 100000 orarcrc01<->cdp1crc02/g | sed -e s/ sdgdcrc01<->cdc1crc02/ 100001 sdgdcrc01<->cdc1crc02/g | sed -e s/ cindcrc01<->cdc1crc02/ 100002 cindcrc01<->cdc1crc02/g | sed -e s/ akrdcrc01<->cdp1crc02/ 100003 akrdcrc01<->cdp1crc02/g | sed -e s/ daldcrc01<->cdp1crc01/ 100004 daldcrc01<->cdp1crc01/g | sed -e s/ ausrcrc01<->cdc1crc01/ 100005 ausrcrc01<->cdc1crc01/g | sed -e s/ syrrcrc01<->cdc1crc01/ 100006 syrrcrc01<->cdc1crc01/g | sed -e s/ manrcrc01<->cdp1crc01/ 100007 manrcrc01<->cdp1crc01/g | sed -e s/ cdc1crc01<->cdp1crc01/ 100014 cdc1crc01<->cdp1crc01/g | sed -e s/ cdc1crc02<->cdp1crc02/ 100015 cdc1crc02<->cdp1crc02/g



So, now I want to rebuild that original ugly line and execute it, so I do:

exec "cat $FILE1 $VCIDS > $FILE2" 


And it fails because $VCIDS is no longer a command, it is now a string, so cat wants to read it as so.

I also tried wrapping it a couple of ways with eval which didn't work either.


My question is, how do I rebuild this command and execute it in BASH?




--
Michael D. Bevilacqua
michael@bevilacqua.us
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug