Visit Our UNIX and Linux User Community


bash Script: Issue with iterating Directory and store into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash Script: Issue with iterating Directory and store into array
# 1  
Old 08-26-2009
Error bash Script: Issue with iterating Directory and store into array

Hi all,

I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array.

Code:
cd $CPFs
    k=0
    for i in *
    do
        if [ -d "$i" ]
        then
            ARRs[$k]="$i"
            k=$(($k+1))
            #echo "$i"
        fi
    done

I got this output
Code:
script.sh: 42: ARRs[0]=1: not found
script.sh: 42: ARRs[1]=2: not found
script.sh: 42: ARRs[2]=3: not found

Line # 42 is the last line(EOF) of my script. The 1,2,3 are directories which is in the right hand side of ARRs[$j]

Thanks in Advance
# 2  
Old 08-26-2009
Can't see an error in this snippet - there must be some error elsewhere in your script. Maybe add a set -x in the head of the script and post the output here.
# 3  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Can't see an error in this snippet - there must be some error elsewhere in your script. Maybe add a set -x in the head of the script and post the output here.
Hey,

The matter is when I comment the ARRs[$k]="$i" line, then it works fine. Here is the output you asked for




Code:
+ [ -d <HomeFolder>/Documents/test ]
+ cd <HomeFolder>/Documents/test_bck/required
+ k=0
+ [ -d 1 ]
+ ARRs[0]=1
difference.sh: 1: ARRs[0]=1: not found
+ k=1
+ [ -d 2 ]
+ ARRs[1]=2
difference.sh: 1: ARRs[1]=2: not found
+ k=2
+ [ -d 3 ]
+ ARRs[2]=3
difference.sh: 1: ARRs[2]=3: not found
+ k=3
+ [ -d 4 ]

# 4  
Old 08-26-2009
Have you used a dos/windows editor?
# 5  
Old 08-26-2009
Quote:
Originally Posted by Franklin52
Have you used a dos/windows editor?
I am using linux bash shell (using ubuntu 9.04 kernal), I don't have any windows partition.
# 6  
Old 08-26-2009
You have a shebang in the top?
# 7  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
You have a shebang in the top?

Yes
Code:
#!/bin/bash


Previous Thread | Next Thread
Test Your Knowledge in Computers #282
Difficulty: Easy
Time-sharing operating systems schedule tasks for efficient use of the system and may also include accounting software for cost allocation of processor time, mass storage, printing, and other resources.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck. Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification. du -sh * | sort -rh 139G Dir_1 84G Dir_2 15G Dir_3 ls -l Dir_1 drwx------ 2... (3 Replies)
Discussion started by: 308002184
3 Replies

3. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Iterating awk over a directory of files?

I need to run the same awk function over an entire directly of files. This is the awk: awk '{$(NF+1)=1}1' Is there a way that I can run this command once over all of the files, along the lines of: awk '{$(NF+1)=1}1' * so that I do not have to run this several times? My main concern is... (2 Replies)
Discussion started by: owwow14
2 Replies

5. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

6. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

7. Shell Programming and Scripting

Script to store the variable in a table or array.

Hi, I have few variable say 10 ex:- l_pc_291334_01_0_01_00.cmp l_pc_441133_50_0_02_00.cmp l_pc_441133_55_0_02_00.cmp Each variable value is coming via loop on a table. I want to create a script that stores these value to a table or array ( But one by one not all at one time as... (4 Replies)
Discussion started by: amitkumar.b2
4 Replies

8. Shell Programming and Scripting

How to store files names from a directory to an array

Hi I want to store the file names into an array. I have written like this but I am getting error. declare -A arr_Filenames ls -l *.log | set -A arr_Filenames $(awk '{print $9}') index=0 while (( $index < ${#arr_Filenames })); do Current_Filename=${arr_Filenames} ... (5 Replies)
Discussion started by: dgmm
5 Replies

9. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

Featured Tech Videos