From: kielhd on
Hi NG,

I am writing a script that is supposed to run on different UNIX-
derivates. It runs OK on AIX, HP-UX and Linux.

At the moment, I am implementing the SunOS part. At SunOS I came over
a problem, where I need your help.

The script consists of a seperate section for each UNIX-derivate and a
common part, that is used by *ALL*
UNIX-derivates. I have got a problem in the common part.

In this common part I am using the command 'awk' to do some text
manipulations:
....
awk -v c1schwell1...
....

In SunOS, this command needs to be replaced by:
....
/usr/xpg4/bin/awk -v c1schwell1...
....

My question:
Is it possible to replace a command depending on the name of the
operating system?

To put my question in a different way:
Is it possible to tell my script to use the command '/usr/xpg4/bin/
awk' instead of the command 'awk' if it is running on SunOS?

TIA,
Henning
From: Janis Papanagnou on
kielhd wrote:
> Hi NG,
>
> I am writing a script that is supposed to run on different UNIX-
> derivates. It runs OK on AIX, HP-UX and Linux.
>
> At the moment, I am implementing the SunOS part. At SunOS I came over
> a problem, where I need your help.
>
> The script consists of a seperate section for each UNIX-derivate and a
> common part, that is used by *ALL*
> UNIX-derivates. I have got a problem in the common part.
>
> In this common part I am using the command 'awk' to do some text
> manipulations:
> ...
> awk -v c1schwell1...
> ...
>
> In SunOS, this command needs to be replaced by:
> ...
> /usr/xpg4/bin/awk -v c1schwell1...
> ...
>
> My question:
> Is it possible to replace a command depending on the name of the
> operating system?
>
> To put my question in a different way:
> Is it possible to tell my script to use the command '/usr/xpg4/bin/
> awk' instead of the command 'awk' if it is running on SunOS?

Consider that calling 'awk' without path will search the PATH
variable components sequentially and get the first awk it finds.
You can prepend the xpg4 path before the paths where other awk
are found. Put PATH=/usr/xpg4/bin:$PATH at the beginning your
scripts, or put that definition in the profile of the users that
want to run the scripts.

Janis

>
> TIA,
> Henning
From: Icarus Sparry on
On Wed, 26 May 2010 14:27:25 +0200, Janis Papanagnou wrote:

> kielhd wrote:
>> Hi NG,
>>
>> I am writing a script that is supposed to run on different UNIX-
>> derivates. It runs OK on AIX, HP-UX and Linux.
>>
>> At the moment, I am implementing the SunOS part. At SunOS I came over a
>> problem, where I need your help.
>>
>> The script consists of a seperate section for each UNIX-derivate and a
>> common part, that is used by *ALL*
>> UNIX-derivates. I have got a problem in the common part.
>>
>> In this common part I am using the command 'awk' to do some text
>> manipulations:
>> ...
>> awk -v c1schwell1...
>> ...
>>
>> In SunOS, this command needs to be replaced by: ...
>> /usr/xpg4/bin/awk -v c1schwell1...
>> ...
>>
>> My question:
>> Is it possible to replace a command depending on the name of the
>> operating system?
>>
>> To put my question in a different way: Is it possible to tell my script
>> to use the command '/usr/xpg4/bin/ awk' instead of the command 'awk' if
>> it is running on SunOS?
>
> Consider that calling 'awk' without path will search the PATH variable
> components sequentially and get the first awk it finds. You can prepend
> the xpg4 path before the paths where other awk are found. Put
> PATH=/usr/xpg4/bin:$PATH at the beginning your scripts, or put that
> definition in the profile of the users that want to run the scripts.

Another approach is to use a variable to hold the command, and then set
that according to the OS. A reasonable name might be AWK. Then at the
start of the script you might have

AWK=awk

and in your 'SunOS' section you add
AWK=/usr/xpg4/bin/awk
and replace all other uses of 'awk' with '$AWK'.

This allows the command name to change, e.g. from awk to gawk, nawk, mawk,
as needed.

Of course you can also set up functions to do this indirection. You could
in the 'SunOS' section just have

awk(){ /usr/xpg4/bin/awk "$@" ; }

and leave the rest of the script unchanged.
From: Jon LaBadie on
kielhd wrote:
> Hi NG,
>
> I am writing a script that is supposed to run on different UNIX-
> derivates. It runs OK on AIX, HP-UX and Linux.
>
> At the moment, I am implementing the SunOS part. At SunOS I came over
> a problem, where I need your help.
>
> The script consists of a seperate section for each UNIX-derivate and a
> common part, that is used by *ALL*
> UNIX-derivates. I have got a problem in the common part.
>
> In this common part I am using the command 'awk' to do some text
> manipulations:
> ...
> awk -v c1schwell1...
> ...
>
> In SunOS, this command needs to be replaced by:
> ...
> /usr/xpg4/bin/awk -v c1schwell1...
> ...
>
> My question:
> Is it possible to replace a command depending on the name of the
> operating system?
>
> To put my question in a different way:
> Is it possible to tell my script to use the command '/usr/xpg4/bin/
> awk' instead of the command 'awk' if it is running on SunOS?
>

You could replace "awk" with a shell variable, eg. AWKCMD, specifying
the command name and/or location. Then, before the call(s) to awk,
set the variable to a value appropriate to that OS.

If Solaris is the only exception something like this might work:

AWKCMD=awk # default
if [ $(uname -s) = SunOS ]
then
AWKCMD=/usr/xpg4/bin
fi

If multiple OS need to be tested for, run a set of tests to
set an OS_TYPE variable to some value. The a case statement
can set the AWKCMD variable depending on OS_TYPE

From: Kees Nuyt on
On Wed, 26 May 2010 05:12:47 -0700 (PDT), kielhd
<kielhd(a)freenet.de> wrote:

>Hi NG,
>
>I am writing a script that is supposed to run on different UNIX-
>derivates. It runs OK on AIX, HP-UX and Linux.
>
>At the moment, I am implementing the SunOS part. At SunOS I came over
>a problem, where I need your help.
>
>The script consists of a seperate section for each UNIX-derivate and a
>common part, that is used by *ALL*
> UNIX-derivates. I have got a problem in the common part.
>
>In this common part I am using the command 'awk' to do some text
>manipulations:
>...
>awk -v c1schwell1...
>...
>
>In SunOS, this command needs to be replaced by:
>...
>/usr/xpg4/bin/awk -v c1schwell1...
>...
>
>My question:
>Is it possible to replace a command depending on the name of the
>operating system?
>
>To put my question in a different way:
>Is it possible to tell my script to use the command '/usr/xpg4/bin/
>awk' instead of the command 'awk' if it is running on SunOS?

me(a)ozon ~ $ which awk
/usr/bin/awk

me(a)ozon ~ $ PATH=$(getconf PATH):$PATH which awk
/usr/xpg4/bin/awk

IIRC on POSIX compliant systems getconf PATH should return a
path to POSIX compliant implementations of 'all' tools, it could
work on all platforms you mentioned.
Best regards,
--
( Kees Nuyt
)
c[_]