How can we assign value to an array variable from an external file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we assign value to an array variable from an external file?
# 1  
Old 07-10-2012
How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how??

I am using below code but its not working.

Code:
#!bin/bash
myarray[1] < file_name
echo ${mayarray[1]}

# 2  
Old 07-10-2012
You read with the read command.
Code:
read var[1] < file

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-10-2012
use declare -a as per bash manual
For example, here is a script that places first thre fields in three array elements
Code:
 
declare -a arr
while read arr[1] arr[2] arr[3];
do
        echo "arr[1]="${arr[1]}
        echo "arr[2]="${arr[2]}
        echo "arr[3]="${arr[3]}
done < $1

my input file:
Code:
1 2 3 4 5
11 22 33 44 55

the output of the script :
Code:
 
!x.sh zz
arr[1]=1
arr[2]=2
arr[3]=3 4 5
arr[1]=11
arr[2]=22
arr[3]=33 44 55

Note how 'unassigned 4th and 5th imput fields are bundled into last argument to read
This User Gave Thanks to migurus For This Post:
# 4  
Old 07-11-2012
suppose my files contains 5 different values separated by newline some what like
Code:
cat file
value1
value2
value3
value4
value5

what script should I use to assign these value to different element of an array variable.
# 5  
Old 07-11-2012
in bash, you can do:

Code:
readarray ARRAY < file

# 6  
Old 07-12-2012
Its not working.Script that I am using is

Code:
#!bin/bash
readarray myarray < sample
echo ${myarray[*]}

where sample is a file :
Code:
cat sample
value1
value2
value3

# 7  
Old 07-12-2012
Firstly, you need to specify a correct shebang:
Code:
#!/bin/bash

Secondly, mapfile AKA readarray can only be used in bash 4, so check with
Code:
/bin/bash --version

--
Alternatively you could use:
Code:
myarray=( $(<sample) )

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

2. Shell Programming and Scripting

Assign variable value from file to another with the same name

Hi All, I need to read two config files in a shell script. In that I need to assign a value from one config file to another. I 'm using bash. config_env.txt prefix=tab_ config_properties.txt table_name=${prefix}account So, when I read these two files in a shell script, I need... (6 Replies)
Discussion started by: shash
6 Replies

3. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

4. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

5. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

6. Shell Programming and Scripting

How to assign an array element to a variable

Hi every one.. I'm new to shell scripting... I would like to assign a single array element to a variable... Is it possible to do it.... Could any body help me.... (3 Replies)
Discussion started by: kaushik_87
3 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. Shell Programming and Scripting

Assign dscl output to variable as an array

Greetings folks, I am trying to assign the output of a dscl command (contains name<spaces>id) to a variable as an array. Currently I am piping the output into a tmp file, then reading the tmp file into an array, then parsing the array. I would like to bypass creating the tmp file portion of... (6 Replies)
Discussion started by: macnetdaemon
6 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question