slipstream81
asked on
Bash scripting - escaping variable substitute in eval
In the following script:
#!/bin/sh
# file count
lc=`cat integrit.log |grep 'changed:' | awk '{print $2}' | wc -l`
# run count
rc=1
odir="/export/export/www/o ld"
while [ $lc -ge $rc ]; do
curfile=`eval cat integrit.log | grep 'changed:' | awk 'NR==$rc {print $2}'`
oldfile=$odir$curfile
echo "Changes in $curfile compared to $oldfile"
diff $curfile $oldfile
rc=`expr $rc + 1`
done
I'd like to have the $2 in the awk script (curfile=..) escaped, but the $rc substituted.
escaping like \$2 doesn't seem to work, nor does curfile=`cat integrit.log | grep 'changed:' | awk 'NR==$(eval $rc) {print $2}'`
Help?
#!/bin/sh
# file count
lc=`cat integrit.log |grep 'changed:' | awk '{print $2}' | wc -l`
# run count
rc=1
odir="/export/export/www/o
while [ $lc -ge $rc ]; do
curfile=`eval cat integrit.log | grep 'changed:' | awk 'NR==$rc {print $2}'`
oldfile=$odir$curfile
echo "Changes in $curfile compared to $oldfile"
diff $curfile $oldfile
rc=`expr $rc + 1`
done
I'd like to have the $2 in the awk script (curfile=..) escaped, but the $rc substituted.
escaping like \$2 doesn't seem to work, nor does curfile=`cat integrit.log | grep 'changed:' | awk 'NR==$(eval $rc) {print $2}'`
Help?
ASKER
I solved this on my own, just used a more logical method. Shell scripting is not my forte either ;)
#!/bin/sh
odir="/export/export/www/o ld"
for n in `cat integrit.log | grep 'changed:' | awk '{print $2}'`; do
curfile=$n
oldfile=$odir$curfile
echo "Changes in $curfile compared to $oldfile"
diff $curfile $oldfile
done
#!/bin/sh
odir="/export/export/www/o
for n in `cat integrit.log | grep 'changed:' | awk '{print $2}'`; do
curfile=$n
oldfile=$odir$curfile
echo "Changes in $curfile compared to $oldfile"
diff $curfile $oldfile
done
You solved it another way but here is a possible solution
to evaluate $rc but not $2 : concatenate a double-quoted string and a single quoted-string, ie :
rc=somevalue
somecommand | awk "NR==$rc"' {print $2}'
Stephane
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
PAQ with points refunded
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
PAQ with points refunded
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is generally easier to read if you use the more modern $(command) syntax instead of `command` (backquotes). This syntax is also nestable.
I don't know the solution to the escaping problem; shell language is not my forte. I just thought I would add the above.