Connecting Tech Pros Worldwide Forums | Help | Site Map

perl with shell dont accept variable

Newbie
 
Join Date: Mar 2008
Posts: 3
#1: Mar 27 '08
Hi

I am using running one perl command using shell scripts, but it won’t take variable
Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. ar="/usr/local/apache/conf"
  3. perl -00ne '/$1/ && print $_' $ar/httpd.conf  > domstat.txt
I am passing following things on command line
./domcheck.sh domainname.com
but perl unable to understand $1 even its not highlight as variable.

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Mar 28 '08

re: perl with shell dont accept variable


Quote:

Originally Posted by winlinfix

Hi

I am using running one perl command using shell scripts, but it won’t take variable

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. ar="/usr/local/apache/conf"
  3. perl -00ne '/$1/ && print $_' $ar/httpd.conf  > domstat.txt
I am passing following things on command line
./domcheck.sh domainname.com
but perl unable to understand $1 even its not highlight as variable.

Where does $1 get defined?
Newbie
 
Join Date: Mar 2008
Posts: 3
#3: Mar 29 '08

re: perl with shell dont accept variable


HI,


$1 = command line variable
it means i am passing

./domcheck.sh domainname.com

$1 = domainname.com
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Mar 29 '08

re: perl with shell dont accept variable


I think you may need to ask on a shell scripting forum, I do not have enough sh experience to know what the problem is. $1 is also a variable used by perl internally, but I am not sure if that would be a problem. Try this and see what happens:

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. perl -e ' print $1;'
just pass your shell script a simple string and see if it gets printed by perl. If not then I think there is a conflict in trying to use the special variable $1 for this purpose.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,573
#5: Mar 29 '08

re: perl with shell dont accept variable


Quote:

Originally Posted by KevinADC

I think you may need to ask on a shell scripting forum, I do not have enough sh experience to know what the problem is. $1 is also a variable used by perl internally, but I am not sure if that would be a problem. Try this and see what happens:

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. perl -e ' print $1;'
just pass your shell script a simple string and see if it gets printed by perl. If not then I think there is a conflict in trying to use the special variable $1 for this purpose.

I have to agree with Kevin. Run his test above and see if it works. My first feeling is that it might cause an issue with Perl as it has a $1 as well, but if his test works, it should work then.

Regards,

Jeff
Newbie
 
Join Date: Mar 2008
Posts: 3
#6: Mar 31 '08

re: perl with shell dont accept variable


Hi,


Thankx for the help but i found another way to solve my problem.
Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2.  
  3. echo "perl -00ne '/$1/ && print \$_' $ar/httpd.conf.bak27marn" > vl.sh
  4. /bin/sh vl.sh
it is working fine.
as perl and shell both are differnt language they do understand different syntax so it would be good to redirect result in one file and run as perl.

Thank you
Reply