Using PRINT to build a script in k-shell

Using PRINT to build a script in k-shell

am 28.11.2007 20:58:34 von JAW

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?

Re: Using PRINT to build a script in k-shell

am 28.11.2007 21:51:55 von Bill Marcum

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?
cat <<\END >>$WSCRIPT
if [ "`echo $LINE | awk -F: '{print \$3}' -`" = "Y" ] ; then
END

Re: Using PRINT to build a script in k-shell

am 28.11.2007 22:18:45 von cfajohnson

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

Re: Using PRINT to build a script in k-shell

am 28.11.2007 22:38:18 von mallin.shetland

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

Re: Using PRINT to build a script in k-shell

am 28.11.2007 22:59:28 von Michael Tosch

Bill Marcum wrote:
> 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?
> 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

Re: Using PRINT to build a script in k-shell

am 29.11.2007 03:42:11 von brian_hiles

JAW 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/lehre/unixffb/quoting-guide.h tml

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

=Brian

Re: Using PRINT to build a script in k-shell

am 02.12.2007 03:31:34 von cfajohnson

On 2007-11-28, Michael Tosch wrote:
>
>
> Bill Marcum wrote:
>> 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?
>> 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

UUOC.


--
Chris F.A. Johnson, author
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