Solved

I want a Here document that will set more than one variables

Posted on 2014-12-08
3
65 Views
Last Modified: 2016-11-23
I would like to have data in a "here doc" and be able to set several variables from the matching here doc line

example:

Here doc

 fred,10.10.10.10,dell
barny,10.10.10.11,toshiba
wilma,10.10.10.12,hp

 

if `hostname`= barney
ip = 10.10.10.11
make = Toshiba

I want the matching row in the data to set all the variables.  

If this is not clear I can explain more


redhat 5,6,7
0
Comment
Question by:TIMFOX123
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40488054
IFS=,
while read h i m ; do
  if [ `hostname` == $h ] ; then
    ip=$i
    make=$m
 fi
done << here
fred,10.10.10.10,dell
barny,10.10.10.11,toshiba
wilma,10.10.10.12,hp
here

Open in new window

If that code is in a file, you need to source it, not execute it
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40489458
Better try "grep":
==> cat m0
cat - <<! >here.txt
fred,10.10.10.10,dell
barny,10.10.10.11,toshiba
wilma,10.10.10.12,hp
!
IFS=','
grep 'barny' here.txt|read host ip make
echo "hostname=$host ip=$ip make=$make"

==> ./m0
hostname=barny ip=10.10.10.11 make=toshiba
==>

Open in new window

0
 

Author Closing Comment

by:TIMFOX123
ID: 40490288
ozo this is perfect

Kidos
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now