How to assign a variable to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to assign a variable to an array
# 1  
Old 09-05-2010
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 assigned it to check to see if the file exist. Thanks.
# 2  
Old 09-05-2010
something like
Code:
#!/bin/bash
i=0
while true   # read user input
do
   read -p "enter file name (blank if finished) : "
   [ -z "$REPLY" ] && break
   FILE[$i]=$REPLY
   ((i++))
done
for ((i=0; i<${#FILE[@]}; i++))   # check if files exist
do
   F=${FILE[$i]}
   if [ -f "$F" ]
   then echo "File $F exists"
   else echo "File $F does not exist"
   fi
done

# 3  
Old 09-05-2010
Maybe it's better - and sure it's easier - to check files before :
Code:
#!/bin/bash
i=0
while true   # read user input and check files
do
   read -p "enter file name (blank if finished) : " F
   [ -z "$F" ] && break
   [ -f "$F" ] || { echo "File does not exist" && continue; }
   FILE[$i]=$F
   ((i++))
done
for ((i=0; i<${#FILE[@]}; i++))   # print files
do
   echo -e "File number $i\t: ${FILE[$i]}"
done

# 4  
Old 09-05-2010
Thanks. Your example helped.
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 can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 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

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. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

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

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

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 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