Printing hostname

Hi,

I am using hostname environment variable in my shell script to print the host name but it's printing nothing.Can you please help me on this.

Please find the part of the code pasted here:

llist=
llist=$(ps -ef | grep -v grep | grep -c "$1")

echo "Hostname is ${hostname}"

From where do you want to extract this hostname value ? You can use simple : localHostName=`hostname`; echo $localHostName

  1. assign the return value to a variable
  2. send (echo) the variable to screen
> valhost=$(hostname)
> echo $valhost    
grape

Hi,

When I am using commnad:hostname in unix command prompt , it's printing the system name of my system but when I use this command in shell script it gives nothing.

localHostName=hostname
echo $localHostname

Try as I show below (ignoring the > as the prompt character):

Also u can use the environment variable HOSTNAME.

Hi,
U can use the environment variable HOSTNAME or
the command hostname <With options such as -a>

Note the backticks around the "hostname" ---- `hostname` this executes command in a sub-shell and returns the value to the parent one. The other way is, as you have been told, to use the HOSTNAME variable, which is set on every session, verify with : "env | grep -i hostname" - you will receieve : HOSTNAME=your.host.name
In script, just call the variable, as in : echo $HOSTNAME.