LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2012, 05:57 AM   #1
sadosan83
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Rep: Reputation: Disabled
concatenate strings in bash


hi all

node1_pc_ip=10.20.5.10
node1_modem_ip=192.168.2.10
node2_pc_ip=10.20.5.20
node2_modem_ip=192.168.2.20



for i in $(seq "$node")
do

echo $node$i_pc_ip

done


echo $node$i_pc_ip now working , output is;
1_pc_ip
2_pc_ip
3_pc_ip
.
I would like to output is ;

10.20.5.10
10.20.5.11
10.20.5.12

help me pls
 
Old 01-11-2012, 07:05 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hello,
1) what is seq "$node" supposed to do?
2) I'm not sure I understand what you want to do, but have you tried
Code:
eval echo \$node${i}_pc_ip
EDIT: 3) or, even better, use arrays

Last edited by millgates; 01-11-2012 at 07:27 AM.
 
Old 01-11-2012, 07:32 AM   #3
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 375
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
Or

varname="node${i}_pc_ip"
echo ${!varname}
 
Old 01-11-2012, 08:29 AM   #4
sadosan83
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Hello,
1) what is seq "$node" supposed to do?
2) I'm not sure I understand what you want to do, but have you tried
Code:
eval echo \$node${i}_pc_ip
EDIT: 3) or, even better, use arrays
thanks ... fixed
 
Old 01-11-2012, 11:08 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Aargh. No, no, no! Please do not use indirect variables for things like this. And for goodness sake, stay away from eval.

As suggested, this is exactly the kind of thing you should be using arrays for.

Code:
node_pc_ip=( 10.20.5.10 10.20.5.20 )
node_modem_ip=( 192.168.2.10 192.168.2.20 )

for i in {1..2}; do

	echo "${node_pc_ip[i]}"
	echo "${node_modem_ip[i]}"

done
Please stop and take the time to read the BashGuide here, to help you understand basic scripting concepts such as arrays.

http://mywiki.wooledge.org/BashGuide


Then at least scan through the pitfalls and faq pages for more pointers and things to be aware of, such as the importance of proper quoting and using brace expansion (or a c-style loop) instead of seq:

http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ

Finally, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.
 
Old 01-12-2012, 03:02 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I agree with David the H., except that since Bash uses zero-based indexing, I do believe the example snippet should read
Code:
node_pc_ip=( 10.20.5.10 10.20.5.20 )
node_modem_ip=( 192.168.2.10 192.168.2.20 )

for i in {0..1}; do

	echo "${node_pc_ip[i]}"
	echo "${node_modem_ip[i]}"

done
 
Old 01-12-2012, 09:45 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
D'oh! Thanks for catching my mistake, NA.
 
Old 01-13-2012, 01:48 AM   #8
sadosan83
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
D'oh! Thanks for catching my mistake, NA.
thanks all member
resolf pls
 
  


Reply



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
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
concatenate variable names in bash xeon123 Linux - Newbie 4 04-07-2011 08:01 PM
[SOLVED] bash: concatenate several orders jmvidalvia Linux - General 3 03-29-2011 10:45 AM
[SOLVED] How to concatenate strings in Shell kofucii Linux - Newbie 2 12-12-2009 05:07 AM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 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