Solved

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

Posted on 2014-12-08
3
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

726 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