'Dynamic' setting of variables in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 'Dynamic' setting of variables in bash script
# 1  
Old 03-18-2014
'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.

Code:
n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done

The error message is:
Code:
bash: p1=line1: command not found
bash: p2=line2: command not found
bash: p3=line3: command not found
bash: p4=line4: command not found
bash: p5=line5: command not found

I want each line of a list of arbitrary length and content stored in variables, so the content of a specific line can be identified by the variable p[1-5] (in this example).

Any suggestions welcome! If possible, I'd prefer a while loop solution.

Thank you for reading!
# 2  
Old 03-18-2014
Try using eval:
Code:
eval p"$n"="$p"

It's not a very good practise to set variables like this though. A much better way is to set arrays:
Code:
$ a[1]=test
$ echo ${a[1]}
test

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 03-18-2014
Thank you! eval works well for this particular purpose!

Can you elaborate on why it isn't good practice this way?

I not familiar with arrays, so i can't figure out how to implement it on the basis of your example.
# 4  
Old 03-18-2014
You could implement an array like this for example:
Code:
n=0; echo "$lst" | while read p; do n=$(($n+1)); a[$n]="$p"; done

Or this:
Code:
echo "$lst" | while read p ; do a[${#a[@]}]="$p" ; done

the use of eval should be limited as much as possible as it could be used to execute malicious code and it can be very confusing...
# 5  
Old 03-18-2014
Thank you for clarifying and for your example!
# 6  
Old 03-18-2014
This is a poor example because, if someone tried to name an array `rm -Rf ~/`, eval would happily execute that and erase your home directory.

We get dozens and dozens of requests for dynamic variable names, but they don't generally make much sense. How would you even use those variables afterwards? If you take a good look at what you're trying to do, there's usually ways to do it in a far more straightforward way.

One way is arrays. Another way is just not storing the data at all -- you don't have to keep absolutely everything stored in memory at all times. If you only intend to use the lines one at a time, there's no point -- just use them as you read them, then throw them away.
# 7  
Old 03-18-2014
Quote:
Originally Posted by Corona688
This is a poor example because, if someone tried to name an array `rm -Rf ~/`, eval would happily execute that and erase your home directory.

We get dozens and dozens of requests for dynamic variable names, but they don't generally make much sense. How would you even use those variables afterwards? If you take a good look at what you're trying to do, there's usually ways to do it in a far more straightforward way.

One way is arrays. Another way is just not storing the data at all -- you don't have to keep absolutely everything stored in memory at all times. If you only intend to use the lines one at a time, there's no point -- just use them as you read them, then throw them away.
I agree with the use of eval, but I'm confused with your response to my example where I'm suggesting arrays instead of eval. In what way is this a poor example?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Setting default values for variables

I have a variable I want to use in bash script. The user will pass an argument to the script and I will store it in `arg_fql`. If the user does not pass the variable, I still never set arg_fql, but I set another variable to a default. However, if the user passes a value, `arg_fql` will be set to... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Shell Programming and Scripting

Shell Script for Setting Env Variables

Hello All. Good Afternoon. I need one small help regarding setting of env variables for a particular host by getting it from the DB. For ex : 1. I am using LOCALHOST. 2. When I run a ./hostset.sh it should pick up the Oracle home details from associated DB and set it. Please... (1 Reply)
Discussion started by: PavanPatil
1 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. Emergency UNIX and Linux Support

Problem setting environment variables from script

Hi all! I know that environment variables can be set on the .bashrc file, but I need to set them from a sh script. I saw a lot of websites that teach this but it doesn't work for me. #!/bin/sh DEKTOP=$DESKTOP=:/home/rrodrigues/Desktop export DESKTOP if I do echo $DESKTOP returns me... (10 Replies)
Discussion started by: ruben.rodrigues
10 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 Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

7. UNIX for Dummies Questions & Answers

Setting env variables using script

Hi, I wrote two small scripts to set env variables in a shell. java_env.csh #!/bin/csh -fn setenv JAVA_HOME '/scratch/software/jdk1.5.0_11' setenv PATH $PATH':'$JAVA_HOME'/bin' and run it using csh ./java_env.csh But the env variables are not set. I tried running each line on the... (5 Replies)
Discussion started by: NoviceAmod
5 Replies

8. UNIX for Advanced & Expert Users

Setting global variables with BASH/Linux

I am using functions in a script and for some strange reason the EXPORT command doesnt seem to be making my variables global. Anyone got any ideas? I am using one function to pass some output top another using the pipe command, eg Function 1 | Function 2 Function 2 reads the value... (3 Replies)
Discussion started by: gregf
3 Replies

9. 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

10. IP Networking

Setting up BIND with dynamic IP

I'm a UNIX newbie, so I know I'll be making foolish errors here... I'm trying to set up my Mac OS X 10.2 server to serve my domain name. I've got Apache running reasonably well , and I know Sendmail is up and running... Now, I seem to need to get BIND running for the domain name, and it... (2 Replies)
Discussion started by: caphector
2 Replies
Login or Register to Ask a Question