For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > backtick variable substitution









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author backtick variable substitution
Hdan

2004-04-22, 2:34 pm

Hi all,

Hopefully someone can help me out with this one.

I have a perl script that runs rsh commands to monitor a remote server.
To minimize time and bandwidth I need to maximize the processing that is
done on the remote server and minimize the number of rsh commands done.

My question is, how can I place a while loop within a system call so
that the loop variable is substituted correctly? Here is an example:


$THE_ERRORS =
`rsh remote_box "cat -n app_error.log | grep -E 'error' |
cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;

Basically, I'm trying to get the error message line and the line before
it returned into $THE_ERRORS. The problem is that $x is not being
interpreted correctly by the head command.

For simplicity, we can take the whole rsh out of the picture and use
this as an example:

$THE_ERRORS =
`cat -n app_error.log | grep -E 'error' |
cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;


Thanks for any suggestions.


Jayakumar Rajagopal

2004-04-22, 2:34 pm

Hi hdan,
try \$ for $.
I did not test it anyway.
HTH,
Jay

-----Original Message-----
From: hdan [mailto:public@austinaction.org]
Sent: Thursday, April 22, 2004 1:13 PM
To: beginners@perl.org
Subject: backtick variable substitution


Hi all,

Hopefully someone can help me out with this one.

I have a perl script that runs rsh commands to monitor a remote server. =

To minimize time and bandwidth I need to maximize the processing that is =

done on the remote server and minimize the number of rsh commands done.

My question is, how can I place a while loop within a system call so=20
that the loop variable is substituted correctly? Here is an example:


$THE_ERRORS =3D
`rsh remote_box "cat -n app_error.log | grep -E 'error' |
cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;

Basically, I'm trying to get the error message line and the line before=20
it returned into $THE_ERRORS. The problem is that $x is not being=20
interpreted correctly by the head command.

For simplicity, we can take the whole rsh out of the picture and use=20
this as an example:

$THE_ERRORS =3D
`cat -n app_error.log | grep -E 'error' |
cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;


Thanks for any suggestions.

Hdan

2004-04-22, 2:34 pm

Thanks. I've tried that, but it didn't work. I've also tried using
exec and system instead of backticks. I tried using eval, too, although
I wasn't sure where to put it in the statement.

-harold


Jayakumar Rajagopal wrote:

>Hi hdan,
>try \$ for $.
>I did not test it anyway.
>HTH,
>Jay
>
>-----Original Message-----
>From: hdan [mailto:public@austinaction.org]
>Sent: Thursday, April 22, 2004 1:13 PM
>To: beginners@perl.org
>Subject: backtick variable substitution
>
>
>Hi all,
>
>Hopefully someone can help me out with this one.
>
>I have a perl script that runs rsh commands to monitor a remote server.
>To minimize time and bandwidth I need to maximize the processing that is
>done on the remote server and minimize the number of rsh commands done.
>
>My question is, how can I place a while loop within a system call so
>that the loop variable is substituted correctly? Here is an example:
>
>
>$THE_ERRORS =
>`rsh remote_box "cat -n app_error.log | grep -E 'error' |
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;
>
>Basically, I'm trying to get the error message line and the line before
>it returned into $THE_ERRORS. The problem is that $x is not being
>interpreted correctly by the head command.
>
>For simplicity, we can take the whole rsh out of the picture and use
>this as an example:
>
>$THE_ERRORS =
>`cat -n app_error.log | grep -E 'error' |
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;
>
>
>Thanks for any suggestions.
>
>
>


tomthumbkop

2004-04-22, 2:51 pm

try using qx and setting it equal to an array in list context.

quote:
Originally posted by Hdan
Thanks. I've tried that, but it didn't work. I've also tried using
exec and system instead of backticks. I tried using eval, too, although
I wasn't sure where to put it in the statement.

-harold


Jayakumar Rajagopal wrote:

>Hi hdan,
>try \$ for $.
>I did not test it anyway.
>HTH,
>Jay
>
>-----Original Message-----
>From: hdan [mailto:public@austinaction.org]
>Sent: Thursday, April 22, 2004 1:13 PM
>To: beginners@perl.org
>Subject: backtick variable substitution
>
>
>Hi all,
>
>Hopefully someone can help me out with this one.
>
>I have a perl script that runs rsh commands to monitor a remote server.
>To minimize time and bandwidth I need to maximize the processing that is
>done on the remote server and minimize the number of rsh commands done.
>
>My question is, how can I place a while loop within a system call so
>that the loop variable is substituted correctly? Here is an example:
>
>
>$THE_ERRORS =
>`rsh remote_box "cat -n app_error.log | grep -E 'error' |
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;
>
>Basically, I'm trying to get the error message line and the line before
>it returned into $THE_ERRORS. The problem is that $x is not being
>interpreted correctly by the head command.
>
>For simplicity, we can take the whole rsh out of the picture and use
>this as an example:
>
>$THE_ERRORS =
>`cat -n app_error.log | grep -E 'error' |
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;
>
>
>Thanks for any suggestions.
>
>
>

Jayakumar Rajagopal

2004-04-22, 3:37 pm

Harold,
(bottom posted)

-----Original Message-----
From: hdan [mailto:public@austinaction.org]
Sent: Thursday, April 22, 2004 1:54 PM
To: Jayakumar Rajagopal
Cc: hdan; beginners@perl.org
Subject: Re: backtick variable substitution


Thanks. I've tried that, but it didn't work. I've also tried using exec and system instead of backticks. I tried using eval, too, although I wasn't sure where to put it in the statement.

-harold


Jayakumar Rajagopal wrote:

Hi hdan,

try \$ for $.

I did not test it anyway.

HTH,

Jay



-----Original Message-----

From: hdan [ mailto:public@austinaction.org]

Sent: Thursday, April 22, 2004 1:13 PM

To: beginners@perl.org

Subject: backtick variable substitution





Hi all,



Hopefully someone can help me out with this one.



I have a perl script that runs rsh commands to monitor a remote server.

To minimize time and bandwidth I need to maximize the processing that is

done on the remote server and minimize the number of rsh commands done.



My question is, how can I place a while loop within a system call so

that the loop variable is substituted correctly? Here is an example:





$THE_ERRORS =

`rsh remote_box "cat -n app_error.log | grep -E 'error' |

cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;



Basically, I'm trying to get the error message line and the line before

it returned into $THE_ERRORS. The problem is that $x is not being

interpreted correctly by the head command.



For simplicity, we can take the whole rsh out of the picture and use

this as an example:



$THE_ERRORS =

`cat -n app_error.log | grep -E 'error' |

cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;





Thanks for any suggestions.



[Jayakumar Rajagopal] +++++++++++++++++++++++
Please let me know if you can run whatever inside the backticks in shell ( unix prompt).
Jay


Hdan

2004-04-22, 3:37 pm

Yes, it works at a ksh prompt:

harold$ cat -n app_system.log.7 | grep -E 'period' | cut -f 1 | while
read x; do head -$x app_system.log.7 | tail -2

15-APR-2004 10:49:03 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to
1082087940.
15-APR-2004 10:49:54 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to
1082087940.
15-APR-2004 12:54:30 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to
1082087940.
15-APR-2004 12:54:59 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to
1082087940.
15-APR-2004 12:55:32 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to
1082087940.


When run from my perl script I get this because $x is null (I think):

harold$ /home/harold/bin/search.pl
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]






Jayakumar Rajagopal wrote:

>Harold,
>(bottom posted)
>
>-----Original Message-----
>From: hdan [mailto:public@austinaction.org]
>Sent: Thursday, April 22, 2004 1:54 PM
>To: Jayakumar Rajagopal
>Cc: hdan; beginners@perl.org
>Subject: Re: backtick variable substitution
>
>
>Thanks. I've tried that, but it didn't work. I've also tried using exec and system instead of backticks. I tried using eval, too, although I wasn't sure where to put it in the statement.
>
>-harold
>
>
>Jayakumar Rajagopal wrote:
>
>Hi hdan,
>
>try \$ for $.
>
>I did not test it anyway.
>
>HTH,
>
>Jay
>
>
>
>-----Original Message-----
>
>From: hdan [ mailto:public@austinaction.org]
>
>Sent: Thursday, April 22, 2004 1:13 PM
>
>To: beginners@perl.org
>
>Subject: backtick variable substitution
>
>
>
>
>
>Hi all,
>
>
>
>Hopefully someone can help me out with this one.
>
>
>
>I have a perl script that runs rsh commands to monitor a remote server.
>
>To minimize time and bandwidth I need to maximize the processing that is
>
>done on the remote server and minimize the number of rsh commands done.
>
>
>
>My question is, how can I place a while loop within a system call so
>
>that the loop variable is substituted correctly? Here is an example:
>
>
>
>
>
>$THE_ERRORS =
>
>`rsh remote_box "cat -n app_error.log | grep -E 'error' |
>
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;
>
>
>
>Basically, I'm trying to get the error message line and the line before
>
>it returned into $THE_ERRORS. The problem is that $x is not being
>
>interpreted correctly by the head command.
>
>
>
>For simplicity, we can take the whole rsh out of the picture and use
>
>this as an example:
>
>
>
>$THE_ERRORS =
>
>`cat -n app_error.log | grep -E 'error' |
>
>cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;
>
>
>
>
>
>Thanks for any suggestions.
>
>
>
>[Jayakumar Rajagopal] +++++++++++++++++++++++
>Please let me know if you can run whatever inside the backticks in shell ( unix prompt).
>Jay
>
>
>
>


Jayakumar Rajagopal

2004-04-22, 3:37 pm



-----Original Message-----
From: hdan [mailto:public@austinaction.org]
Sent: Thursday, April 22, 2004 2:14 PM
To: Jayakumar Rajagopal
Cc: hdan; beginners@perl.org
Subject: Re: backtick variable substitution


Yes, it works at a ksh prompt:

harold$ cat -n app_system.log.7 | grep -E 'period' | cut -f 1 | while read x; do head -$x app_system.log.7 | tail -2

15-APR-2004 10:49:03 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to 1082087940.
15-APR-2004 10:49:54 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to 1082087940.
15-APR-2004 12:54:30 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to 1082087940.
15-APR-2004 12:54:59 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to 1082087940.
15-APR-2004 12:55:32 PI-I-PI_Log_Report_Period
Report period 01/01 00:00 - 04/15 23:59. From time 1072933200 to 1082087940.


When run from my perl script I get this because $x is null (I think):

harold$ /home/harold/bin/search.pl
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]
Usage: head [-Count | -n Number | -c Number] [File...]








Jayakumar Rajagopal wrote:

Harold,

(bottom posted)



-----Original Message-----

From: hdan [ mailto:public@austinaction.org]

Sent: Thursday, April 22, 2004 1:54 PM

To: Jayakumar Rajagopal

Cc: hdan; beginners@perl.org

Subject: Re: backtick variable substitution





Thanks. I've tried that, but it didn't work. I've also tried using exec and system instead of backticks. I tried using eval, too, although I wasn't sure where to put it in the statement.



-harold





Jayakumar Rajagopal wrote:



Hi hdan,



try \$ for $.



I did not test it anyway.



HTH,



Jay







-----Original Message-----



From: hdan [ mailto:public@austinaction.org]



Sent: Thursday, April 22, 2004 1:13 PM



To: beginners@perl.org



Subject: backtick variable substitution











Hi all,







Hopefully someone can help me out with this one.







I have a perl script that runs rsh commands to monitor a remote server.



To minimize time and bandwidth I need to maximize the processing that is



done on the remote server and minimize the number of rsh commands done.







My question is, how can I place a while loop within a system call so



that the loop variable is substituted correctly? Here is an example:











$THE_ERRORS =



`rsh remote_box "cat -n app_error.log | grep -E 'error' |



cut -f 1 | while read x; do head -$x app_error.log | tail -2; done"`;







Basically, I'm trying to get the error message line and the line before



it returned into $THE_ERRORS. The problem is that $x is not being



interpreted correctly by the head command.







For simplicity, we can take the whole rsh out of the picture and use



this as an example:







$THE_ERRORS =



`cat -n app_error.log | grep -E 'error' |



cut -f 1 | while read x; do head -$x app_error.log | tail -2; done`;











Thanks for any suggestions.







[Jayakumar Rajagopal] +++++++++++++++++++++++

Please let me know if you can run whatever inside the backticks in shell ( unix prompt).

Jay





[Jayakumar Rajagopal] The same works well for me. It should work for you. You may try grep "-c -i" instead of "-E" . ( My machine does not support -E option ) . also try giving 'echo $x' just before 'head'. You will understand why the problem came. you must use \$x for $x, and -\$x for -$x.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com