Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1946
  • Last Modified:

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/old"
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?
0
slipstream81
Asked:
slipstream81
1 Solution
 
bryanhCommented:
I scratched my head over this one quite a bit until I realized that my browser displays forward and backward quotes the same, and 2 single quotes the same as a double quote.

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.
0
 
slipstream81Author Commented:
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/old"
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
0
 
skianCommented:

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
0
 
jmcgOwnerCommented:
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
0
 
moduloCommented:
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now