Note
Commands which produced wrong output on the target Ubuntu system have been commented out, but are left in place for instruction and reference.
Download an example script outline here, as organized in this guide, or just view the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #! /bin/sh
# Describe program's principal objective
#
# Initialize variables
#
# Assign parameters ($n) to named variables
#
# User execution confirmation (location varies)
#
# Declare repeated code functions
#
# !! MAIN SECTION !!
#
# Validity tests (setting exit status)
#
# Main code, if Valid
#
# Exit section: echo results, log errors
#
|
Download a sample script answering the Hello World challenge of Lesson 1 here, or just view the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #! /bin/bash
# My first script, presenting programming to the world
#
# Initialize variables
DISPLAY='World'
#
# Assign parameters ($n) to named variables
# if [! $1 = ''] && [-n $1] ; # fails without spaces around [ ]
# if [ $1 != '' ] && [ -n $1] ; # works with !# and single [ ]
# if [ ! $1 == '' ] && [ -n $1] ; # works to (!) negate the expression
if [ -n $1 ] && [ ! $1 = '' ] ; # works w/ single [ and double [[
then DISPLAY=$1
fi
#
# User execution confirmation (location varies)
# echo "Say Hello?"
# echo "$(tput setaf 1) Say Hello?$(tput sgr0)"
echo "$(tput setaf 1)Run this script? (y/n) $(tput sgr0)"
read RESP
if [ "$RESP" != 'y' ]; then
echo "Canceled"
exit 1
fi
#
# Declare repeated code functions (none declared)
#
# !! MAIN SECTION !!
#
# Validity tests (setting exit status)
#
# Main code, if Valid
#
# Exit section: echo results, log errors
# echo 'Hello World'
# echo 'Hello DISPLAY' ; # full quoting
# echo "hello DISPLAY" ; # partial quoting
# echo 'Hello $DISPLAY' ; # $-operator for evaluation
# echo "Hello $DISPLAY" ; # $ evaluation in partial quotes
# exit statements with user message display
echo "$(tput setaf 2) Hello $DISPLAY $(tput sgr0)"
#
|
Download a sample script answering the expressions topics from Lesson 2 here, or just view the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #!/bin/bash
# Describe program's principal objective
#
# Initialize variables
#
#
# Assign parameters ($n) to named variables
#
# User execution confirmation (location varies)
#
# Declare repeated code functions
#
# !! MAIN SECTION !!
#
# Validity tests (setting exit status)
#
echo
echo test variable expansion in quotes
#
COMMAND='git branch -a'
echo $COMMAND
# echo '$COMMAND'
echo "$COMMAND"
#
echo
echo test command execution in parentheses
#
COMMAND='git branch -a'
# echo $( "$COMMAND" )
$( echo $COMMAND )
# $( echo '$COMMAND' )
$( echo "$COMMAND" )
#
echo
echo test command list in parentheses
#
TEXT='embedded grouped commands'
# echo $(
# ' These statements test'
# " $TEXT. "
# )
#
# $(
# echo " These statements test"
# echo " $TEXT. "
# )
#
echo $(
echo "$(tput setaf 1) These statements test "
echo " $TEXT. $(tput sgr0)"
)
#
echo
echo test command group anonymous function
#
TEXT='an embedded anonymous function'
# echo ${
# echo 'These statements test'
# echo " $TEXT. "
# }
#
echo
echo 'test an integer calculation'
#
SEVEN=7
FIVE=5
# echo '$FIVE times $SEVEN is $(( FIVE * SEVEN ))'
echo "$FIVE times $SEVEN is $(( FIVE * SEVEN ))"
#
# Main code, if Valid
#
#
# Exit section: echo results, log errors
#
|
Download a sample script answering the Lesson 2 logic control topics here, or just view the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #!/bin/bash
# Describe program's principal objective
#
# Initialize variables
#
#
# Assign parameters ($n) to named variables
#
# User execution confirmation (location varies)
#
# Declare repeated code functions
#
# !! MAIN SECTION !!
#
# Validity tests (setting exit status)
#
#
# Main code, if Valid
#
echo
echo "iterative for loop"
#
echo "$(tput setaf 2)"
for NAME in 'in' 'inn' 'ian' 'eoin'
do
echo "$NAME"
done
echo "$(tput sgr0)"
#
echo
echo "integer for loop"
#
# for (( K=4; $K -ge 0; $K-- ))
# do
# echo "The negative of $K squared is: $(( K * -K ))"
# done
#
for $(( I=4; $I>=0; I-- ))
do
echo "$(tput setaf 2) The negative of $K squared is: $(( K * -K )) $(tput sgr0)"
done
#
echo
echo "case branching test"
#
OPT='YNmaybe'
# OPT='dontknow'
# OPT='whatever'
ANS=''
case $OPT in
Y*|y*)
ANS+="positive answer" ;;
*N*|*n*)
ANS+="negative answer" ;;&
*)
ANS+="ambiguous answer" ;;
esac
echo "$(tput setaf 2)$ANS$(tput sgr0)"
#
echo
echo "if branching test"
#
CHROMEVER='google-chrome-stable_current_'
if [[ $EUID -eq 0 ]]
then
PTN='i*86'
if [[ `uname -i` =~ $PTN ]]
then
CHROMEVER+='i386.deb'
else
CHROMEVER+='amd64.deb'
fi
echo "$(tput setaf 2) $CHROMEVER $(tput sgr0)"
else
echo "$(tput setaf 1) try again using sudo $(tput sgr0)"
fi
#
echo
echo "While / Until looping tests"
#
K=5
while [ $K -gt 0 ]
# until [[ $K -le 0 ]]
do
(( K-- ))
echo -e "$(tput setaf 2) $K squared is: $(( K ** 2 )) $(tput sgr0)"
done
#
echo
echo "select loop"
#
select NAME in 'in' 'inn' 'ian' 'eoin'
do
echo "$(tput setaf 2) $NAME $(tput sgr0)"
break
done
#
#
# Exit section: echo results, log errors
#
|
Download a sample script answering the Lesson 3 installer code challenge here, or just view the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | #! /bin/sh
# --- Program Principal Objective ---
# For Ubuntu up to 14.04 or other Ubuntu-based desktop systems:
# This script installs core utilities and general productivity apps.
#
# ##### VARIABLE DECLARATIONS
#
PROMPT=''
EXIT=0
OS=''
VERSION=''
OPER=''
APTMGR='apt-get'
REPOS=''
APT=''
PKGS=''
# default values before configuration
MIRRORS='MIRRORS=("http://us.archive.ubuntu.com/ubuntu'
MIRRORS+=',http://mirror.pnl.gov/ubuntu/'
MIRRORS+=',http://mirrors.centarra.com/ubuntu/'
MIRRORS+=',http://mirror.tocici.com/ubuntu/'
MIRRORS+=',http://mirrors.us.kernel.org/ubuntu/")'
#
# CONFIGURATION VALUES FROM FILE
#
# (none used)
#.
# ##### PARAMETER ASSIGNMENTS
#
# (none used)
#
# ##### FUNCTION DECLARATIONS
#
confirm() {
# uses $PROMPT, returns $EXIT
#
tput rev
echo -n $PROMPT
read -n 1 -p "? [Y/n]" RESPONSE
tput sgr0
echo
case $RESPONSE in
Y|y)
EXIT=0 ;;
N|n)
EXIT=1 ;;
*)
EXIT=2 ;;
esac
}
#
apt-manager() {
# returns APTMGR
#
dpkg -s 'apt-fast' > /dev/null 2>&1
if [ $? -ne 0 ]
then
APTMGR='apt-get'
else
APTMGR='apt-fast'
fi
}
#
apt-repos() {
# uses REPOS, APTMGR, returns APT
# calls apt-manager
#
apt-manager
# Install repositories listed in variable REPOS
APT=0
for NAME in $REPOS
do
APT+=1
apt-add-repository $NAME
done
# verify installation and update packages indexes
if [ $APT -ne 0 ]
then
echo -e "\e[1;32m Updating repository indexes \e[0m"
$APTMGR -y -f install && apt-get -y update
fi
}
#
apt-pkgs() {
# uses PKGS, APTMGR; returns APT
# calls apt-manager
#
apt-manager
# Install packages listed in variable PKGS
APT=0
for NAME in $PKGS
do
if [ $OPER = '' ]
Then OPER='install'
fi
case $OPER in
'purge')
dpkg -s $NAME > /dev/null 2>&1
if [ $? -eq 0 ]
then
APT+=1
echo -e "\e[1;33m Installed $NAME will be purged \e[0m"
$APTMGR -y purge $NAME
fi
;;
'install')
dpkg -s $NAME > /dev/null 2>&1
if [ $? -ne 0 ]
then
APT+=1
echo -e "\e[1;32m Missing $NAME will be installed \e[0m"
$APTMGR -y install $NAME
fi
;;
*) ;;
esac
done
# verify installation and update packages indexes
if [ $APT -ne 0 ]
then
echo -e '\e[1;32m Wait as system packages are updated \e[0m'
$APTMGR -y -f install && apt-get -y update
fi
}
#
apt-fast-config() {
# updates configuration file /etc/apt-fast.conf
# uses MIRRORS, sets /etc/apt-fast.conf
#
touch /etc/apt-fast.conf
sed -r "/MIRRORS=.*$/d" -i /etc/apt-fast.conf
sed -r "$ a\$MIRRORS" -i /etc/apt-fast.conf
}
#
apt-fast-install() {
# uses VERSION, EXIT, PROMPT
# returns PKGS
# calls apt-repos, apt-pkgs, confirm
#
# check if apt-fast is installed
dpkg -s 'apt-fast' > /dev/null 2>&1
if [ $? -eq 0 ]
then
PROMPT='Skip apt-fast mirror configuration'
confirm
if [ $EXIT -ne 0 ]
then
apt-fast-config
EXIT=0
fi
else
# determine REPOS for apt-fast
case $VERSION in
10*|11*|12*|13*)
REPOS='ppa:apt-fast/stable' ;;
14.04*)
REPOS='ppa:saiarcot895/myppa' ;;
*)
echo -e "\e[1;31m apt-fast is not released for $VERSION \e[0m" ;;
esac
#
if [ $REPOS != '' ]
then
PROMPT='Install program apt-fast'
confirm
if [ $EXIT -gt 0 ]
then EXIT=0 ; # not confirmed, reset EXIT
else
# install apt-fast
PKGS='apt-fast'
apt-repos
apt-pkgs
apt-fast-config
fi
fi
fi
}
#
# ##### CONFIRM EXECUTION
#
# check OS type
# Uses PROMPT, returns OS, VERSION, EXIT
read OS < /etc/issue
case $OS in
Ubuntu*)
ARRAY=($OS)
VERSION=${ARRAY[1]} ;;
Debian*) ;;
*)
echo "Not Debian or Ubuntu, exiting ..."
EXIT=1 ;;
esac
#
# Check privileges
if [ $EUID -ne 0 ]
then
echo -e "\e[1;31m try again using sudo \e[0m"
EXIT=1
else
# test user confirmation
PROMPT='Install packages for Kubuntu'
confirm
fi
#
if [ $EXIT -gt 0 ]
then
echo -e "\e[1;31m Package installation was canceled. \e[0m"
exit $EXIT
fi
#
# ##### MAIN VALIDITY TESTS
#
apt-fast-install
#
# ##### MAIN CODE
#
# universe/multiverse repositories
apt-add-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
#
# add repositories for: Cinelerra, Ubuntu tweaks, Rails
# REPOS='ppa:cinelerra-ppa/ppa ppa:tualatrix/ppa ppa:ubuntu-on-rails/ppa'
# apt-repos
# upgrade Firefox, replace latex-xft-fonts with ttf-lyx, add curl for Chrome
PKGS='curl google-chrome-stable firefox ttf-lyx ubufox'
apt-pkgs
#
# install desktop productivity apps
PKGS='blender dia filezilla freemind gimp git gnucash inkscape mypaint'
PKGS+=' openshot scribus shotwell xaralx xsane'
# PKGS+=' cinelerra'
apt-pkgs
#
# install desktop utility apps
PKGS='aptitude byobu cifs-utils diffuse dosbox dosemu'
PKGS+=' hplip-gui keepassx krdc kubuntu-restricted-extras lftp mc'
PKGS+=' nfs-common openvpn plasma-widget-lancelot putty recordmydesktop'
PKGS+=' screen shutter unison vlc whois wine wireshark xclip'
# PKGS+=' playonlinux ubuntu-tweak'
apt-pkgs
#
# install Sun (Oracle) java
# PKGS='sun-java6-bin sun-java6-fonts sun-java6-javadb sun-java6-jdk'
# PKGS+=' sun-java6-jre sun-java6-plugin'
# apt-pkgs
#
# install playonlinux windows game console
# wget -q "http://deb.playonlinux.com/public.gpg" -O- | apt-key add -
# wget http://deb.playonlinux.com/playonlinux_trusty.list -O /etc/apt/sources.list.d/playonlinux.list
# $APTMGR update
# $APTMGR install playonlinux
# install python and TeX tools
#
OPER='install'
PKGS='python-setuptools python-dev python-numpy python-qt4 python-qt4-gl python-vtk'
PKGS+=' build-essential texlive-full'
apt-pkgs
#
# install sphinx-tools
OPER='purge'
PKGS='python-configobj python-docutils python-pygments python-sphinx rst2pdf'
apt-pkgs
easy_install configobj docutils pygments sphinx rst2pdf
#
# ##### EXIT CODE
#
apt-get clean && apt-get update && apt-get upgrade
if [ $EXIT -eq 0 ]
then
PROMPT='Installation successful. Reboot now'
if [ $EXIT -gt 0 ]
then reboot &
fi
else echo "EXIT value is $EXIT"
fi
exit $EXIT
################################################################################
# 10/12/14 GARL -- Added apt-fast installation to speed routine
# 10/29/14 GARL -- Rewritten for TechCamp 2014 workshop
|