LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pass a script line to a file (https://www.linuxquestions.org/questions/linux-newbie-8/pass-a-script-line-to-a-file-930963/)

rockie321 02-23-2012 07:20 PM

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

savona 02-23-2012 07:34 PM

$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

rockie321 02-23-2012 08:18 PM

Quote:

Originally Posted by savona (Post 4610565)
$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

Thanks for your reply

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

catkin 02-23-2012 08:54 PM

Code:

print "lsmap -all |grep \$i | awk '{print \$1}' >> /tmp/abc" >> /tmp/abc

Dark_Helmet 02-23-2012 08:58 PM

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
EDIT:
catkin beat me to it! Though, he's got a slightly different approach :)

rockie321 02-23-2012 11:42 PM

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

Dark_Helmet 02-24-2012 12:11 AM

Quote:

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.
You mean that:
Code:

print 'lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk \'{print $3}\' >> /tmp/bcd' >> /tmp/abcd
did not work? I would be surprised if it didn't.

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.

catkin 02-24-2012 12:14 AM

Quote:

Originally Posted by rockie321 (Post 4610684)
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

Yes -- by using single quotes but embeded single quotes cannot be escaped so need to be in something like a double quoted string:
Code:

print 'lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk  '"'"'{print $3}'"'"' >> /tmp/bcd'  >> /tmp/abcd

Dark_Helmet 02-24-2012 01:45 AM

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
print 'lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk \'{print $3}\' >> /tmp/bcd';
print "\n";

From the command line:
Code:

$ perl nested_single_quote_print.pl
lsmap -vadapter `head -1 /tmp/abc` |grep "Backing device" | awk '{print $3}' >> /tmp/bcd
$


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."

rockie321 02-24-2012 02:20 AM

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.

catkin 02-24-2012 04:54 AM

Quote:

Originally Posted by Dark_Helmet (Post 4610754)
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...

Right ... and well spotted. Same here:
Code:

c@CW8:~$ help print
printf: printf [-v var] format [arguments]
[snip]
c@CW8:~$ type print
bash: type: print: not found


rockie321 02-24-2012 10:52 AM

Quote:

Originally Posted by catkin (Post 4610864)
Right ... and well spotted. Same here:
Code:

c@CW8:~$ help print
printf: printf [-v var] format [arguments]
[snip]
c@CW8:~$ type print
bash: type: print: not found


I was running it on AIX 6.1
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.