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.

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

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

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

+ [ -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 ]

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.

You have a shebang in the top?

Yes

#!/bin/bash

Try this:

#!/bin/bash

i=0

cd /tmp

for f in *; do
        arr[i++]=${f}
done

echo ${arr[*]}

exit 0

when I tried to run your code I got this output.

check.sh: 9: arr[i++]=Resume_A_09_v2.pdf: not found
check.sh: 9: arr[i++]=SciTE.4115.in: not found
check.sh: 9: arr[i++]=kde-a: not found
check.sh: 9: arr[i++]=keyring-fghIJp: not found
check.sh: 9: arr[i++]=ksocket-a: not found
check.sh: 9: arr[i++]=orbit-a: not found
check.sh: 9: arr[i++]=orbit-root: not found
check.sh: 9: arr[i++]=plugtmp: not found
check.sh: 9: arr[i++]=pulse-dExDc8m05Dcj: not found
check.sh: 9: arr[i++]=seahorse-6OBRgA: not found
check.sh: 9: arr[i++]=ssh-zVGRKA3354: not found
check.sh: 9: arr[i++]=virtual-a.Zm5Csq: not found
check.sh: 11: Bad substitution

Strange - what does

bash --version

show?

Yeah for me too

the output for your query is

GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

Hm, mine is even older.

What happens to this?

a[0]=2
echo ${a[0]}

Might also want to try it on command line.

When I tried with terminal It works, but when I run the script, it shows the error.:confused:

new.sh: 3: a[0]=2: not found
new.sh: 4: Bad substitution

Ok, then try following:

echo $SHELL
which $SHELL

both are /bin/bash

/bin/bash
/bin/bash

Hm I have no idea currently why it does work on command line but not in a script. No clue, sorry.

No worries, anyway thank you for your great help.

I will update if I found something.

Regards,

Anish

---------- Post updated at 04:40 PM ---------- Previous update was at 12:58 PM ----------

When I changed the mode using chmod and I tried with ./ not sh the it worked fine.

Whenever I tried with sh script.sh, I got that error, but not with ./script.sh.

Thanks for the help.

i copy exxactly the code you put i change the valor of $CPFs
for a directory of my server, and the script work OK (HP-UX, with sh)....
the error is in line 42 but the code you put si more short than 42 lines, can you put all the code of you scritp

Tbh I never call any script with a shell in front of it. Glad you found it though :slight_smile:

when i write the my last time i don't see all you response but now i read...
I'm probing, now i'm in linux enviorement with bash and i has the same problem that you say.

$ sh new.sh
new.sh: 11: ARRs[0]=dir1: not found
new.sh: 11: ARRs[1]=dir2: not found

As i now in sh (i don't now version) you must declare as array de variable, for this porpuse is

man sh
" sh is the standard command interpreter for the system.  The current ver
     sion of sh is in the process of being changed to conform with the POSIX
     1003.2 and 1003.2a specifications for the shell.  This version has many
     features which make it appear similar in some respects to the Korn shell,
     but it is not a Korn shell clone (see ksh(1)). "
man ksh

" To assign values to an
      array, use set -A name value ....  The value of all subscripts must be
      in the range of 0 through 1023.  Arrays need not be declared.  Any
"

if you execute ./new.sh you execute under bash because is your shell by defect...and shell works Ok, you can optain the same result if you execute:

bash new.sh

I'm think that when you execute whith sh something happens and the error is the same that if yo make this shell in old unix version ( o may be better said old sh ) that they need you declare de arrays with
set -a ARRs valor1 valor2 .......
i'm not suere what is the problem but think this thinks can be relation...
(sorry i'm spanish and i don't explain so good in english)