Need to generate an array based on user input
I need to generate a range of variables based on user input. The user will first choose the number of variables in the range, then assign a value to each of the variables. I'm trying to do this in a loop but I'm running into problems.
This is what I have to create the range of variables: Code:
read -p "Enter a number for range of variables: " foo Code:
var1 var2 var3 var4 var5 Code:
for i in `myfunction`; do Code:
var1 but Code:
1234 Why isn't the value being assigned to the variable? |
let treats the expression as an arithmetic expression, so if you pass a string there, don't expect to get anything reasonable.
What are you trying to do in the first place? Why don't you just use arrays? |
I'm working on a repartitioning script for Android
I know I can do something like this... Code:
name1=boot; name2=system; name3=data; name4=cache; name5=media I'm trying to figure out a way to assign a value to a string of text, as it's created in the script, based on the user's input. I figured that was the case with the 'let' command, but trying to do something like... Code:
$i=$value I've also tried... Code:
`echo $i`=`echo $value` I'm trying to do it this way because the values of 'size1 size2 size3 etc...' can be changed at any point and I want to be able to use one script and just plug in the new values of those variables. Does that make any sense at all? I guess when it's all said and done, I'd like to be able to change the values of the variables in both $NAME and $SIZE, on the fly. Is that possible? |
You can probably accomplish this with declare or even read
Code:
foo=bar Code:
foo=bar I still think your best bet is to use arrays. something like Code:
read -a vars Code:
for i in "${vars[@]}"; do #iterating over the values Code:
for i in "${!vars[@]}"; do #iterating over the indices If you want to read the names, one per line, you can do something like Code:
declare -a names Code:
read -p "enter number of names: " number |
Quote:
Agreed, that is much more effective than what I was trying to do. Thank you very much for your help. |
Quote:
|
+1 to konsolebox. I was thinking the same thing.
How can I use array variables? http://mywiki.wooledge.org/BashFAQ/005 The reason being that trying to create and use dynamic variable names is difficult and non-transparent. How can I use variable variables (indirect variables, pointers, references) or associative arrays? http://mywiki.wooledge.org/BashFAQ/006 Associative arrays let you use the same base array name, with a separate index for each string given. This means there's no need to worry about the limitations on variable names. They're much cleaner, safer, and easier to use than indirect variable creation. Just be sure to declare the array globally, and not inside the function, if the values need to be used outside of it (although bash 4.2 has a new -g option to declare that you can use instead). In addition: Code:
for i in `myfunction`; do This is an incredibly common mistake. A for loop is for iterating over a list of words. But if there's whitespace involved then you cannot safely generate a list with a command substitution like this, due to the shell's word-splitting. You need to use a while+read loop or some other technique. PS: $(..) is highly recommended over `..` PPS: External tools like tr and sed can often be replaced by bash's built-in string manipulation features. |
All times are GMT -5. The time now is 08:40 PM. |