Defining Dynamic Number of Variables in a Bash Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Defining Dynamic Number of Variables in a Bash Script
# 1  
Old 04-26-2010
Defining Dynamic Number of Variables in a Bash Script

Code:
Code:
$ cat test.bash
#!/bin/bash

job=$1
steps=$2

num=$(echo "$@" | wc -w)

Example Submission:
Code:
$ ./test.bash BS01 3 1 2 3

What:
I want to be able to take the number of $2 (steps) and then define the substeps ($3++) for each step.

i.e. the submission of the above would define the following variables

Code:
job BS01
steps 3
substep1 1
substep2 2
substep3 3

I don't know how to create the substeps?

I was thinking about stepping through the $@ using the num variable I setting the substeps in the loop.

Code:
countera=3
counterb=1
while [ $counter -le $num ]; do
 substep${counterb}=eval \$$countera
 countera=countera+1
 counterb=counterb+1
end;

Of course, this doesn't work at all, but it's the closest my inexperience can take me.

Any help is appreciated!

Thanks
# 2  
Old 04-26-2010
Try something like this:
Code:
#!/bin/ksh

i=1
job=$1
steps=$2

shift; shift

while [ $i -le $steps ]
do
  eval "substep${i}=$1"
  shift
  i=$(( $i + 1 ))
done

echo $job
echo $substep1
echo $substep2
echo $substep3

# 3  
Old 04-26-2010
Thanks that worked well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script accepting variables in valid number format

Hi Experts I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have ... (3 Replies)
Discussion started by: ersan-poguita
3 Replies

2. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

3. Shell Programming and Scripting

Create, validate and using dynamic variables in Bash scripting

Hi All, I am really struggling to solve this problem, this might be small but I am not able to, can somebody help me? I have few directories and these directories receives text files in large amount with in fraction of seconds. So I just want to send all the files in current directory to... (2 Replies)
Discussion started by: VasuKukkapalli
2 Replies

4. UNIX for Dummies Questions & Answers

Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you. #!/bin/bash var1=100.25 var2=100.25 if (( $var1 == $var2 )); then echo "Matching" else echo "Not Matching" fi Error: ./number.sh: line... (6 Replies)
Discussion started by: Ariean
6 Replies

5. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

6. Shell Programming and Scripting

defining variables

Hey all, I was wondering if someone would take a look at this script I'm working on. I don't know if i have the syntax correct for my variables and if the for loop is written correctly. any assistance would be greatly appreciated. #!/usr/bin/bash ###########################################... (12 Replies)
Discussion started by: em23
12 Replies

7. Shell Programming and Scripting

Dynamic variables within shell script

Hi Gurus, I have a requirement of writting the shell script where it should ask me two values FND_TOP=/d02/app/oracle/xxx/fnd/11.5.0 CDCRM_TOP=/d02/app/oracle/xxx/cdcrm/11.5.0 and then keep these values stored as variables for the execution of rest of the script. Because, I have to... (2 Replies)
Discussion started by: isingh786
2 Replies

8. UNIX for Dummies Questions & Answers

Defining Variables

I'm trying to define a variable named sin I already have a variable named cos, which has the value "hello" I want sin to have the value of "hellothere", so sin would be something like sin = $cos & "there" but I'm not sure that I know the syntax. Can anyone help? :confused: (4 Replies)
Discussion started by: sailorliones
4 Replies

9. UNIX for Dummies Questions & Answers

defining a variable as a number or character?

I am writing a script that needs to accept numbers into a variable by prompting and using the code read num case $num in 000) break ;; *) I am fairly new to unix and am loving the versatility of the language but need a little shove in the right... (1 Reply)
Discussion started by: noobian
1 Replies

10. UNIX for Dummies Questions & Answers

Defining variables at boot time

Hi, I'm looking for advice on where is the best place on Solaris to put a script that will setup system vairables prior to any users loging in. I've tried /etc/rc3.d without much success as the variables do not appear in the output from an env command. I want the system to have these... (7 Replies)
Discussion started by: ianf
7 Replies
Login or Register to Ask a Question