I'm writing a bash script to handle the PPA's on Ubuntu & derivatives. I was wondering how to know if I'm handling this the right way.
So, the script works (flawlessly), but I posted it to a blog and got this feedback :
The way you wrote you script is the one we all use when we begin : we don't know the variables, so we use traditionnal commands, and in the end if we remove the "#" arguments from the script, the code is sad (maybe he meant 'poor' ?)
Here is a sample :
#! /bin/bash
SCRIPT="ppa-tool"
VERSION="2.0.1"
DATE="2014-03-26"
RELEASE="$(lsb_release -si) $(lsb_release -sr)"
[ -n "`echo "$1" | grep ppa`" ] && PPA=$1
[ -n "`echo "$2" | grep ppa`" ] && PPA=$2
helpsection()
{
echo -e "Usage : $SCRIPT [OPTION]... [PPA]...
-h, --help shows this help
-c, --check check if [PPA] is available for your release
Version $VERSION - $DATE"
exit 0
}
error() { echo -e "$SCRIPT - Oops, something went wrong\nTry « $SCRIPT --help » for more information." && exit 0 ; }
ppa_verification()
{
wget http://ppa.launchpad.net/$(echo $PPA | sed -e 's/ppa://g')/ubuntu/dists -O /tmp/"$SCRIPT-check.tmp" -q
if [[ -n "`cat "/tmp/$SCRIPT-check.tmp" | grep $(lsb_release -sc)`" ]] ; then
echo -e "$SCRIPT : '$PPA' is available for $RELEASE"
else
echo -e "$SCRIPT : '$PPA' is NOT available for $RELEASE"
fi
rm "/tmp/$SCRIPT-check.tmp"
}
[ "$1" == "--help" ] || [ "$1" == "-h" ] && helpsection
if [ "$1" == "--check" ] || [ "$1" == "-c" ] || [ "$1" == "check" ] ; then
[ -n "`echo $2`" ] && ppa_verification && exit 0
error
fi
error
(complete script - also in French)
Apparently, my syntax is bad, but I don't see what I could do to improve this, and I don't understand the feedback I recieved. Could you help me figure it out ?
EDIT: From Unix&Linux (where I first posted, but was redirected here), someone said it wasn't good to use capital letters for variables. So $SCRIPT should become $script.