how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download.
i writed this script

#!/bin/bash
wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master
CURRENDIR=/home/kernel-downloader

cd $CURRENDIR
rm $CURRENDIR/zipfiles 2>/dev/null

ls -1 *.zip
if [ $? -eq 0 ]
then
echo ".zip file found"
ls -1 $CURRENDIR/*.zip > $CURRENDIR/zipfiles
for i in `cat $CURRENDIR/zipfiles`
do
unzip $i
done
fi
if [ $? -eq 1 ]
then
echo "NOT found"
fi

but it not unpack zip file after download, i'm only getting this error

./script/test: line 5: cd: /kernel-downloader: No such file or directory
ls: cannot access *.zip: No such file or directory

can some give me idea's why i'm getting this error :slight_smile:

Are you sure the error msgs come from your script?

CURRENDIR=/home/kernel-downloader
cd $CURRENDIR

should give sth like cd: /home/kernel-downloader: No such file or directory if really missing.
Btw, rm $CURRENDIR/zipfiles should fail as well then.

wget downloads to the current directory, so in your case to wherever you were before you cd'd to somewhere else. So it's likely no .zip files are found making ls fail with your second err msg.

Aside, $? will NOT retain its contents but will be overwritten with the result of every single command you issue. If you need it later (... not found ...), save it to a variable immediately after the command that you are evaluating.

Further to @RudiC excellent post, the redirection of the error channel hides this error.

It is clear that the directory /home/kernel-downloader does not exist or that your Operating System (whatever that is?) needs the string containing the hyphen character in quotes.

cd "${CURRENDIR}"

Off-topic: Use hyphen characters in directory or file names at your own risk.
There is nothing to stop you using any character (except null) in a directory or file name, but hyphens, tabs and space characters will give you the most trouble.