Operating System - HP-UX
1829403 Members
1660 Online
109991 Solutions
New Discussion

Re: How to retrieve system values for use within script

 
SOLVED
Go to solution
David Fosgate_1
Occasional Advisor

How to retrieve system values for use within script

As a newbie to the Unix script language I'm not sure how to retrieve the server name within script. Or how to retrieve system values for use within the script. The script I'm writing for reporting system status will be run on 9 servers. I would like to include the server name in the messaging sent to operations when there is a problem.

Thanks

Dave
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to retrieve system values for use within script

David:

Here's two forms:

# X="I am $(hostname)"
# Y="I am `hostname`"

Either will work. Both X and Y hold the same string. The "X" form is the POSIX "preference".

Regards.

...JRF...
Rick Garland
Honored Contributor

Re: How to retrieve system values for use within script

To get various parameters, you can issue the system command and the command is in back-ticks.

Example: HOST=`hostname`
echo $HOST

OS=`uname -s`
echo $OS
Bruce Regittko_1
Esteemed Contributor

Re: How to retrieve system values for use within script

Which shell are you using? If it is the POSIX - HP-UX's default shell - or Korn shell, all of the previous suggestions will work fine. If the C or Bourne shell is being used, then the $(cmd) syntax will not work. The "backticks" should work in all 4 shells.
www.stratech.com/training
Kofi ARTHIABAH
Honored Contributor

Re: How to retrieve system values for use within script

David:

my favorite for capturing host name information is:

uname -a | read OS HOST VER DUMMY
echo $HOST $OS - $VER

Some advise. rather than have each server monitor itself, how about letting a central server probe your list of servers and send a message if there is a problem with one of them?

I find that if you rely on each server to report its failure(?) when mail on that server fails, how will it get its message to operations?
nothing wrong with me that a few lines of code cannot fix!
Maureen Gunkel
Trusted Contributor

Re: How to retrieve system values for use within script

David:

This is how I do it:

var=`hostname`
echo $var

(where var is your variable name)

Hope this helps
No matter where you go, there you are.
James R. Ferguson
Acclaimed Contributor

Re: How to retrieve system values for use within script

David:

For variety, here's another variation of Kofi's post:

# uname -a|awk '{print $1,$2,$3}'

This will echo the first 3-fields or the OS, HOSTNAME and VERSION returned from uname.

Or, to capture this in a variable, and then echo it, do:

# X=`uname -a|awk '{print $1,$2,$3}'`
# echo $X

...JRF...
Jay Gaffney_4
Advisor

Re: How to retrieve system values for use within script

>>For variety, here's another variation of >>Kofi's post:
>># uname -a|awk '{print $1,$2,$3}'
>>This will echo the first 3-fields or the OS, >>HOSTNAME and VERSION returned from uname.
>>Or, to capture this in a variable, and then >>echo it, do:
>># X=`uname -a|awk '{print $1,$2,$3}'`
>># echo $X

Another way, similar to James' above but without awk -

% uname -a
HP-UX alba B.10.20 A 9000/778 2015484926 two-user license
% uname -s -n -r
HP-UX alba B.10.20
%

So,
% X=$(uname -snr)
% echo $X
HP-UX alba B.10.20
%

Or

% X=`uname -snr`

will get the same thing.
% man uname
will show you allot of options.

Enjoy,

Jay

David Fosgate_1
Occasional Advisor

Re: How to retrieve system values for use within script

All great answers and observations. I'm developing a number std out files and will monitor for specific values and message back any questionable metrics.
The process will be a cron job on each server, reporting back centrally.

Kofi, et all, The idea of centrally polling the servers for the same metrics sounds great. Any ideas on how that might be accomplished would be great!!

Thanks again for the support

Dave