Pass a script line to a file
Hi,
I have a line of script which needs to be printed to a file to create a new script. lsmap -all |grep $i | awk '{print $1}' >> /tmp/abc But when I do a print "lsmap -all |grep $i | awk '{print $1}' >> /tmp/abc" >> /tmp/bcd it reads the $i inside the quotes and displays errors. So how can I have the system ignore what is given in quotes and just print it into a file Thanks |
$i is a variable that is not set.
Is this just part of the script or is this the only line? lsmap -all |grep $i The above command will not work because bash doesn't know what $i is. You never declared it. Here is some good info on variables: http://www.google.com/url?sa=t&rct=j...cwdlO43PGRzMSw |
Quote:
No, I will be passing values for $i via command line. What I need to to have is file /tmp/bcd to contain the following cat /tmp/bcd lsmap -all |grep $i I have few other commands that I need to pass to /tmp/bcd as well. echo "lsmap -all |grep $i" >> /tmp/bcd doesn't work so does print. So how can I do it |
Code:
print "lsmap -all |grep \$i | awk '{print \$1}' >> /tmp/abc" >> /tmp/abc |
You didn't say what scripting language you're using, but I'm assuming perl.
You can prevent perl from trying to expand the $i reference by using single quotes (') around your statement rather than double quotes ("). Now, your statement has some nested single quotes that are used to give awk a script. To prevent perl from getting confused, you need to escape the nested single quotes by using a backslash (\). For instance: Code:
print 'lsmap -all |grep $i | awk \'{print $1}\' >> /tmp/abc' >> /tmp/bcd catkin beat me to it! Though, he's got a slightly different approach :) |
Thanks Guys. Is there a way that I can ask a script to ignore everything in a line and print it directly.
For eg, print "lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk '{print $3}' >> /tmp/bcd" >> /tmp/abcd I tried both your suggestions on the above command but its not able to cover them all. Help |
Quote:
Code:
print 'lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk \'{print $3}\' >> /tmp/bcd' >> /tmp/abcd Please explicitly confirm that your scripting language is perl. I would hate to give suggestions for one language if you are using another. And in short, no, there is no magic bullet to tell a script to ignore a line and print it verbatim. The closest thing (again, assuming perl) would be to use single quotes. But again, you would need to escape any nested single quotes, double escape double-nested single quotes, and so on. |
Quote:
Code:
print 'lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk '"'"'{print $3}'"'"' >> /tmp/bcd' >> /tmp/abcd |
Ok, I'm a little confused. So I may just "bow out."
On my machine, this works for perl: nested_single_quote_print.pl: Code:
#!/usr/bin/perl Code:
$ perl nested_single_quote_print.pl If the script trying to print is in bash, then catkin is correct (about not being able to escape internal single quotes). Though, my system does not have a "print" command (aside from a mailcap-related command). Nor does my bash have a builtin command for print. The only thing similar would be printf. So that's why I'm confused. My bash does not have "print" which implies another language... Or maybe I'm just too tired. Either way, it might be best for me to "bow out." |
Thank you very much catkin & Dark_Helmet
Both your suggestions worked. Dark's on Perl and Catkin's on KSH. Sorry did not mention the scripting language I was using which confused you Dark. Thanks Again. |
Quote:
Code:
c@CW8:~$ help print |
Quote:
appserv:/home/test$type print print is a shell builtin appserv:/home/test$echo $SHELL /usr/bin/ksh appserv:/home/test$print hello hello |
All times are GMT -5. The time now is 01:40 AM. |