Write filenames into an array variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write filenames into an array variable
# 8  
Old 04-24-2012
Array assignments must not have spaces after the "=", I suspect you have:
Code:
sqlfiles= ( "$HOME/HY_DUMP"/*.sql )

instead of:
Code:
sqlfiles=( "$HOME/HY_DUMP"/*.sql )

Also this:
Code:
# save and change IFS 
OLDIFS=$IFS 
IFS=$'\n' 
 # read all file name into an array 
fileArray=( $(ls -l) )   
# restore it 
IFS=$OLDIFS

and this:
Code:
IFS=$'\n' testarray=( $(ls -1) )

are the same (in the above syntax IFS is set for 1 statement and automatically restored by the shell)
# 9  
Old 04-24-2012
Chubler,

i tried to remove the space between the = and still i had the same problem. Yes, the code line you gave is the same as the one i used in my script. Your code gave me an idea to use it.

Anyway, i end up with the same problem. There are syntax errors as i listed before.

I moved to another approach that i posted in the recent blog. I still have some problems but, it is better than my previous failed attempts.

Please look at it and let me know if you could tweek the problem in the latest script.

Appreciate your quick reply.

-A

---------- Post updated at 11:18 AM ---------- Previous update was at 11:09 AM ----------

NeutronScott,

I ran your script and this is error i am getting:


syntax error at line 2: `sqlfiles=' unexpected

-A
# 10  
Old 04-24-2012
If your experiences in the other thread are any indication, you are not using the bash shell. These problems will remain, until you do.
# 11  
Old 04-24-2012
Corona,

I reran the scripts using bash shell and they were throwing the same errors at me.

I am still looking for some help trying to print the values stored in the array for the code below:

Code:
#!/bin/bash
cd $HOME/HY_DUMP
num=0
for filename in *.sql
do 
testarray[$num]=$filename
(( num += 1 ))
echo $num
done
testnum=$num
num=0
echo $testnum

Any help with this, i would highly appreciate.
-A
# 12  
Old 04-24-2012
Code:
echo "${testnum[@]}"

# 13  
Old 04-24-2012
Quote:
Originally Posted by amrutha0303
Corona,

I reran the scripts using bash shell and they were throwing the same errors at me.
I suspect you still have spaces before the assignment as Chubler_XL noted.
# 14  
Old 04-24-2012
Corona,

I dont get what you mean. I have run the latest script i posted just above your post. I was able to assign the file names to the array. I am not sure how to look at the values stored in the array.

I need some help trying to print the values in the array.

-A

---------- Post updated at 12:08 PM ---------- Previous update was at 11:55 AM ----------

Hello All,

Here is the code i wrote to print the values assigned in the variable:

Code:
#!/usr/bin/env bash
cd $HOME/HY_DUMP
num=0
for filename in *.sql
do


testarray[$num]=$filename
#num=`expr $num + 1`
(( num += 1 ))
echo $num
#echo "new yo"
done
testnum=$num
num=0
echo $testnum
for filename in *.sql
do
echo ${testarray[$num]}
echo $num
(( num += 1))

done

And this is the output i am recieving:

[output]
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
25
HY_FILE_LOAD_Export.sql
0
HY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
1
HY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
2
HY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
3
HY_INDV_export.sqlHY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
4
HY_indv_PROGRAM_CWP_export.sqlHY_INDV_export.sqlHY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
5
hy_indv_service_map_export.sqlHY_indv_PROGRAM_CWP_export.sqlHY_INDV_export.sqlHY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
6
hy_prog_info_export.sqlhy_indv_service_map_export.sqlHY_indv_PROGRAM_CWP_export.sqlHY_INDV_export.sqlHY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
7
hy_program_bypass_export.sqlhy_prog_info_export.sqlhy_indv_service_map_export.sqlHY_indv_PROGRAM_CWP_export.sqlHY_INDV_export.sqlHY_INDV_PROGRAM_export.sqlHY_INDV_CONTACT_export.sqlHY_INDV_ADDRESS_export.sqlHY_FILE_LOAD_Export.sql
8
hy_program_close_export.sql
9
hy_program_service_area_export.sql
10
hy_questionaire_export.sql
11
hy_ref_table_export.sql
12
hy_security_group_export.sql
13
hy_security_grp_tbls_map_export.sql
14
hy_security_tables_export.sql
15
hy_service_area_county_export.sql
16
hy_service_area_export.sql
17
hy_truncate_tables_export.sql
18
hy_work_mgweb_load_export.sql
19
hy_work_mhmr_load_export.sql
20
hy_work_sas_load_export.sql
21
tt_batch_rank_export.sql
22
tt_cil_prog_list_export.sql
23
tt_csil_select_export.sql
24

[/output]

The wierdest thing with the output is some of the values are getting concatenated with the previous values.
Is there anyway to flush the value stored in filename after the value is being assigned to the array so that we could avoid this problem?

-A

Last edited by Scrutinizer; 04-24-2012 at 02:14 PM.. Reason: Code tags for data sample too..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Naming output files based on variable parameters and input filenames

Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
Discussion started by: bdeads
2 Replies

2. Shell Programming and Scripting

Storing filenames in an array in shell script

hi, i am writing a shell script in which i read a line in a variable. FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt" i want to create a array and store single file names in a array.. so the array should contain arr="s1.txt" arr="s2.txt" arr="s3.txt" arr="s4.txt" arr="s5.txt" how to... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

How to get filenames in a directory and write them in to a file?

I need to get the names of files which are starting with a string testfile. Also i want to create a XML file in the same location and write these file names into the XML. Ex: <path> <dir> <file>testfile1</file> </dir> <dir> <file>testfile2</file> </dir>... (4 Replies)
Discussion started by: vel4ever
4 Replies

4. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

Storing cutted filenames into variable

Hi, I'd like to write a script which works on various files file1.jpg, file2.jpg ..... These files are splitted and their names are something like file_.XXX. I'd like to merge them and convert them at some moment again in file.XXX also file1_1.jpg file1_2.jpg ... >file1.pdf file2_1.txt... (2 Replies)
Discussion started by: bsco
2 Replies

7. Homework & Coursework Questions

i have no understanding of how to write an array or use one, please help!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script that can check... (1 Reply)
Discussion started by: wendyshephard
1 Replies

8. Shell Programming and Scripting

i have no understanding of how to write an array or use one, please help!

im in a basic unix class and our professor speaks broken engliash so i can never understand what exactly we are doing in class and i have no prior experience with unix. we were given an assignment to make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script... (1 Reply)
Discussion started by: wendyshephard
1 Replies

9. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies
Login or Register to Ask a Question