[Level 3] Install script for Ubuntu apt-get Command

I wrote a shell script for install ubuntu package:

#!/bin/bash
#apt-get update; apt-cache search key word; ## update apt-get cache and search key word
#apt-get install pkg-name; apt-get upgrade; ## install package
#apt-get remove pkg-name;                   ## remove package
#grep install /var/log/dpkg.log             ## apt-get install history
#ls /var/lib/dpkg/info                      ## apt-get installed packages
#dpkg -L tomcat6                            ## list apt-get specific installed packages

## show usage
showUsage() {
  cat <<EOF
Usage:
  $0 [-u] [-s] package1 package2
  $0 [-u] [-s] all
  $0 -l
option:
  -l: list all package
  -u: force to update apt-get cache
  -s: get source only
Ex.
  $0 vim
  $0 -u all
Package:
`egrep "^install_.*()" $0 | sort | grep -v "install_template()" | cut -d'_' -f2 | cut -d'(' -f1 | sed -e 's/^/  /'`
EOF
}

listAllPackages() {
  echo "All packages list:"
  egrep "^[_]?install_.*()" $0 | sed -e 's/^_//' | sort | grep -v "install_template()" | cut -d'_' -f2 | cut -d'(' -f1 | sed -e 's/^/  /'
}

## initialize
init_apt_get() {
  declare -i nReturn=0

  if ! grep "apt update done" $apt_get_init_file > /dev/null 2>&1
  then
    echo $update
    eval $update || nReturn=$nReturn+1
    eval $updatedone
  fi

  if ! grep "apt upgrade done" $apt_get_init_file > /dev/null 2>&1
  then
    echo $upgrade
    eval $upgrade || nReturn=$nReturn+2
    eval $upgradedone
  fi

  return $nReturn
}

## mysql server
_install_mysql-server() {
  echo $install mysql-server
  eval $install mysql-server && return $?
}

## vim
install_vim() {
  echo $install vim
  eval $install vim && return $?
}

## java
install_sun-java6-jdk() {
  echo $install sun-java6-jdk
  eval $install sun-java6-jdk && return $?
}

## eclipse
install_eclipse() {
  echo $install eclipse
  eval $install eclipse && return $?
}

## python-mysqldb
install_python-mysqldb() {
  echo $install python-mysqldb
  eval $install python-mysqldb && return $?
}

## django, use source file to install django (1.2 beta), python ./setup.py build, python ./setup.py install
_install_python-django() {
  echo $install python-django python-django-doc
  eval $install python-django python-django-doc && return $?
}

## tomcat6
_install_tomcat6() {
  echo $install tomcat6
  eval $install tomcat6 && return $?
}

## dpkg-dev, for get source from apt repository
install_dpkg-dev() {
  echo $install dpkg-dev
  eval $install dpkg-dev && return $?
}

## vnc4server
install_vnc4server() {
  echo $install vnc4server
  eval $install vnc4server && return $?
}

## template, remove the underscore at the head of function name, then replace all "template", and remove/uncomment the line "return 0".
__install__template() {
  return 0 ## uncomment it for real function
  echo $install template
  eval $install template && return $?
}


####################################################### main

## sudo init
clear
sudo id > /dev/null 2>&1

# environment variables
apt_get_init_file=/etc/.apt_get_init_file && sudo touch $apt_get_init_file
declare -i nShift
bSourceOnly=false
bForceUpdate=false
sOption=""

# command alias, called by $cmd
#echo 'Y' > /tmp/Y
search='sudo apt-cache search'
update='sudo apt-get -y --force-yes update'
install='sudo apt-get -y --force-yes install'
remove='sudo apt-get -y --force-yes remove'
upgrade='sudo apt-get -y --force-yes upgrade'
source='sudo apt-get -y --force-yes source'

updatedone="sudo echo apt update done... >> $apt_get_init_file"
upgradedone="sudo echo apt upgrade done... >> $apt_get_init_file"


sGetopts=":lsu"
while getopts $sGetopts o
do
  case "$o" in
  l)
    listAllPackages
    exit 0
    ;;
  s)
    bSourceOnly=true
    sOption="$sOption -s"
    ;;
  u)
    bForceUpdate=true
    sOption="$sOption -u"
    ;;
  \?)
    showUsage
    exit 1
    ;;
  esac
done
nShift=$OPTIND-1
shift $nShift

if [ -z $1 ]
then
    showUsage && echo "Error without args, exit program..." && exit 1
fi

## init or update
if (! init_apt_get) || $bForceUpdate
then
  ## update cache
  eval $update
fi


## replace install option with source
if $bSourceOnly
then
  install=$source
fi


while true
do
  if [ -z $1 ]
  then
    break
  elif [ $1 == "all" ]
  then
    egrep "^install_.*()" $0 | grep -v "install_template()" | cut -d'_' -f2 | cut -d'(' -f1 | xargs $0 $sOption
    break
  elif ! grep "^install_$1()" $0 > /dev/null 2>&1
  then
    echo "Cannot find $1, exit program..." && exit 2
  elif ! install_$1
  then
    echo "Error in install $1, exit program..." && exit 3
  else
    shift
  fi
done



Wish this helps.

regards,
Stanley Huang

Comments

Popular posts from this blog

[Level 1] Rar tool for Solaris.

[Level 2] iif in Python