Unix Shell - Using PRINT to build a script in k-shell

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Using PRINT to build a script in k-shell





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Using PRINT to build a script in k-shell
JAW

2007-11-28, 7:34 pm

print ' if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ;
then' >> $WSCRIPT

I am writing a script that will generate a script based on an input
file.

I want the above command to print a line that looks like the below:

if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then


However, the below is the output:

if [ "`echo $LINE | awk -F: {print $3} -`" = "Y" ] ; then


It is missing the two apostrophes around the braces as the above
shows.

Any tips on getting this to work?
Bill Marcum

2007-11-28, 7:34 pm

On 2007-11-28, JAW <jwilliam@aglresources.com> wrote:
>
>
> print ' if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ;
> then' >> $WSCRIPT
>
> I am writing a script that will generate a script based on an input
> file.
>
> I want the above command to print a line that looks like the below:
>
> if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then
>
>
> However, the below is the output:
>
> if [ "`echo $LINE | awk -F: {print $3} -`" = "Y" ] ; then
>
>
> It is missing the two apostrophes around the braces as the above
> shows.
>
> Any tips on getting this to work?

cat <<\END >>$WSCRIPT
if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then
END
Chris F.A. Johnson

2007-11-28, 7:34 pm

On 2007-11-28, JAW wrote:
>
>
> print ' if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ;
> then' >> $WSCRIPT
>
> I am writing a script that will generate a script based on an input
> file.
>
> I want the above command to print a line that looks like the below:
>
> if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then
>
>
> However, the below is the output:
>
> if [ "`echo $LINE | awk -F: {print $3} -`" = "Y" ] ; then
>
>
> It is missing the two apostrophes around the braces as the above
> shows.
>
> Any tips on getting this to work?


You cannot enclose single quotes inside single quotes. Use double
quotes and escape the double quotes inside.

printf "%s\n" "if [ \"\`echo $LINE | awk -F: '{print \$3}' -\`\" = \"Y\" ] ; then"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
mallin.shetland

2007-11-28, 7:34 pm

Chris F.A. Johnson scrisse:

> You cannot enclose single quotes inside single quotes...


you have to escape it _outside_ the single quotes

print ' if [ "`echo $LINE | awk -F: '''{print \$3}''' -`" = "Y" ] ;
then' >> $WSCRIPT


PS ''' single quote - backslash - single quote - single quote

Michael Tosch

2007-11-28, 7:34 pm

Bill Marcum wrote:
> On 2007-11-28, JAW <jwilliam@aglresources.com> wrote:
> cat <<\END >>$WSCRIPT
> if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then
> END


Yes, a here document is perfect here.
BTW the generated code is safer and better readable like this:

cat <<\END >>$WSCRIPT
if echo "$LINE" | awk -F: '$3=="Y"' | grep -q . ; then
END

--
Michael Tosch @ hp : com
bsh

2007-11-29, 1:36 am

JAW <jwill...@aglresources.com> wrote:
> ...
> Any tips on getting this to work?


Non-trivial quoting is usually the first hurdle to overcome in
shell scripting, just like pointers are in C, and the object-
oriented paradigm is in Java/C++.

These two documents cover the same material, stressing applications
and parsing context, respectively:

http://www.grymoire.com/Unix/Quote.html
http://www.mpi-inf.mpg.de/~uwe/lehr...ting-guide.html

You don't have to read all at once of any one file.

=Brian


Chris F.A. Johnson

2007-12-02, 1:36 am

On 2007-11-28, Michael Tosch wrote:
>
>
> Bill Marcum wrote:
>
> Yes, a here document is perfect here.
> BTW the generated code is safer and better readable like this:
>
> cat <<\END >>$WSCRIPT
> if echo "$LINE" | awk -F: '$3=="Y"' | grep -q . ; then
> END


UUOC.


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com