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
vijay.shad
  • 2,354
  • 6
  • 26
  • 33

3 Answers3

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.

Enrico Carlesso
  • 6,630
  • 4
  • 33
  • 41
  • Hi enrico, after removing that $ i am getting linux-softwares/launchers/join_files.sh: 51: FILES[0]: not found what is this? – vijay.shad Feb 28 '10 at 20:37
  • 1
    Remember your question of some minutes ago? Spaces hurts :) Just remove spaces after FILES[$file_count] – Enrico Carlesso Feb 28 '10 at 20:42
  • ohh yes. Sorry for the lost lesion. :) – vijay.shad Feb 28 '10 at 20:48
  • 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. – vijay.shad Mar 01 '10 at 06:34
  • It works for me. But not my script.How can i give my script to you? – vijay.shad Mar 01 '10 at 07:37
  • Put it on pastebin.org, or edit your first post adding it to the end. – Enrico Carlesso Mar 01 '10 at 07:56
  • With the only edit http://pastebin.org/99313 highlighted, it works on my system... The error does not shows to me... – Enrico Carlesso Mar 01 '10 at 09:02
  • By the way, using single marks like VAR='$old_var' the expression is not evaulated, I mean, echo $VAR will show $old_var insted of it contents. Use double marks VAR="$old_var". – Enrico Carlesso Mar 01 '10 at 09:04
  • Does not works for me. Do you see any other reason of this. What error i get is "linux-softwares/launchers/join_files.sh: 60: FILES[0]=/home/vijay/downloads/Reservoir/Reservoir.avi: not found". – vijay.shad Mar 01 '10 at 11:05
  • Is your file still like the one posted? 'Couse FILES[0] does not look to lie on the 60th line. The version you posted works for me returning: `$ ./99305.download Preparing to join avi files. Enter file name > test.avi You entered: test.avi .... Verifing file integrity. Do you want to add more files. Please enter yes or no no preparing join command Going to execute command mencoder -oac copy -ovc copy -o joined-output.avi size = 1 Going to execute command mencoder -oac copy -ovc copy -o joined-output.avi Going to execute command mencoder -oac copy -ovc copy -o joined-output.avi test.avi` – Enrico Carlesso Mar 01 '10 at 11:19
  • Any suggestion what may go wrong. This is my only second shell script. I am not able to contemplate anything. :( – vijay.shad Mar 01 '10 at 13:05
  • http://pastebin.org/99420 This is the content form original file. I have added two echo statement for around array assignment. – vijay.shad Mar 01 '10 at 13:29
  • Ok. Also this one is working for me. What is your default shell? ls -l /bin/sh points to bash? Really I cannot understand what's the problem, it's working perfectly on my environment... – Enrico Carlesso Mar 01 '10 at 13:52
  • Hi, output of the your command "vijay@vijay-laptop:~$ ls -l /bin/sh lrwxrwxrwx 1 root root 4 2009-08-28 00:01 /bin/sh -> dash " is this okay. I am using default shell on ubuntu 9.10. – vijay.shad Mar 01 '10 at 13:55
  • NOT DASH! :) use bash, please! just set #!/bin/bash in the first line of your script, please! – Enrico Carlesso Mar 01 '10 at 14:27
  • Yes sir, That did worked, Thanks for all your helps. now reading for the differences between both. thanks again. – vijay.shad Mar 01 '10 at 14:52
  • It has been a pleasure, and a new lesson for me... Avoid dash! :D – Enrico Carlesso Mar 01 '10 at 15:31
1

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

files+=( "$file" )
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
ghostdog74
  • 307,646
  • 55
  • 250
  • 337