LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-11-2012, 08:28 PM   #1
linux098
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Rep: Reputation: Disabled
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
 
Old 10-11-2012, 08:36 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,958

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Code:
hostname | cut -b -3

Last edited by schneidz; 10-11-2012 at 08:38 PM.
 
1 members found this post helpful.
Old 10-11-2012, 08:49 PM   #3
linux098
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
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
 
Old 10-12-2012, 12:09 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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.

Last edited by chrism01; 10-12-2012 at 12:11 AM.
 
Old 10-12-2012, 09:21 AM   #5
linux098
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by chrism01 View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error in getting Hostname&ipaddress of remote machine using shell scripts arun5002 Linux - Server 4 09-02-2011 11:56 AM
Desire trick to modify hostname in shell script maddog. Linux - General 1 03-28-2011 09:21 PM
$HOSTNAME shell script not working Ricio Programming 4 07-23-2008 02:18 PM
Using shell script to change hostname(Fedora Core6) lion1978 Linux - Software 10 03-05-2007 02:14 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration