LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2015, 05:01 PM   #1
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Rep: Reputation: Disabled
NEED small help with $ in the bash file


Hi Guys ,

i have code as below :
IP1=69.46.87.106
IP2=69.46.87.107
IP3=69.46.87.108
IP4=69.46.87.109
IP5=69.46.87.110
################################
D=$(cat /dev/urandom | tr -dc '1-5' | fold -w 1 | head -n 1)
=====================
D is from 1-5

as u see above i need to echo the output randomly

say i want to put
echo $IP$D

i want to be able to have IP1 , IP2 , IP3

as u see above
$IP$D is not working with

is there a method to identify variable with many $ like fourmula

echo $IP$D

??

what options do i have to fix my issue

cheers
 
Old 11-05-2015, 05:41 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
There is no variable named IP, thus $IP yields the empty string.

Your can achieve what you want (looks like a load balancer written in Bash ) with an array. For example:
Code:
IP=(69.46.87.106 69.46.87.107 69.46.87.108 69.46.87.109 69.46.87.110)
echo ${IP[$D]}
See also the array section in the bash reference manual.

EDIT: Array indexes start at 0, not 1. You would have to tweak your code so that D ranges from 0 to 4. Or insert a dummy address at the beginning of the array (which would be an ugly hack).

Last edited by berndbausch; 11-05-2015 at 07:30 PM.
 
2 members found this post helpful.
Old 11-05-2015, 07:16 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
For the simple case you described, an array is certainly the easiest way to go.

For the more general case, you can build up a variable name as a string by any means you like, and then use that string indirectly as a variable name:
Code:
VarName=IP$D
echo ${!VarName}
That "!" ahead of VarName says to treat that string as the name of the variable to be referenced.
 
2 members found this post helpful.
  


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
small bash script copy one file to list of directories on file lahirushanaka Linux - Newbie 10 06-14-2014 11:00 AM
small bash command Bob32 Linux - Newbie 3 03-23-2011 02:12 AM
small question about rm in BASH Guru Mind Programming 11 10-16-2006 03:08 PM
Bash small problem bit128_linux Programming 6 01-13-2006 12:56 AM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration