Multiple variable substitution in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple variable substitution in one line
# 1  
Old 04-06-2010
Multiple variable substitution in one line

Hi,

I want to get content of a$i variables with one command:

a1=/tmp1.log
a2=/tmp2.log

for i in 1 2;do
log=<some code>
echo $log
done

and get the content of a1 and a2:
/tmp1.log
/tmp2.log

Thanks
# 2  
Old 04-06-2010
Quote:
Originally Posted by gdan2000
...
I want to get content of a$i variables with one command:

a1=/tmp1.log
a2=/tmp2.log

for i in 1 2;do
log=<some code>
echo $log
done

and get the content of a1 and a2:
/tmp1.log
/tmp2.log

...
Code:
$
$
$ # display the content of file f9
$ cat f9
a1=/tmp1.log
a2=/tmp2.log
$
$ # set the field separator
$ IFS="="
$
$ # read the variable and its value from f9
$ while read var value
> do
>   echo $value
> done <f9
/tmp1.log
/tmp2.log
$
$

tyler_durden

Or without setting the IFS -

Code:
$
$
$ cat f9
a1=/tmp1.log
a2=/tmp2.log
$
$ while read line
> do
>   log=`echo ${line##*=}`
>   echo $log
> done <f9
/tmp1.log
/tmp2.log
$
$


Last edited by durden_tyler; 04-06-2010 at 10:17 AM..
# 3  
Old 04-06-2010
thanks, but this solution is missing the point

a1,a2 variables are just an example. I want to use indexes 1,2,3... to append to other variables as well, e.g. c1,c2,c3.

And I don't want to use predefined arrays or save names in separate files.

a1=/tmp1.log
a2=/tmp2.log

b1=/var/tmp.log
b2=/var/tmp.log

...
....

for i in 1 2 ...;do
log=<some code> # $a$i
blog=<some code> # $b$i
....
echo $log
echo $blog
....
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple variable substitution in a file in one go

I have a huge script which is defining variables with full path of commands in the beginning of code and using those variables in the script. For Example: ECHO=/bin/echo LS=/bin/ls SED=/bin/sed AWK=/bin/awk UNAME=/bin/uname PS=/bin/ps DATE=/bin/date GREP=/bin/grep $ECHO "hello... (1 Reply)
Discussion started by: veeresh_15
1 Replies

2. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

3. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

4. Programming

Makefile: multiple target variable substitution

Greetings! Basically, I would like to properly handle this with gnu make: alltools: my_tool mysecond_tool mythird_tool etc_tool %_tool: dir1/%_tool.vf dir2/%_tool/subdir2/%_tool.ver <tab>@echo done %.vf: <tab>RUN_VF $* %.ver: <tab>RUN_VER $* So, if I were to do something like:... (0 Replies)
Discussion started by: Harlinator
0 Replies

5. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

6. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

7. Shell Programming and Scripting

How to use sed substitution using a $variable for a line containing a word

$ cat ggg /E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case REGION1: /E*Fare/dist/src/nggfstaticbase/EFare/Server CODEBASE1: /dev_tools/LINUXMTP-4/EFS070718E/EFare/Server DATABASE1: nggfstatic SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input PROLOGINITSIZE not yet set You... (4 Replies)
Discussion started by: Sangal-Arun
4 Replies

8. Shell Programming and Scripting

Multiple Substitution

Hello Guys, I have some variables declared as below VARIABLE1=Table VARIABLE2=VARIABLE1 Now when I do an echo $VARIABLE2 it gives me below atrcus303{root} #: echo $VARIABLE2 VARIABLE1 I want to echo the value of VARIABLE1 using the VARIABLE2. So I tried to the below atrcus303{root} #:... (1 Reply)
Discussion started by: Mohammed
1 Replies

9. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question