LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   AIX shell script to get Hostname from Machine and (http://www.linuxquestions.org/questions/linux-newbie-8/aix-shell-script-to-get-hostname-from-machine-and-4175431768/)

linux098 10-11-2012 08:28 PM

AIX shell script to get Hostname from Machine and
 
Need to write a script which sources the env prefix variable.

For that purpose I need first identify the env variable with the help of machine hostname

We have three diff AIX environments Devlopment, Test and Production

The corresponding hostnames for the above environments are devtim testtim and prodtim

The script should dynamically get the hostname and consider the first three letters of the hostname (dev tes and pro)
and assign it to the env variable that can be used further in the script

HOSTNAME=$ (hostname) gets me the host name but I do not know the logic on how to trim the the first three letters
and assign the output (dev, tes or pro) to env variable

so when I run the script and if I do echo $env at end it should give me the env value that is set based on the
machine hostname
env=dev
or
env=tes
or
env=pro

so when further in the script if I need to build the hostname of an ldap server for this env
my script will source this env variable as prefix ans the rest of the hostname is common

ldap_host=$env.ldap.company.com

example ldap_host my dev machine ldap_host=dev.ldap.company.com
example ldap_host my Test machine ldap_host=tes.ldap.company.com
example ldap_host my Prod machine ldap_host=pro.ldap.company.com


Any advise or example script is greatly appreciated

schneidz 10-11-2012 08:36 PM

Code:

hostname | cut -b -3

linux098 10-11-2012 08:49 PM

Quote:

Originally Posted by schneidz (Post 4803491)
Code:

hostname | cut -b -3

Thanks Schneidz for a prompt response

So hostname | cut -b -3 gets me the string for env variable (dev,tes & pro)

once after cutting the hostname how do I need to assign the output to Env variable

Can I use the below option

#!/bin/sh
#set -x
HOSTNAME=$(hostname | cut -b -3)
env=$HOSTNAME
echo $env

chrism01 10-12-2012 12:09 AM

Why don't you try it ?

BTW, when embedding a var value into a string, you should use { } thus
Code:

ldap_host=${env}.ldap.company.com
This clarifies to the parser where the varname starts/stops.

linux098 10-12-2012 09:21 AM

Quote:

Originally Posted by chrism01 (Post 4803561)
Why don't you try it ?

BTW, when embedding a var value into a string, you should use { } thus
Code:

ldap_host=${env}.ldap.company.com
This clarifies to the parser where the varname starts/stops.

Thanks Chris and Schneidz

It worked! :)


All times are GMT -5. The time now is 07:35 PM.