1833861 Members
2565 Online
110063 Solutions
New Discussion

Re: script question

 
Hunki
Super Advisor

script question

I have this script in which whenever I am trying to run it on hosta it does an equal for hostb and fails ...let me show u the set -x output :

+ uname -n
+ [ hosta = hostb ]

I have the if statement in the script for this which is failing :

if [ `uname -n` = hostb ]
then
...
else
# should execute stuff meant for hosta
fi

Its weird in a sense that most of my other scrips are scripted the same way and I even tried having a variable for the uname -n command but it would still equal the wrong box.
5 REPLIES 5
Hunki
Super Advisor

Re: script question

In fact I tried ( if then else fi ) with a sample script and do get the same message for set -x ( I mean hosta = hostb ) and it runs fine after that , so there is probably something wrong in my script after else.
Bill Hassell
Honored Contributor

Re: script question

The statement:

if [ `uname -n` = hostb ]

is better written:

if [ $(uname -n) = hostb ]

as the backticks (aka, grave accent) have been deprecated for many years.

As far as a failed match, uname -n reports what it was set to in the startup config file /etc/rc.config.d/netconf. On the machine that is failing, uname has been been set to an unexpected value. Scripting won't fix this because the box is misconfigured. uname -n is *NOT* connected with DNS (see the man page for uname). By convention the Internet name and uname will be the same but there is no restriction to make them different.


Bill Hassell, sysadmin
Sandman!
Honored Contributor

Re: script question

Try replacing `uname -n` with $(uname -n) and make sure spacing is correct.
A. Clay Stephenson
Acclaimed Contributor

Re: script question

Always identify your shell when asking a question like this.

In any event, if the situation is as described, the else part should be executing.

Try this (and apply some discplined quoting which you are missing):

if [ "$(uname -n)" = "hostb" ]
then
...
else
...
fi

or better still if using the Korn of POSIX shell:
if [[ "$(uname -n)" = "hostb" ]]
then
...
else
...
fi



If it ain't broke, I can fix that.
Arturo Galbiati
Esteemed Contributor

Re: script question

Hi,
use:
if [[ $(hostname) = hostb ]]

or

if [[ $(uname -n) = hostb ]]

HTH,
Art