quoting complex strings in Korn Shell problem

quoting complex strings in Korn Shell problem

Post by Kate Julif » Tue, 04 Apr 2000 04:00:00



I am new to the Korn shell and rusty on UNIX.
I need to fix the following piece of code
idlettys='finger|awk if [[ $(expr substr "$0" 1 3)
= $testch ]] then print $(expr substr "$0" 1 3) fi'
I realize I have to somehow quote the string to be processed by awk but
cannot see how.
I've tried a number of combinations but am getting nowhere. Thanks in
advance,
Kate

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

quoting complex strings in Korn Shell problem

Post by Real » Wed, 05 Apr 2000 04:00:00



Quote:> I am new to the Korn shell and rusty on UNIX.
> I need to fix the following piece of code
> idlettys='finger|awk if [[ $(expr substr "$0" 1 3)
> = $testch ]] then print $(expr substr "$0" 1 3) fi'
> I realize I have to somehow quote the string to be processed by awk but
> cannot see how.
> I've tried a number of combinations but am getting nowhere. Thanks in
> advance,
> Kate

If you want to store the results of a command in a variable, use backticks
(`). But in ksh you can also enclose the command in $( and ) characters.
Easier to read and it has the same effect.

About your little script, I think you want to do something like this;
   idlettys=$( finger | grep "^${testch}" | cut -c 1-3 )"

For more information, see "man ksh" and search for "Command Substitution".

Good luck,
Real

 
 
 

quoting complex strings in Korn Shell problem

Post by qazw.. » Thu, 06 Apr 2000 04:00:00




Quote:> I am new to the Korn shell and rusty on UNIX.
> I need to fix the following piece of code
> idlettys='finger|awk if [[ $(expr substr "$0" 1 3)
> = $testch ]] then print $(expr substr "$0" 1 3) fi'
> I realize I have to somehow quote the string to be processed by awk
but
> cannot see how.

Nothing too Kornshellish about this. This is simply an "awk" problem.

You can pass environment variables into awk via one of two ways. This,
of course, depends on your version of awk:

Method 1
    awk -v myvar=$myvar '{print $0}' myfile

Method 2
    awk '{print $0}' myvar=$myvar myfile

So, (once you figure out which method your version of awk uses) you now
know how to pass environment variables into awk.

Now, your next problem is that all awk programs must start with "{" and
end in "}". You didn't have that with your program. However, to prevent
the shell from interpreting the braces, you have to quote them.

Next, the format for if statements are:
    if(<condition>){<command lines>}

I am not 100% sure what your program is doing. My finger command puts
the tty in the third column of the output and idle time in the fourth
column, so I used $3 which is the tty name. I also placed your program
on multiple lines, so it is easier to see the awk program. There is no
reason why it cannot be crammed into a single line:

    idlettys=$(finger|
        awk -v idletty=$idletty '
        {
            if ($3 = idletty)
            {
                print $3
            }
        }')

If you have problems getting awk to read your Kornshell variables,
there is a third method of passing environmental variables into awk..

You use double quotes instead of single quotes. However, when you come
across an awk column variable, you must close the double quotes, open a
single quote, put the variable you want, close the single quote, then
open the double quote again. See the example below:

    idlettys=$(finger|
        awk "
        {
            if ("'$3'" = $idletty)
            {
                print "'$3'"
            }
        }")

This way, you can put the environment variable "$idletty" right in the
awk script.

Remember that awk is its own scripting language separate from Kornshell
(like Perl or Tcl), so you cannot mix Kornshell syntax into your awk
scipt. (Yes, I know it's technically feasible, but I've never seen it
done).

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

quoting complex strings in Korn Shell problem

Post by Ken Pizzi » Sat, 08 Apr 2000 04:00:00



>Now, your next problem is that all awk programs must start with "{" and
>end in "}". You didn't have that with your program. However, to prevent
>the shell from interpreting the braces, you have to quote them.

Hmmm...  Then you don't consider this to be an awk program?
  awk '$5 ~ /foo/'

Quote:>    idlettys=$(finger|
>        awk -v idletty=$idletty '
>        {
>            if ($3 = idletty)
>            {
>                print $3
>            }
>        }')

Awk scripts consist of rules of the form "PATTERN { ACTION }";
the above can be simplified by making use of this:
  idlettys=$(finger |
             awk -v idletty="$idletty" '$3 == idletty { print $3 }')

Quote:>If you have problems getting awk to read your Kornshell variables,
>there is a third method of passing environmental variables into awk..

>You use double quotes instead of single quotes. However, when you come
>across an awk column variable, you must close the double quotes, open a
>single quote, put the variable you want, close the single quote, then
>open the double quote again.

Or, more simply, just preceed the $ of the column variable with a \ .

Quote:> See the example below:

>    idlettys=$(finger|
>        awk "
>        {
>            if ("'$3'" = $idletty)
>            {
>                print "'$3'"
>            }
>        }")

idlettys=$(finger | awk "\$3 == \"$idletty\" { print \$3 }"

Quote:>This way, you can put the environment variable "$idletty" right in the
>awk script.

Though if it is not a number you'll need to put it in "s, as I
did above.

Quote:>Remember that awk is its own scripting language separate from Kornshell
>(like Perl or Tcl), so you cannot mix Kornshell syntax into your awk
>scipt. (Yes, I know it's technically feasible, but I've never seen it
>done).

You just did it above with your "interpolate the variable right
into the script".

                --Ken Pizzini

 
 
 

1. quoted strings within quoted strings

I need to send a character string which contains an embedded character
string to another host using the rsh command.  The argument of the rsh
command must itself be enclosed in quotes.  My code (which must be csh
code) is the following:#

    rsh usdbs 'echo "select * from view_ae where protocol = ?G8808?" | isql dictionary'

where the question marks around the G8808 character string are place-
holders for a third level of delimiter.  I've tried to use the escape
symbol (\) in various locations with no luck.  Because the command line
is actually issued from within an application program, it must be a
one-liner and must be C-shell.

Does anyone know how to do this?  Thanks in advance.

RLB

2. Can I boot my Linux box without a keyboard?

3. Korn Shell: Using sed to replace string with multiline string?

4. Contract to Perm Opportunity in Hurst, TX

5. Need help using complex SORT in Korn Shell

6. Network monitor packet parser

7. 82801DB ide patch worked

8. Need Help writing a complex Korn Shell Script

9. best korn shell resources and is there a korn shell faq

10. Q: sh: Can I quote '"' in a double quoted string?

11. strings with while space and the korn shell