Operating System - HP-UX
1834533 Members
2805 Online
110069 Solutions
New Discussion

ps -o within script fails

 
SOLVED
Go to solution
Sylvain CROUET
Respected Contributor

ps -o within script fails

Hi.

I want to use the ps command within a script to control processes and I want to build it with variables:
PS="/usr/bin/ps"
PS_OPT="-efl"
$PS ${PS_OPT} > temp_file

It works fine when PS_OPT="-efl" but it does not work when PS_OPT="-e -o args"; I get the following error message:
/usr/bin/ps: illegal option --
/usr/bin/ps: illegal option -- -
/usr/bin/ps: is not a valid field name

To be able using the -o option I set UNXI95 to 1 and export it just before executing the ps command.

ps -e -o args works fine when executed directly on command line.
10 REPLIES 10
Mark Grant
Honored Contributor

Re: ps -o within script fails

I don't think you want to export UNIX95. This mean the variable will be set for all commands.

See if this makes any difference

UNIX95= $PS ${PS_OPT}
Never preceed any demonstration with anything more predictive than "watch this"
Pete Randall
Outstanding Contributor

Re: ps -o within script fails

The -o switch is part of the XPG4 extensions to ps, invoked with the "UNIX95= ps -o" syntax.


Pete

Pete
Jean-Louis Phelix
Honored Contributor

Re: ps -o within script fails

Hi,

I confirm that this 3 lines script works perfectly :

PS="/usr/bin/ps"
PS_OPT="-e -o args"
UNIX95= $PS ${PS_OPT}

Regards.
It works for me (© Bill McNAMARA ...)
Helen French
Honored Contributor

Re: ps -o within script fails

This should work for you:

PS="/usr/bin/ps"
PS_OPT="-e -o args"
UNIX95= $PS $PS_OPT

Use UNIX95 with the command instead of exporting it.
Life is a promise, fulfill it!
Bill Hassell
Honored Contributor

Re: ps -o within script fails

As everyone has stated, DON'T export UNIX95. It will affect other commands and libraries, sometimes producing undesired effects. UNIX95 is defined in certain commands but there is no central list so you'd have to read every man page for every command and function. To get a sense of the pervasiveness of this command, go to docs.hp.com and search for UNIX95.

As mentioned, always put UNIX95= in front of the ps command. NOTE: It may look strange but it wor4ks just fine. You don't have to assign a value, just the = sign is fine.


Bill Hassell, sysadmin
Sylvain CROUET
Respected Contributor

Re: ps -o within script fails

It still does not work.
Here is my script attached. Have I made some mistakes I am unable to see?
Bill Hassell
Honored Contributor

Re: ps -o within script fails

Looks OK. I pasted the 3 lines for PS and PS_OPT then the commandline and it works OK. Run your script with tracing:

sh -x


Bill Hassell, sysadmin
Jean-Louis Phelix
Honored Contributor
Solution

Re: ps -o within script fails

Hi,

Try to avoid the IFS="|". Use it only at the place where you need it and restore the default value after, ie :

OFS="$IFS"
IFS="|"
xxxx
IFS=$"OFS"

without IFS changed, your script works ...

Regards.
It works for me (© Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: ps -o within script fails

Hi,

in your case, to avoid modifying IFS you could also use :

cat $FICHIER_PARAM | tr -d '|' ' ' | while read ...
do
xxx
done

Regards.
It works for me (© Bill McNAMARA ...)
Sylvain CROUET
Respected Contributor

Re: ps -o within script fails

Exact, the problem was due to the IFS.
Thanks.