6

I want my script to define an empty array. array values should be added if predefined condition gets true. for this what i have done is

declare -a FILES
file_count=0
if [ "$file_ext" != "$SUPPORTED_FILE_TYPE" ] ; then
        echo "$file_ext is not supported for this task."
else
        $FILES[$file_count] = $filename
        file_count=$file_count+1
fi

when executing this script i am getting some error like this

linux-softwares/launchers/join_files.sh: 51: [0]: not found
1

3 Answers 3

3

When settings data in array does not recall with $:

declare -a FILES
file_count=0
if [ "$file_ext" != "$SUPPORTED_FILE_TYPE" ] ; then
        echo "$file_ext is not supported for this task."
else
        FILES[$file_count]=$filename
        file_count=$file_count+1
fi

FILES without $.


This works for me:

#!/bin/bash
declare -a FILES
file_count=0

file_ext='jpg'
SUPPORTED_FILE_TYPE='jpg'
filename='test.jpg'

if [ "$file_ext" != "$SUPPORTED_FILE_TYPE" ] ; then
        echo "$file_ext is not supported for this task."
else
        FILES[$file_count]=$filename
        file_count=$(($file_count+1))
fi

As you see, a little modification $(( )) for math operation, but the FILES assignements is the same...


As pointed out after lots of tests, Ubuntu default shell seems to be dash, which raised the error.

Sign up to request clarification or add additional context in comments.

17 Comments

Hi enrico, after removing that $ i am getting linux-softwares/launchers/join_files.sh: 51: FILES[0]: not found what is this?
Remember your question of some minutes ago? Spaces hurts :) Just remove spaces after FILES[$file_count]
ohh yes. Sorry for the lost lesion. :)
The array assignment does not seems working "FILES[$file_count]=$filename" results in a error message "files.sh: 61: FILES[0]=/home/vijay/reservoir.txt". Can you tell what is the problem.
It works for me. But not my script.How can i give my script to you?
|
1

To add an element to the end of an array, use the += operator (since bash 3.1 in 2004):

files+=( "$file" )

Comments

0

you can write it this way as well

declare -a FILES
file_count=0
if [ "$file_ext" != "$SUPPORTED_FILE_TYPE" ] ; then
        echo "$file_ext is not supported for this task."
else
        FILES[((file_count++))]=$filename
fi

To: Vijay

tiny demonstration, list *.txt files in directory and put to array FILES

declare -a FILES
i=0
for file in *.txt
do
  FILES[((i++))]=$file 
done
# display the array
for((o=0;o<${#FILES};o++))
do
    echo ${FILES[$o]} $o
done

output

$ ./shell.sh
A.txt 0
B.txt 1
file1.txt 2
file2.txt 3
file3.txt 4

5 Comments

this does not work. The error message is "files.sh: 39: Syntax error: "(" unexpected (expecting ")")"
there's an extra "$" in variable FILES. remove it.
I did not get any $ where is that?
i already changed it. see my latest edit. originally, it was $FILES[((file_count++))]=$filename. which is wrong
see my edit demo. It works for me. Also you have accepted Enrico's answer, in his answer, he uses FILES[$file_count]=$filename, without the "$" sign. So i am puzzled why you still have problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.