Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell functions and awk

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Data that looks like this stored in a
shell var:
ide data: /dev/hda MaxMultCout=16 time=35s
throughput: 6.97 MB\s

I filter the data so:
parser() {

echo $vallines | awk ' {
if ($0) {
gsub(/.*: \/.*:/, "", $0)
print
}
}'
return
}

I have to hardcode the var name because if
pass the parameter to the shell function
like this:
parser() {
_val=$1

echo $_val | awk code

}
Then , of course I only get the
ide data line as input, which really is bad.

As it is I will have to rewrite this code
for each different variable input....

Any ideas? I would like to be able to
parameterize the shell function if at all possible.

TIA
MD
 
How do CALL the "parser" function?

[I'd assume you're using ksh]

It should be:
parse "$vallines"

and then you should be able to use your construct inside the "parse" function body itself:
_val=$1


vlad
 
Thanks for responding vlad..

Just like any other sheel function it is called like this:

parser $var; the variable is $1 ; all standard bash stuff.

The var is derived:
for x in `proc`
do
case $x in


pat1) eval $x
#the string is already formatted
#MaxMultSect=num
#so it is trivial to make it a shell
#var
#sanity
if [ ! -z "$var" ]
then
etc..
But this is off subject..
Yes, I do what you say, and it seemed to me it should work,
but the var only contains the first "/dev/hda" of the var.
This behavior is not any good for this script.
I need all components of the stored var not just the first
delimited one...

Any ideas?

 

here's the script that I've ran

------------------- script ----------------------

#!/bin/ksh

var='ide data: /dev/hda MaxMultCout=16 time=35s throughput: 6.97 MB\s'

parser() {

echo ${1} | awk '{
if ($0) {
gsub(/.*: \/.*:/, "", $0)
print
}
}'
return
}

parser "${var}"

------------------- script ----------------------

and the output is:
6.97 MB\s


Is that something that's expected? What's the issue?

vlad
 
Yes, That is what I wanted.
Maybe it is just bash.
Instead , in the script when I run under
sh -x scriptname , i can see the, first element of
the variable string(before first whitespace) being taken
for the whole variable string.
You do not have this behavior is ksh?

Thank You
 
nope, i don't

I don't even have that behavior if I run as YOU run it, in other words using Bourne shell as an interpreter [btw, it Bourne shell, not Bash shell].

My env is Solaris2.7

puzzlin'..........

vlad
 
actually bourne again shell, but okay, you win..
Yeah, Linux 2.2, It works in the shell..
not from the script, with the same code...
Totally bizarre.
 
Try using double quotes instead of single quotes when
you assign to var:

var="ide data: /dev/hda MaxMultCout=16 time=35s throughput: 6.97 MB\s"

HTH


flogrr
flogr@yahoo.com

 
Thanks flogrr..
This info is generated via a cmd sub:
output=`awk ' {
if ($0 ~ /string/) {
etc..
}

This introduces the correct format for an eval that
uses the string=val format produced to "dynamically" create a shell var...quoting the string is impossible before the command sub and afterwards requoting the info in the shell var should not be necessary. Any whitespace and the script stops reading the var at that point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top