Dynamic Variable creation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dynamic Variable creation
# 1  
Old 08-20-2015
Dynamic Variable creation

I am trying to create some variables based on the input by the user, say if user entered 3 then 3 variables and if 5 then 5 variables.

I am using a for loop

Code:
 for (( i=1; i <= $num; i++ ))
do
x="num"
x+=$i
done

When i am using echo $x it will show num1 but now how to create variables named num1, num2 and so on.
I am not able to use:-
Code:
echo "$x"=0

Please advise.
# 2  
Old 08-20-2015
What operating system and shell are you using?...

This is what shell array variables are designed for (if your shell supports them); otherwise you need something like eval (and eval is dangerous except under very limited circumstances).
# 3  
Old 08-20-2015
I am using Linux(2.6.32-358.el6.x86_64) and Bash.
# 4  
Old 08-20-2015
In both bash and ksh you can have arrays with integer subscripts (and with recent bash and 1993 or later ksh, you can also use non-numeric strings as subscripts). For example:
Code:
$ for((i = 1; i <= 5; i++))
do	num[$i]=$(($i * $i))
done
$ for((i = 1; i <= 5; i++))
do	printf 'num[%d]=%d\n' $i ${num[$i]}
done
num[1]=1
num[2]=4
num[3]=9
num[4]=16
num[5]=25
$

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variable assignment

#!/bin/sh if then echo "Insufficient number of arguments ">> error".log" ; echo "Please check error log for more details"; exit 1 ; else file_name=$1".csv"; fi ; in_par_number=`head -n1 $file_name | sed 's///g' | wc -c` read -a arr <<< `head -n1 $file_name | sed 's// /g'` ... (6 Replies)
Discussion started by: JayDoshi
6 Replies

2. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

3. Shell Programming and Scripting

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (7 Replies)
Discussion started by: slatoms
7 Replies

4. Red Hat

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (1 Reply)
Discussion started by: slatoms
1 Replies

5. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

6. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

7. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

8. UNIX for Advanced & Expert Users

Dynamic file creation

This my script ls *.ksh ls *.ksh > a.txt i=1 cat "a.txt" | while read a do if then echo "abc" echo "abc" > m.ksh echo "m.ksh" >> a.txt i=2 fi echo $a done ls *.ksh -------------------------- My Output is a.ksh - > first ls *.ksh (1 Reply)
Discussion started by: kingganesh04
1 Replies

9. Shell Programming and Scripting

Creation of log file under dynamic date folder

HI I want to create a date folder and then a log file under it, which will hold all output of shell script. Say shell script abc.sh runs every day and I want to redirect the output of abc.sh > /opt/bea/wls81/Pkmtest/$(date +%Y%m%d)/ant.log. Here date should always change according to system... (2 Replies)
Discussion started by: pankajkrmishra
2 Replies

10. Shell Programming and Scripting

creation variable

I want to define a variable like this. Can i declare such type? I have already define this, but error is displaying sysntax error. So tell me what will be the write. int create_mailfile(int,char*,char*,char*,char*,char*,char*); (1 Reply)
Discussion started by: debasis.mishra
1 Replies
Login or Register to Ask a Question