?
Solved

Bash scripting - escaping variable substitute in eval

Posted on 2003-03-03
5
Medium Priority
?
1,940 Views
Last Modified: 2011-09-20
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
Comment
Question by:slipstream81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:bryanh
ID: 8062394
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
 

Author Comment

by:slipstream81
ID: 8063295
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
 
LVL 1

Expert Comment

by:skian
ID: 8072098

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
 
LVL 20

Expert Comment

by:jmcg
ID: 10101952
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10156953
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question