LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2012, 08:20 PM   #1
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Rep: Reputation: 1
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
 
Old 02-23-2012, 08:34 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
$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
 
Old 02-23-2012, 09:18 PM   #3
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by savona View Post
$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

Last edited by rockie321; 02-23-2012 at 09:21 PM.
 
Old 02-23-2012, 09:54 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Code:
print "lsmap -all |grep \$i | awk '{print \$1}' >> /tmp/abc" >> /tmp/abc
 
Old 02-23-2012, 09:58 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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
 
Old 02-24-2012, 12:42 AM   #6
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
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
 
Old 02-24-2012, 01:11 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.
 
Old 02-24-2012, 01:14 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by rockie321 View Post
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
 
Old 02-24-2012, 02:45 AM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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."
 
Old 02-24-2012, 03:20 AM   #10
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
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.
 
Old 02-24-2012, 05:54 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Dark_Helmet View Post
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
 
Old 02-24-2012, 11:52 AM   #12
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by catkin View Post
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
 
1 members found this post helpful.
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
how to pass command-line parameter to shell script? Kropotkin Linux - Newbie 12 07-25-2011 10:24 AM
[SOLVED] pass source file into awk script vinaytp Linux - Newbie 11 07-15-2010 04:38 AM
[SOLVED] pass file-name as command line argument vikas027 Programming 2 10-15-2008 12:52 AM


All times are GMT -5. The time now is 04:58 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration