LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-15-2007, 05:29 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
Trying to write a perl script that will print shell variable


Hello all,


I am trying to write perl script that will print shell script to file. below is example.pl script

#/usr/bin/perl

print<<<EOF
#!/bin/bash

OUTPUT=eth0
INPUT=eth1
IPTABLES=/sbin/iptables


$IPTABLES -A INPUT -p tcp -i $OUTPUT -s 0/0 to 0/0 -j ACCEPT
EOF

When I run the script
root# ./example.pl >firewall.sh

it prints the file like

#!/bin/bash

OUTPUT=eth0
INPUT=eth1
IPTABLES=/sbin/iptables

-A INPUT -p tcp -i -s 0/0 to 0/0 -j ACCEPT


HERE $ are removed by the scripts. What is the correct script inorder to print the $ in perl?


Any help?
 
Old 04-15-2007, 06:07 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

The example given has more then one mistake, here's one that will work:
Code:
#!/usr/bin/perl

print <<"EOF";
#!/bin/bash

OUTPUT=eth0
INPUT=eth1
IPTABLES=/sbin/iptables


\$IPTABLES -A INPUT -p tcp -i \$OUTPUT -s 0/0 to 0/0 -j ACCEPT
EOF
Changes made:
- Include a proper hasbang (no ! in your example),
- << instead of <<<,
- first (!) EOF needs the ; at the end,
- the first EOF needs double quotes,
- special chars need to be escaped inside your here document.

Hope this helps..

Last edited by druuna; 04-15-2007 at 06:35 AM.
 
Old 04-16-2007, 09:02 AM   #3
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Dual boot :: Slackware 14.1 64bit multilib | Kali linux 64bit multi lib
Posts: 192

Rep: Reputation: 42
This one doesn't need escapes.

Code:
#!/usr/bin/perl

print q(#!/bin/bash

OUTPUT=eth0
INPUT=eth1
IPTABLES=/sbin/iptables


$IPTABLES -A INPUT -p tcp -i $OUTPUT -s 0/0 to 0/0 -j ACCEPT
);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 09:01 AM
Write output or command to variable (shell) kernel_geek Programming 9 03-12-2007 05:45 AM
Perl wont print my Variable!!! socceroos Programming 10 03-15-2006 06:04 PM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 04:47 AM
Write shell commands to print ricki Programming 2 07-31-2005 04:52 PM


All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration