Tuesday, April 16, 2013

[ Level 2 ] Ubuntu package install script.

I wrote an easy script for install Ubuntu packages.
#!/bin/bash

#
# This script is for install popular packages
#
#$: check package, if exists, skip the following prefix #^
#@: apt-get install packages
#!: unix command
#^@: apt-get install packages, depends on previous #$
#^!: unix command, depends on previous #$
#

showUsage() {
  cat <<EOF
Usage:
  $0
  $0 [package name]
Ex.
  $0
  $0 rdesktop
EOF
}

isPackageNotInstall() {
  dpkg -l $1 >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Package '$1' exists, skip it!"
    return 1
  else
    return 0
  fi
}

getContent() {
  declare -i begin
  declare -i end
  declare -i size
  begin=$1
  end=$2
  size=$end-$begin+1
  tail -n +$begin $0 | head -$size
}

getPackageName() {
  echo $1 | cut -d' ' -f2- | cut -d'#' -f1
}

getCommand() {
  echo "$line" | cut -d' ' -f2- | cut -d'#' -f1
}

die() {
  echo $1
  exit 1
}

aptGetInstall() {
  echo -n "Start install package '$@'..."
  apt-get -y install $@ && echo "OK" || die "FAIL"
}

commandExecute() {
  echo -n "Start execute command '$@'..."
  $@ && echo "OK" || die "FAIL"
}


########## main ##########
if [ ! -z "$@" ]
then
  apt-get -y install $@
else
  start_from_here_string="Package List, Start from here!"
  stop_at_here_string="Package List, Stop at here!"
  start_line_no=`grep -n "$start_from_here_string" "$0" | tail -1 | cut -d: -f1`
  stop_line_no=`grep -n "$stop_at_here_string" "$0" | tail -1 | cut -d: -f1`
  SKIP_FLAG=false
  CHECK_FLAG=false
  getContent $start_line_no $stop_line_no | while read line
  do
    if echo "$line" | grep -e "^#\\$" >/dev/null 2>&1
    then
      CHECK_FLAG=true 
      if isPackageNotInstall `getPackageName "$line"`
      then
        echo "Package (`getPackageName '$line'`) doesn't exist!"
        SKIP_FLAG=false
      else
        SKIP_FLAG=true
      fi
      continue
    elif echo "$line" | grep -e "^#^" >/dev/null 2>&1
    then
      $SKIP_FLAG && echo "Dependy exists, skip `getPackageName "$line"`" && continue
      line=`echo "\$line" | sed -e 's/^#^/#/'`
    else
      CHECK_FLAG=false
    fi

    echo $line | grep -e "^#@" >/dev/null 2>&1 && isPackageNotInstall `getPackageName "$line"` && aptGetInstall `getPackageName "$line"` 
    echo $line | grep -e "^#!" >/dev/null 2>&1 && commandExecute `getCommand "$line"`
  done
  exit
  
  cat $0 | grep ^#@ | awk '{print($2)}' | while read sPkg
  do
    echo "pkg install $sPkg"
  done
fi

exit
exit

##
## Package List, Start from here!
##

#### app ####
#@ vlc
#@ testdisk
#@ nfs-common
#@ openssh-server
#@ samba

#### system ####
#@ htop
##@ scim scim-pinyin scim-chewing scim-tables-zh scim-qtimm im-switch
##! im-switch -s scim

#### network ####
#@ chromium-browser
##@ openvpn
#@ wireshark
#@ transmission
#@ rdesktop

#### media ####
#@ gimp
#@ avidemux
#@ audacity
#@ freemind
#@ flashplugin-nonfree
##! sudo cp /usr/lib/flashplugin-installer/libflashplayer.so /usr/lib/chromium-browser/plugins
#@ mimms # download mms streaming

#### tools ####
#@ meld
#@ vim
#@ tree
##@ adacontrol # ptree
#@ minicom

#### developer tools ####
#@ maven2
#@ visualvm
#@ libssl-dev

#### install acrobat reader ####
#@ ia32-libs wget
##! wget http://ardownload.adobe.com/pub/adobe/reader/unix/9.x/9.1.1/enu/AdbeRdr9.1.1-1_i486linux_enu.bin
##! chmod +x AdbeRdr9.1.1-1_i486linux_enu.bin
##! ./AdbeRdr9.1.1-1_i486linux_enu.bin

#$ grub-customizer
#^! add-apt-repository ppa:danielrichter2007/grub-customizer
#^! apt-get update
#^@ grub-customizer

##
## Package List, Stop at here!
##

Wish this helps.

regards,
Stanley Huang