Solved

use a variable (like $i) as part of another variables name in bash

Posted on 2013-06-20
5
232 Views
Last Modified: 2013-06-23
I have a file with variables like this...
server_1=
port_1=
user_1=

server_2=
port_2=
user_2=

Open in new window


I am trying to write a script that can read them in and manipulate them and write them back out.  And it should be able to handle any number of them.  Below is a simplified version of what I want to be able to do.  (i.e. not actually trying to edit anything here.  Just write it back out)

Is this at all possible?


source filewithvariables
for (( i=1; i<=$number_of_servers; i++ ))
do

echo "server_${i}=${server_${i}}" >> output 
echo "port_${i}=${port_${i}}" >> output 
echo "user_${i}=${user_${i}}" >> output 

done

Open in new window

0
Comment
Question by:Xetroximyn
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39263887
eval echo "server_${i}=\${server_${i}}" >> output
or
name=server_${i}; echo "server_${i}=${!name}" >> output
0
 
LVL 84

Expert Comment

by:ozo
ID: 39263920
or
for name in ${!server_*}  ; do  echo "$name=${!name}"  ; done
0
 

Author Comment

by:Xetroximyn
ID: 39266692
Thanks!

Just want to understand this a bit better... I think I understand the val method.  Baiscally eval replaces the ${i} with the "1" and leaves the first $ in there because it's escaped and thus bash is left to execute
server_1=${server_1}

And I think I understand the indirect referencing with the ${!name} syntax.  It seems like it takes the contents of $name and uses that for the variable name to get the contents of instead of just getting the contents of ${!name}

However - if it worked like I think it does - I don't quite understand why these two things would not be the same

echo $name_1
i=1
echo $name_${!i}

Open in new window


any chance you can help me understand that?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39267231
i=1
echo $name_${!i}
would have the same effect as
echo $name_$1
0
 

Author Closing Comment

by:Xetroximyn
ID: 39269814
TY!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

13 Experts available now in Live!

Get 1:1 Help Now