endy
November 22nd, 2005, 11:03 PM
Hi, I thought it was about time I too learned some bash scripting and as I use Easynews' excellent services I thought I'd make something related to that.
As far as I can tell this works as it's supposed to, that is you can pass any valid download queue number (1 to 10) as an argument and the script will download those zip queues or pass it "a" to download all of them, pass no argument and it prints some help text.
What I was wondering was have I done the conditions for the if statements the best way? I can't help but think I've gotten lost regarding parenthesis.
Any comments or suggestions?
#!/bin/bash
# Download from easynews.com, with arguments
#
# Original command for reference:
# wget -H -c -nd -r -l1 --no-check-certificate --http-user="USERNAME" --http-passwd="PASSWORD" \
# -I"/news/" -Rremove "https://secure.members.easynews.com/zipmanager.html?editzip=Q01&.remove"
#
#
PARMS_1="-H -c -nd -r -l1 --no-check-certificate"
PARMS_2="-I"/news/" -Rremove"
USER="username"
PASS="password"
URL1="https://secure.members.easynews.com/zipmanager.html?editzip=Q"
URL2="&.remove"
QUEUE="01 02 03 04 05 06 07 08 09 10"
ALL_CMD="a"
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` queue1, queue2, etc..."
echo "Queue numbers can be 1 to 10 or use \"$ALL_CMD\" to get all queues."
echo "Exit."
exit
fi
# Test that the arguements are valid (within 1 to 10 or $ALL_CMD)
for arg in $*
do
if (( ($arg > 10) || ($arg < 1) )) && [[ $arg != $ALL_CMD ]]
then
echo "Bad argument:" \"$arg\"
echo "Only queue numbers between 1 to 10 and \"$ALL_CMD\" are allowed."
echo "Exit."
exit
fi
done
# Test for $ALL_CMD parameter and if so download ALL queues and exit, ignoring all other arguments
for arg in $*
do
if [[ $arg == $ALL_CMD ]]
then
for queue in `echo $QUEUE`
do
wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1$queue$URL2
done
exit
fi
done
# Otherwise download the selected queues
for arg in $*
do
if [[ $arg == 10 ]]
then wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1$arg$URL2
else
wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1"0"$arg$URL2
fi
done
exit 0
As far as I can tell this works as it's supposed to, that is you can pass any valid download queue number (1 to 10) as an argument and the script will download those zip queues or pass it "a" to download all of them, pass no argument and it prints some help text.
What I was wondering was have I done the conditions for the if statements the best way? I can't help but think I've gotten lost regarding parenthesis.
Any comments or suggestions?
#!/bin/bash
# Download from easynews.com, with arguments
#
# Original command for reference:
# wget -H -c -nd -r -l1 --no-check-certificate --http-user="USERNAME" --http-passwd="PASSWORD" \
# -I"/news/" -Rremove "https://secure.members.easynews.com/zipmanager.html?editzip=Q01&.remove"
#
#
PARMS_1="-H -c -nd -r -l1 --no-check-certificate"
PARMS_2="-I"/news/" -Rremove"
USER="username"
PASS="password"
URL1="https://secure.members.easynews.com/zipmanager.html?editzip=Q"
URL2="&.remove"
QUEUE="01 02 03 04 05 06 07 08 09 10"
ALL_CMD="a"
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` queue1, queue2, etc..."
echo "Queue numbers can be 1 to 10 or use \"$ALL_CMD\" to get all queues."
echo "Exit."
exit
fi
# Test that the arguements are valid (within 1 to 10 or $ALL_CMD)
for arg in $*
do
if (( ($arg > 10) || ($arg < 1) )) && [[ $arg != $ALL_CMD ]]
then
echo "Bad argument:" \"$arg\"
echo "Only queue numbers between 1 to 10 and \"$ALL_CMD\" are allowed."
echo "Exit."
exit
fi
done
# Test for $ALL_CMD parameter and if so download ALL queues and exit, ignoring all other arguments
for arg in $*
do
if [[ $arg == $ALL_CMD ]]
then
for queue in `echo $QUEUE`
do
wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1$queue$URL2
done
exit
fi
done
# Otherwise download the selected queues
for arg in $*
do
if [[ $arg == 10 ]]
then wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1$arg$URL2
else
wget $PARMS_1 --http-user=$USER --http-passwd=$PASS $PARMS_2 $URL1"0"$arg$URL2
fi
done
exit 0