use of the enhanced version of test builtin command (double brackets)

fix some misuse test command ( quote variable strings and use eq/ne for integers )
remove unecessary semi-colon at the end of each if ( use it only if the "then" is on the same line )
adding some TODO's
and may be other minor changes...
This commit is contained in:
Matthieu Fronton 2012-08-15 16:51:39 +02:00
parent 51fb9cb7cd
commit 33a32b47db

View file

@ -1,16 +1,21 @@
#!/bin/bash #!/bin/bash
# TODO : test if the script is executed as root
echo "======== YunoHost Installation ========" echo "======== YunoHost Installation ========"
echo "======== Check dependences ========" echo "======== Check dependences ========"
DOMAIN=$(hostname -d)
DOMAIN="$(hostname -d)"
dpkg -l | grep -q lsb-release dpkg -l | grep -q lsb-release
if [ $? = 1 ]; # @abeudin : Using double brackets is more "secure" (kind of) => http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS
if [[ $? -eq 1 ]]
then then
apt-get update -qq apt-get update -qq
apt-get install lsb-release -y apt-get install lsb-release -y
fi fi
if [ ! -f /etc/yunohost/yunohost.conf ]; if [[ ! -f /etc/yunohost/yunohost.conf ]]
then then
mkdir /etc/yunohost/ mkdir /etc/yunohost/
cat << EOF > /etc/yunohost/yunohost.conf cat << EOF > /etc/yunohost/yunohost.conf
@ -35,28 +40,28 @@ fi
echo "======== Check domain ========" echo "======== Check domain ========"
whiptail --title "Yunohost Domain" --yesno "Your domain is $DOMAIN\nDo you want to change this?" 8 78 whiptail --title "Yunohost Domain" --yesno "Your domain is $DOMAIN\nDo you want to change this?" 8 78
YESNO=$? YESNO=$?
if [ $YESNO = 255 ]; if [[ $YESNO -eq 255 ]]
then then
exit 1 exit 1
elif [ $YESNO = 0 ]; elif [[ $YESNO -eq 0 ]]
then then
DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3) DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3)
fi fi
if [ $DOMAIN = '' ]; if [[ -z "$DOMAIN" ]]
then then
DOMAIN=$(hostname -d) DOMAIN="$(hostname -d)"
fi fi
whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78
if [ $? = 0 ]; if [ $? -eq 0 ]
then then
echo "======== Update hostname ========" echo "======== Update hostname ========"
# Update hostname # Update hostname
echo "yunohost" > /etc/hostname echo "yunohost" > /etc/hostname
grep -q "yunohost.$DOMAIN" /etc/hosts grep -q "yunohost.$DOMAIN" /etc/hosts
if [ $? = 1 ]; if [[ $? -eq 1 ]]
then then
echo -e "127.0.0.1\tyunohost.$DOMAIN\tyunohost" > /etc/hosts echo -e "127.0.0.1\tyunohost.$DOMAIN\tyunohost" > /etc/hosts
/etc/init.d/hostname.sh start /etc/init.d/hostname.sh start
fi fi
@ -65,19 +70,22 @@ then
#Get gpg key #Get gpg key
wget -O- http://lemonldap-ng.org/_media/rpm-gpg-key-ow2 -q | apt-key add - -qq wget -O- http://lemonldap-ng.org/_media/rpm-gpg-key-ow2 -q | apt-key add - -qq
wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq
# TODO : use custom yunohost.sources.lits file and store it in sources.list.d
grep -q "lemonldap-ng.org" /etc/apt/sources.list grep -q "lemonldap-ng.org" /etc/apt/sources.list
if [ $? = 1 ]; if [[ $? -eq 1 ]]
then then
echo "deb http://lemonldap-ng.org/deb squeeze main" >> /etc/apt/sources.list echo "deb http://lemonldap-ng.org/deb squeeze main" >> /etc/apt/sources.list
fi fi
grep "yunohost" /etc/apt/sources.list grep "yunohost" /etc/apt/sources.list
if [ $? = 1 ]; if [[ $? -eq 1 ]]
then then
if [ $(lsb_release -is) = "Debian" ]; if [[ "$(lsb_release -is)" = "Debian" ]]
then then
echo "deb http://repo.yunohost.org/ squeeze main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ squeeze main" >> /etc/apt/sources.list
echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list
elif [ $(lsb_release -is) = "Ubuntu" ]; elif [[ "$(lsb_release -is)" = "Ubuntu" ]]
then then
echo "deb http://repo.yunohost.org/ precise main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ precise main" >> /etc/apt/sources.list
echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list
@ -86,24 +94,36 @@ then
#Update repo #Update repo
apt-get update -qq apt-get update -qq
if [ $? != 0 ] ; if [[ $? -ne 0 ]]
then then
echo "Update Repo Failure" # TODO : rollback (restore legacy configuration for hostname, sources.list, ...)
exit 1 echo "Update Repo Failure"
exit 1
fi fi
echo "======== Install ========" echo "======== Install ========"
#add answer in debconf db #add answer in debconf db
debconf-set-selections debconf debconf-set-selections debconf
#Install yunohost packages #Install yunohost packages
debconf-apt-progress --logfile /var/log/yunohost.log --logstderr /var/log/yunohost.error -- apt-get -y install yunohost yunohost-config yunohost-config-postfix postfix postfix-ldap postfix-policyd-spf-perl debconf-apt-progress \
if [ $? != 0 ] ; --logfile /var/log/yunohost.log \
--logstderr /var/log/yunohost.error \
-- \
apt-get -y install \
yunohost \
yunohost-config \
yunohost-config-postfix \
postfix postfix-ldap \
postfix-policyd-spf-perl
if [[ $? -ne 0 ]]
then then
echo "Install yunohost Failure" # TODO : warn the user that no rollback will be performed and list modified files
exit 1 echo "======== Installation failed ========"
exit 1
else else
service slapd restart service slapd restart
service apache2 restart service apache2 restart
service dovecot restart service dovecot restart
service postfix restart service postfix restart
@ -112,10 +132,9 @@ then
service nscd restart service nscd restart
service nslcd restart service nslcd restart
echo "======== Installation success ========" echo "======== Installation success ========"
fi fi
else else
echo "======== Installation cancelled ========" echo "======== Installation cancelled ========"
exit 1 exit 1
fi fi