Bash script to read a hostname and separate into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to read a hostname and separate into variables
# 1  
Old 06-25-2009
Bash script to read a hostname and separate into variables

Hi All,

I'm trying to concoct a bash script to use with a Puppet Implementation that will accept a hostname and break it down into variables.

For example, my hostnames look like this --> machinename-group-building.example.com

I'm looking for a way in the script to read until the first hypen (run it like --> "/usr/bin/hostnamescript machinename-group.building.example.com"), and assign the "machinename" into a variable, read to the second hyphen and assign the "group" into a variable, and then read until the period and assign the "building" into the final variable - disregarding the rest.

What would be the easiest way to tackle this? Thanks!
# 2  
Old 06-25-2009
off the top of my head, this should work:

Code:
machine=`echo $hostname | awk -F'-' '{print $1}'`
group=`echo $hostname | awk -F'-' '{print $2}'`
building=`echo $hostname | awk -F'-' '{print $3}' | sed 's/\..*//'`

I'm sure there is a better way. There is always 17+ ways to skin the cat.
# 3  
Old 06-25-2009
That works! Thanks a million!

Code:
#!/bin/sh

hostname=$1

#first=`echo $1 | cut -d '-' -f1`
#two=`echo $1 | cut -d '-' -f2`
#three= `echo $1 | cut -d '-' -f3`

one=`echo $hostname | awk -F'-' '{print $1}'`
two=`echo $hostname | awk -F'-' '{print $2}'`
three=`echo $hostname | awk -F'-' '{print $3}' | sed 's/\..*//'`

echo "One: [$one]"
echo "Two: [$two]"
echo "Three: [$three]"

# 4  
Old 06-25-2009
Here is another way using an array. I reassigned the values to meaningful variables, but that's optional.

Code:
#!/bin/bash

var=($( echo $1 | sed -e 's/^\([[:alnum:]]*\)-\([[:alnum:]]*\).\([[:alnum:]]*\)\.[.[:alnum:]]*$/\1 \2 \3 /g' ))

host=${var[0]}
group=${var[1]}
building=${var[2]}

printf "host=%s\ngroup=%s\nbuilding=%s\n" $host $group $building

exit 0

output:
Code:
./post302328860.sh machinename-group.building.domain.com
host=machinename
group=group
building=building

I know you asked for bash, but here is a simple perl script:

Code:
#!/usr/bin/perl -w
use warnings;
use strict;
m/^(\w+)-(\w+)\.(\w+)\.[\.\w]*$/;
my ($machine $group $building)=($1,$2,$3);
print "machine=$machine\ngroup=$group\nbuilding=$building\n";

-B
# 5  
Old 06-25-2009
Quote:
Originally Posted by glarizza
What would be the easiest way to tackle this?

Shell parameter expansion. There's no need for an external command:

Code:
name=machinename-group-building.example.com
right=${name##*-*-}
hostnamescript "${name%"-$right"}.$right"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read variables from a text file for use in csh script

Hello, I have a text file (say, declarevars.txt) that contains multiple lines that are essentially meant to be variable declarations: set arr1 = (var1a var1b var1c) set arr2 = (var2a var2b var2c) . . . I want to be able to read this text file within a csh (sorry) script and have that... (2 Replies)
Discussion started by: arjaydj
2 Replies

2. Shell Programming and Scripting

Problem with variables and bash script

From the command line: dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX -rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Discussion started by: dionbl
2 Replies

3. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

4. Shell Programming and Scripting

Bash-how to properly READ and PASTE variables.

Recently i made a script for a project at molecular dynamics but am stuck at the last step.The thing i want to do is to ask the user to input the number of particles, then replace the bolded numbers at lines 9 and 17.. code #!/bin/bash #read number of particles echo "insert the number of... (2 Replies)
Discussion started by: arisinhell
2 Replies

5. UNIX for Advanced & Expert Users

Bash script with export variables

Hi all guys, how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it. But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script. Consequently in... (8 Replies)
Discussion started by: idro
8 Replies

6. Shell Programming and Scripting

Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#) #!/bin/bash #Sample Script VAR1= echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." read VAR1 case $VAR1 in 1) echo "Pick... (4 Replies)
Discussion started by: eclerget
4 Replies

7. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

8. Shell Programming and Scripting

How to make 2 separate arguments in 1 bash script?

This is what I have: #!/bin/bash #ascript.sh WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1` sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config WORD2=`tail -n +$1 /home/gscn/word2.txt | head -1` sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp >... (4 Replies)
Discussion started by: guitarscn
4 Replies

9. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies

10. Shell Programming and Scripting

Cant separate variables

Hey guys, new problem....im not being able to seperate variables. the code runs like this... OPTIONS=$(awk '{print $2}' /etc/fstab} a=$(zenity --list --text "Mount points selection" --radiolist --column "choice" --column "mountpt" FALSE $OPTIONS); echo $a Note: the result i get is that... (2 Replies)
Discussion started by: dplate07
2 Replies
Login or Register to Ask a Question