Operating System - HP-UX
1831388 Members
3489 Online
110025 Solutions
New Discussion

Feedback requested on output format for scripting

 
marie-noelle jeanson_1
Trusted Contributor

Feedback requested on output format for scripting

Hi,

HP is designing a new function for an HP-UX command. This option would allow to pass a number of attribute names to the command, and the command output would display the associated values. This functionality would mostly be used in script mode, so we are looking for feedback on what is the best format to manage the output from a script.

Command:
cmd get -a -a ... -a

Output choices:

1) values only:
: : ... :

2) attr names + values:
= : = : ...

3) attr names then values on a new line:
: : .. :
: : ... :

4) other proposal?

which is best and why?

Thanks!

Marie-Noelle
9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: Feedback requested on output format for scripting

I would personally prefer option 2, BUT I would prefer that each attribute be on a separate line.

=
=

Also, what happens if a particular attribute doesn't exist or has no value? Hopefully there is some sort of place holder or string to indicate each case?

A variation on option2 may also work if each value is on a separate line.




As long as a non-existant attribute or null attribute value is indicated.

My 2nd option can be handy for scripting if doing:

while read LINE
do
DO YOUR STUFF
done < cmd get -a attr1 -a attr2 -a attr3

or something to that effect. That way you have each value on a separate line and can act accordingly if needed.
Fred Ruffet
Honored Contributor

Re: Feedback requested on output format for scripting

I would go for the second form. If it used by script, I imagine it has to be easily parsed, and it seems to me that second form will be the best for that. Maybe separated by newlines instead of ':', but that's not really important.

Regards,

Fred


--

"Reality is just a point of view." (P. K. D.)
A. Clay Stephenson
Acclaimed Contributor

Re: Feedback requested on output format for scripting

My choice would be all on one line for easy processing by Perl or awk and I would only want the values BUT the separator should be a because that is nicely visible whitespace but unlike ':', ',', or , would be an extremely unlikely character to be embedded within a value.

...

It's also trivially easily to parse the line with Perl, awk, or the shell with as the field separator. is always my weapon of choice for this.
If it ain't broke, I can fix that.
Bharat Katkar
Honored Contributor

Re: Feedback requested on output format for scripting

Hi,

1. Since this function is going to be used in scriting i would go for listing the attributes in one single line so that they can be used easily for further processing.
2. I would prefer to have one more option for having attribute name displayed with value and the other one only the value which should be default.
e.g. i would add one more switch say v to the command to get attribute name along with the value and by default it should only display the value.

a) cmd get -a -a ...
:::...

b) cmd get -v -a -a ...
:::.....

Regards,
You need to know a lot to actually know how little you know
Hein van den Heuvel
Honored Contributor

Re: Feedback requested on output format for scripting

I like the simple option 1) because supposedly my script asked for the atributes in a given order and knows to receive them in that order.

As Clay hints to, the prefered seperator might not be obvious. Why not have that be a command line option like AWKs -F. A good default would be TAB.

The parsing now is simple READ.
The basic Perl parsing now is a very simple:

@val=split /\t/, `$command`
or with no spaces in the values:
$_ = `$command`;
@val=split;

If this is an option which generates multiple, popular, attributes (like ps -ef) then a header line is needed. This could be implied by such multi-attribute selection.
You then just pre-parse the attributes and turn them into string/keyed indexes. In Perl:

$_=<>;
@attrs=split;
$i=0;
$attr{$a}=$i++ while($attrs[$i]};
$_=<>;
@val=split;
print $val[$attr{"attr2"}]."\n"

or

@attrs=split /\t/,<>;
for ($i=0;$i<@attrs;$i++) {$attr{$attrs[$i]}=$i};
@val=split /\t/,<>;
print $val[$attr{"attr2"}]."\n"

btw... I noticed one minor catch with a TAB versus the simple perl whitespace split. With an explicit split on TAB the potential newline after the last field become part of the field, so needs to be 'chomped' first:

$_=<>;
chomp;
@attrs=split /\t/;


I suppose a generic solution would be an attribute/value format string with recognized words for attribute and value:
-F ":\n"
-F "=:"
-F "\t"

nah!

fwiw,
Hein.
Tom Schroll
Frequent Advisor

Re: Feedback requested on output format for scripting

My vote is similar to Patrick's. Option 2, with name/value pairs...on a seperate line. If the name/value pairs are to be seperated by whitespace, how are we dealing with embedded whitespace as a value?

In this case, it might be a good idea to add embedded quotes when a value contains whitespace. But that can also create a slippery slope...because what if a value contains an escaped quote...

Certainly it creates more food for thought.
If it ain't broke, it needs optimized.
Sandman!
Honored Contributor

Re: Feedback requested on output format for scripting

I'll vote for option 2 where the name-value pairs are on a separate line so that its easy to read. However if the list is too long would there be a way to scroll backwards to see what the name-value pairs are?

regards
marie-noelle jeanson_1
Trusted Contributor

Re: Feedback requested on output format for scripting

Hi,

Thank you all for your interest in my question. Let me clarify one point, in our case, we always know the list of attribute names (they are passed as argument), and if an attribute does not exist, the whole command fails. Unless you feel this is not a good approach and you prefer to just say "not found" or some well known value if this happens.

Your replies so far are split between option 1 (single line) and option 2 (new line), and alsop between displaying the attribute name or not.

One solution would be to offer an option for each of those (display on single line versus display one per line; display attribute name or not).

All your comments are valid and appreciated. Feel free to add more,

Marie-Noelle

A. Clay Stephenson
Acclaimed Contributor

Re: Feedback requested on output format for scripting

This thing should behave like a UNIX command -- what a concept. If you specify an incorrect attribute or argument then the command should exit with a non-zero status and write a description of the error on stderr. Conversely, if all is well, the command should exit with a zero status and write the output on stdout. There is no need to display the attribute name on the output because that is implicit in the arguments passed. I would add an optional argument that allows a user specified field separator
with the default set to . If the field separator specified by the argument is "\n" then you could have the best of both worlds
because those poor lost souls whole prefer separate lines of output for each value now get it and us smart guys who want them all in one line get them like that. Moreover since the last value outputted gets a "\n" in both cases there is no additional coding required. There should be a man page and I always add a usage section requested with -u to all my scripts/programs.

If it ain't broke, I can fix that.