Passing a string Varible from AWK to a sh Script

Passing a string Varible from AWK to a sh Script

Post by Larry Jam » Sat, 27 Aug 1994 22:08:31



     Can someone tell me how to pass a string varible from AWK to the
shell.  The following is an example of a shell script I'm having
problems with:

Shell script:
--------------------------------------------------
#!/bin/sh

# Line #1.  This is the line I'm having problems with:
# processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}{'theline='print $0}' -`

# Line #2   This is the working line, but I can't get the parameter $0
#           into the shell program.
processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}' -`

echo "Looking for: "          $1
echo "The process number is: "$processno
echo "The line found is: "    $theline
--------------------------------------------------

     I would like for the shell script to have the full line that AWK
processes.
     Thanks in advance for any suggestions or comments.

                                -- Larry
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


Computer Consultant/Programmer                    716-685-1865-ext 550  Voice

 
 
 

Passing a string Varible from AWK to a sh Script

Post by Bill Parret » Wed, 31 Aug 1994 04:56:35


:      Can someone tell me how to pass a string varible from AWK to the
: shell.  The following is an example of a shell script I'm having
: problems with:
:  
: Shell script:
: --------------------------------------------------
: #!/bin/sh

: # Line #1.  This is the line I'm having problems with:
: # processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}{'theline='print $0}' -`

: # Line #2   This is the working line, but I can't get the parameter $0
: #           into the shell program.
: processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}' -`

: echo "Looking for: "          $1
: echo "The process number is: "$processno
: echo "The line found is: "    $theline
: --------------------------------------------------

:      I would like for the shell script to have the full line that AWK
: processes.

        It seems to me that you are trying to do two completely dif-
     ferent things at the same time.  The awk program is powerful but
     it cannot do everything -- especially all at the same time. :-)

        Are you trying to do something like the following *two step*
     procedure?:

   theLine=`ps -ef | awk '$8 ~ /'$1'/ {print $0}'`
   processno=`echo $theLine| awk '$8 ~ /'$1'/ {print $2}{'theline='print $0}'`

        I hope this helps.  May you grok in fullness ...

Bill--

+ William A. (Bill) Parrette -- _/_/_/ _/_/_/ _/_/_/    _/_/ ---------------+

|4000 Executive Pk. Dr., #310   _/     _/   _/   _/ _/      Onsite & Public |
|  Cincinnati, OH 45241-4007   _/     _/   _/   _/ _/   _/ Class Information|
+------- 800-752-5448 ----- _/_/_/   _/   _/_/_/    _/_/ -------------------+

 
 
 

Passing a string Varible from AWK to a sh Script

Post by Dr A.P. Mathas/100 » Tue, 30 Aug 1994 20:08:31


%%     Can someone tell me how to pass a string varible from AWK to the
%% shell.  The following is an example of a shell script I'm having
%% problems with:
%%
%% Shell script:
%% --------------------------------------------------
%% #!/bin/sh
%%
%% # Line #1.  This is the line I'm having problems with:
%% # processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}{'theline='print $0}' -`
%%
%% # Line #2   This is the working line, but I can't get the parameter $0
%% #           into the shell program.
%% processno=`ps -ef | awk '$8 ~ /'$1'/ {print $2}' -`
%%
%% echo "Looking for: "          $1
%% echo "The process number is: "$processno
%% echo "The line found is: "    $theline

First I'll inflict a few comments:
  (i). Just because tar needs a filename of "-" when you pipe into it does
       not mean that awk goes; it doesn't.
 (ii). I'm curious as to what the -f option to ps does as I've not seen it
       on any system.
(iii). What do you want to happen if ps gives you more than one matching
       line? I've just taken the first one (this is what the head -1 is
       doing).
 (iv). As usual, life would be simpler if you used nawk or gawk. Perl *s
       would no doubt claim that perl should be used; but as I'm not one of them
       I can't admonish you with this.

And now a solution; there may be better ones:

#!/bin/sh

pro=$1
set -- `ps e | awk '$8 ~ /'"$pro"'/ {printf $0}' | head -1`

pronum=$2
linenum="$*"
echo "Looking for: "$pro
echo "The process number is: "$pronum
echo "The line found is: "$linenum"<"

---

Andrew Mathas

Wisdom is the principal thing;
therefore get wisdom; and with
thy getting get understanding.
                 Proverbs iv, 7

 
 
 

1. Pass Awk Varibles To Serach!!!(Me Stupid)

Hi,

Sorry to bother you but........

Well I'm tring this simple script:

awk -F, '/$name/{printf("%s %s",$1,$2)' name=RICH

OK I got a comma delimited database. I know if I replace the $name bit with
RICH then it will grab everything in the first field that has RICH in it...
cool, but what I want it to supply a variable to awk to seach for a different
thing every time depending what the name varible is. I've tried:

awk -F, '/`echo $name`/{printf("%s %s",$1,$2)'

Where name is a environmen variable. I'm trying to get an environment variable
into awk so it searches on the first field according to the environment
variable. I know that in the first script it will take the name varibale, but
only in the main bit of the code, but not in the search!!!!

HELP!

Cheers

Rich

--
"Tis after death that we measure men."
James Barron Hope

2. new router

3. How to pass shell varibles into awk

4. Viewing 180 Col Reports on Terminals

5. Execute a sh script under perl and sh: sh script; perl script?

6. UPS

7. Apache 1.3.3 - CGI scripts - form varibles not passed

8. CB PCI-DAS1602 cannot access board

9. awk, sh and string variables -- weird problem via /etc/services!

10. Passing values generated in awk/nawk back to script

11. /bin/sh passing arguments to sourced script

12. Question: passing $var and "$var" into AWK from script

13. passing arguments from awk to another script