TIP: How to make nested back ticks work in shell programs

Get answers here.

Moderators: BBear, theunknownhost, flaguy

Post Reply
User avatar
Sapphyre
Official Test Penquin
Posts: 3337
Joined: Tue Nov 19, 2002 2:50 am
Location: Canada

TIP: How to make nested back ticks work in shell programs

Post by Sapphyre » Fri Oct 19, 2007 5:14 pm

I have this line to run - it runs fine on the command line

Code: Select all

for EACH in `grep 07-10-19 /path/to/cgi.log|grep foo|cut -d':' -f6|grep -oE "[0-9a-z]+/"|cut -d'/' -f1|sort|uniq|sort`; do grep -h 'POST /*/cgi-bin/bar.pl' /home*/$EACH/path/to/foobar|cut -d'-' -f1|sort|uniq -c|sort; done;


I need to get those results into a variable in a shell script
IPADDRESSES=` code `;

but that would lead to double backticks
IPADDRESSES=`for EACH in `code`; do foo; done;`
which wouldn't work - I found a source saying to use $( ) in place of the inner backticks.

So you do this instead
IPADDRESSES=`for EACH in $(code); do foo; done;`

Code: Select all

IPADDRESSES=`for EACH in $(grep 07-10-19 /path/to/cgi.log|grep foo|cut -d':' -f6|grep -oE "[0-9a-z]+/"|cut -d'/' -f1|sort|uniq|sort); do grep -h 'POST /*/cgi-bin/bar.pl' /home*/$EACH/path/to/foobar|cut -d'-' -f1|sort|uniq -c|sort; done;`


You can use a single pair of backticks, innermost or outermost, with multiple nesting sets of $( )

HTH!

(Sorry folks, I edited my original question into a tip because this does indeed work. My problem was a missing backtick further up in my script causing everything to be misinterpreted/out of whack)

User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

Post by Arf » Fri Oct 19, 2007 10:09 pm

Sapph,
I think I understand. I think that I could use this. But why are you using such a complex example? My gosh, girl, my brain hurts.

m2
Hard Drive Crasher
Posts: 767
Joined: Sun Nov 21, 2004 2:19 pm
Location: Lexington, KY

Post by m2 » Sat Oct 20, 2007 12:29 pm

Arf wrote:My gosh, girl, my brain hurts.


I searched the manual for "overactive pain receptors in uppermost extremity". Apparently it's a known bug but there's a temporary workaround. Reduce the default value for these two settings in the code:

Code: Select all

$head2wallVelocity = [some value];
$bangHeadFrequency = [some value];


m2

User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

Post by Arf » Sat Oct 20, 2007 4:03 pm

Loved it M2. LOL :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest