Assign user input to already declared array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign user input to already declared array
# 1  
Old 09-07-2011
Assign user input to already declared array

What I am doing is creating a top menu, which a user will select a choice with a number entry. That number corresponds to a string in an array. I then want to assign that response to another array I've already declared.

For example:
Code:
#!/bin/bash
colors=(red blue yellow)
red=(cherry fire)
blue=(baby sky)
yellow=(canary banana)

echo "Choose a color: 0) Red, 1) blue, 2) Yellow"
read choice

************
From there, the user will enter 1, 2 or 3. How do I then assign the value of say colors[2] to my array $yellow that I have already declared?

I'm sure this is easy, but I cannot figure this out. And yes, I'm new to scripting.

Last edited by Franklin52; 09-08-2011 at 08:05 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 09-07-2011
BASH is a string language with weak types, so copying an entire array around like that means having to handle it as a string.

There is an operator to turn an array into one string: "${ARR[*]}" But how're you going to do that when all you have is the name of the array, not the array itself? "${!ARR[*]}" doesn't work, the precedence is wrong, and you can't put the[*] outside it to do it backwards. You could shove it all into an eval, which can accomplish any weird syntax you want, but would be difficult. And then you have to turn it back into an array, wrapping it in another layer of stuff.

It's easy to turn a string into an array, though, and BASH has the ! operator to turn a variable name into a variable string. So why not keep your data as strings for easy retrieval?

Code:
#!/bin/bash

# Not arrays -- just strings.  Split them later.
colors="red blue yellow"
red="cherry fire"
blue="baby sky"
yellow="canary banana"

# This variable is used to read other variables.
name="colors"

while [ ! -z "${!name}" ]
do
        # Fill an array with variable name stored in 'name'.
        # i.e. When it's 'colors', it gets filled with ( red blue yellow )
        ARR=( ${!name} )
        echo "Choose a color counting from 0: ${ARR[*]}"
        read choice
        name="${ARR[$choice]}"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-07-2011
Yes, that is much easier and will work. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

4. Shell Programming and Scripting

every time user input create array perl

Hi, How to create array every time user input and store user input and display all array print " Enter input " my @input = split(' ', $input) chmop($input = <STDIN>; foreach ($input) { @array= @input; } print @array"\n"; (1 Reply)
Discussion started by: guidely
1 Replies

5. Shell Programming and Scripting

Assign value to array separated by #

Hi all , I have a string like para1#para2#para3 i want to assign para1 as first element para2 as second and so on i tried IFS=# set -A array para1#para2#para3 echo ${array} para1 para2 para3 i want echo ${array} para1 (2 Replies)
Discussion started by: max_hammer
2 Replies

6. Programming

assign array with a local var???

Hi all, I am just wondering is this (below code) valid in programming int xx = 10110; int i; long yy = {1,2,3,4,5,6,7,8,xx}; I compiled this using gcc will all working option successfully without getting any warning or error. I didn't see this kind of code in my past... (7 Replies)
Discussion started by: zing_foru
7 Replies

7. Shell Programming and Scripting

assign value to array variable

Hi, I have a piece of code as follows: i=0 while read LINE do var = "$LINE" i=$((i+1)) echo "${var}" done < file I want to assign value to the array var. However, when i execute the script i get a error. Please can you help me know what i am missing. I ultimately want to... (2 Replies)
Discussion started by: sunrexstar
2 Replies

8. Shell Programming and Scripting

How to assign a variable to an array

I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I... (3 Replies)
Discussion started by: tvb2727
3 Replies

9. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

10. Shell Programming and Scripting

sh shell user input and stote it to a array

Dear Friends, I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ? EX:- #! /bin/sh read DATA echo "DATA -" $DATA echo "DATA -" $DATA echo "DATA... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question