user3019354
user3019354

Reputation: 51

Failure to move gzipped file

I'm attempting to write a program that moves zipped files that arrive in a directory, unzips them and then outputs the contents.

 #!/bin/bash
shopt -s extglob
echo "Press [CTRL+C] to stop.."
#begin loop
while :
do
    #search folder test_source for files and append names to array
    queue+=($(ls /home/ec2-user/glutton/test_source | egrep 'test[0-9]{1}.gz'))
    for i in $queue; do
        #move file in test_source to temp folder
        mv /home/ec2-user/glutton/test_source/${queue[i]} /home/ec2-user/glutton/temp
        #unzip file
        gunzip /home/ec2-user/glutton/temp/${queue[i]}
        #add new file name to variable unzipped
        unzipped=($(ls /home/ec2-user/glutton/temp | egrep 'test[0-9]{1}'))
        cat temp/$unzipped
        #Test for successful run
        exit_status=$?
        if [ $exit_status -eq 1 ]; then
            #If fail move file to bank and output
            mv /home/ec2-user/glutton/temp/$unzipped /home/ec2-user/glutton/logs/bank
            echo "Error! $unzipped moved to /home/ec2-user/glutton/logs/bank"
            #write to error.log
            echo "moved ${queue[i]} to bank due to error" >> /home/ec2-user/glutton/logs/error.log
        else 
            #If success delete file
            rm /home/ec2-user/glutton/temp/$unzipped
        fi
        #wipe variable
        unset unzipped
        i=$i+1
    done
#wipe array
unset queue
i=0
#go to top of loop
done

This has worked pretty well up until I added the unzipping feature and now my program outputs this error when attempting to move the .gz file:

   ./glutton.sh: line 11: test0.gz: syntax error: invalid arithmetic operator (error token is ".gz")

When I run the first part of my script in the command line it seems to work perfectly, but doesn't when I run it on its own, I'm pretty confused.

Upvotes: 0

Views: 86

Answers (1)

c3st7n
c3st7n

Reputation: 1971

Your main issue is that when you iterate an array like you are doing, you get the first item of the array, not the index. So in your case, $i is not a number, it is the filename (i.e. test1.gz) and it will only see the first file. The cleanest way I have seen to iterate the items in an array would be for i in "${arrayName[@]}".

Also, using '{1}' in your regex is redundant, the character class will already match only 1 character if you don't specify a modifier.

It shouldn't matter depending on the contents of your 'temp' folder, but I would be more specific on your egreps too, if you add -x then it will have to match the whole string. As it is currently, a file called 'not_a_test1' would match.

Upvotes: 1

Related Questions