Write filenames into an array variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write filenames into an array variable
# 1  
Old 04-24-2012
Write filenames into an array variable

Hello there,

I am a newbie to unix and i have been trying to code a shell script for bash shell which reads the files in a folder using ls and writes them into an array variable. But, i am running into several errors while running the script. The script is given below:

Code:
#!/bin/bash
script_loc=/export/home/oracle/HY_DUMP
cd $script_loc
num=0
for i in `ls -1 | grep -v ^'[.]\|[..]'$ | sed s/' '/'\\ '/g` ;
do
testarray[$num]=$i
echo ${testarray[$num]}
num = 'expr $num + 1'
done

It gives me the following error when i run the above code:

Code:
Usage: grep -hblcnsviw pattern file ...
script_1.sh: [.]\|[..]$: Not Found

When i run the command :
Code:
ls -1 | grep -v ^'[.]\|[..]'$ | sed s/' '/'\\ '/g

individually on the command line, i get a list of all the files neatly.

Any help is highly appreciated!

Moderator's Comments:
Mod Comment No cross-posting. Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-24-2012 at 02:29 AM.. Reason: code tags
# 2  
Old 04-24-2012
Try this grep -Ev '^\.$|\.\.$' for your grep statement.

You can also populate the array in one go with:

Code:
#!/bin/bash
script_loc=/export/home/oracle/HY_DUMP
cd $script_loc
IFS=$'\n' testarray=( $(ls -1) )

# 3  
Old 04-24-2012
Chubler,

Thanks for the reply. I could populate it by directly assigning the output of the ls -l to a variable but, the output of the ls symbol will be loaded into the 0 th position of the testarray , i.e, unix treats it as an normal variable but not as an array variable.

for the grep statement:
Code:
script_loc=/export/home/oracle/HY_DUMP
cd $script_loc
num=0
for i in `ls -1 | grep -Ev'^\.$|\.\.$ | sed s/' '/'\\ '/g` ;
do
       testarray[$num]=$i
        echo ${testarray[$num]}
        num = 'expr $num + 1'
done

here is the error it is throwing:
Code:
grep: illegal option -- E
grep: illegal option -- ^
grep: illegal option -- \
grep: illegal option -- .
grep: illegal option -- $
grep: illegal option -- |
grep: illegal option -- \
grep: illegal option -- .
grep: illegal option -- \
grep: illegal option -- .
grep: illegal option -- $
grep: illegal option --
grep: illegal option -- |
grep: illegal option --
grep: illegal option -- e
grep: illegal option -- d
grep: illegal option --
grep: illegal option -- /
Usage: grep -hblcnsviw pattern file . . .

Any help is highly appreciated.
-A
Quote:
Originally Posted by Chubler_XL
Try this grep -Ev '^\.$|\.\.$' for your grep statement.

You can also populate the array in one go with:

Code:
#!/bin/bash
script_loc=/export/home/oracle/HY_DUMP
cd $script_loc
IFS=$'\n' testarray=( $(ls -1) )

---------- Post updated at 08:50 AM ---------- Previous update was at 07:05 AM ----------

I tried out a new approach and something is going wrong. I am not sure what or why. Could anyone help me tweek the code and make it work?

Code:
#!/bin/bash
DIR=$HOME/HY_DUMP/
 
# failsafe - fall back to current directory
#[ "$DIR" == "" ] && DIR="."
 
# save and change IFS
OLDIFS=$IFS
IFS=$'\n'
cd $DIR 
# read all file name into an array
fileArray = ( $(`ls -l`) )
 
# restore it
IFS=$OLDIFS
 
# get length of an array
tLen=${#fileArray[@]}
echo $tLen
# use for loop read all filenames
for (( i=0; i<${tLen}; i++ ));
do
  echo "${fileArray[$i]}"
done

Error that i am encountering:

script.sh: syntax error at line 12: `(' unexpected

The error is occuring when i am trying to assign the value of ls -l to the variable filename.

ANy help on this.

-A


Moderator's Comments:
Mod Comment How to use code tags


---------- Post updated at 09:49 AM ---------- Previous update was at 08:50 AM ----------

Hello All,

I came up finally with a way to assign the values of the filename to an array variable but, the problem is all the values are getting assigned to the first position of the array variable. This is because the count/num variable pointing to the location of the array is not incrementing.
SmilieSmilie
Could anyone please help with this?Help is highly appreciated!!

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


Last edited by Franklin52; 04-24-2012 at 11:05 AM.. Reason: Please use code tags
# 4  
Old 04-24-2012
I am so glad you dropped the ls. You are using bash. Learn it's great features. I am not quite sure what you are trying to accomplish though this is what I gather.

1. Find all files ending with ".sql" in current directory
2. Place them all in an array.
3. Count how many elements are in the array.

Code:
#!/bin/bash
sqlfiles=( *.sql )
echo "${#sqlfiles[@]}"


Last edited by neutronscott; 04-24-2012 at 12:13 PM.. Reason: stray code tag
# 5  
Old 04-24-2012
NeutronScott,

Thanks for the reply. I realized ls was not a good command to go with. I replaced it. It works great. I figured how to increment as well. The problem now is i am not sure how to incorporate a for loop to print the values stored in the array.

I am trying to write all the filenames with .sql extention into an array and manipulate it likewise.

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

Any help with the printing of array is greatly appreciated.

Its just getting even more annoying when i am a newbie to this.
SmilieSmilie

Thanks for the reply
# 6  
Old 04-24-2012
Quote:
Originally Posted by amrutha0303
Code:
#!/bin/bash
cd $HOME/HY_DUMP
num=0
for filename in *.sql
do 
testarray[$num]=$filename
num = `expr $num+1`
echo $num
done
testnum=$num
num=0
echo $testnum

ok. `expr` is unnecessary. also, assignment is done without any spaces around the "=":
Code:
num=$((num+1))

is the least invasive way to change your approach.

but the code i gave is much better. if you still need to loop over files then to do some processing:
Code:
shopt -s nullglob  # edit: forgot this.
cd "$HOME/HY_DUMP"
sqlfiles=( *.sql )
testnum=${#sqlfiles[@]}

num=0
for file in "${sqlfiles[@]}"; do
    echo "$((num++)) $file"
done

echo "$testnum"

edit: forgot to set nullglob, so that if no file exist the array will be empty instead of having literal '*.sql'

Last edited by neutronscott; 04-24-2012 at 02:15 PM..
# 7  
Old 04-24-2012
Neuron,

The problem with your code on my machine is that it would throw errors like `(' unexpected syntax errors when i try something like you said.

That is reason why i had to take the longer way.

Could you please add an extension to my code to print the values in the array?

length of the array is indicated by variable testnum.

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