views:

381

answers:

2

Hi

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
+3  A: 

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
Hi enrico, after removing that $ i am getting linux-softwares/launchers/join_files.sh: 51: FILES[0]: not foundwhat is this?
vijay.shad
Remember your question of some minutes ago? Spaces hurts :) Just remove spaces after FILES[$file_count]
Enrico Carlesso
ohh yes. Sorry for the lost lesion. :)
vijay.shad
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
It does no seems an error...I Update my answer, take a look.
Enrico Carlesso
It works for me. But not my script.How can i give my script to you?
vijay.shad
Put it on pastebin.org, or edit your first post adding it to the end.
Enrico Carlesso
Please see herehttp://pastebin.org/99305
vijay.shad
With the only edit http://pastebin.org/99313 highlighted, it works on my system... The error does not shows to me...
Enrico Carlesso
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
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
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.aviYou entered: test.avi .... Verifing file integrity.Do you want to add more files. Please enter yes or nonopreparing join commandGoing to execute command mencoder -oac copy -ovc copy -o joined-output.avisize = 1Going to execute command mencoder -oac copy -ovc copy -o joined-output.aviGoing to execute command mencoder -oac copy -ovc copy -o joined-output.avi test.avi`
Enrico Carlesso
Any suggestion what may go wrong. This is my only second shell script. I am not able to contemplate anything. :(
vijay.shad
Try to repast your actual code.
Enrico Carlesso
http://pastebin.org/99420 This is the content form original file. I have added two echo statement for around array assignment.
vijay.shad
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
Hi, output of the your command"vijay@vijay-laptop:~$ ls -l /bin/shlrwxrwxrwx 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
NOT DASH! :) use bash, please! just set #!/bin/bash in the first line of your script, please!
Enrico Carlesso
Yes sir, That did worked, Thanks for all your helps. now reading for the differences between both. thanks again.
vijay.shad
It has been a pleasure, and a new lesson for me... Avoid dash! :D
Enrico Carlesso
A: 

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
this does not work. The error message is "files.sh: 39: Syntax error: "(" unexpected (expecting ")")"
vijay.shad
there's an extra "$" in variable FILES. remove it.
ghostdog74
I did not get any $ where is that?
vijay.shad
i already changed it. see my latest edit. originally, it was `$FILES[((file_count++))]=$filename`. which is wrong
ghostdog74
I have already removed that but. it does not help.
vijay.shad
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.
ghostdog74