Code clean up part 4

- use "[" instead of "test"
- remove double quotes where word splitting does not occur
- revise some "sed" statements
- use "((" construct where possible
- use "return" in place of long winded "if" statements
- use "+=" where possible
- use "type" instead of "which"
- use "[[" construct where appropriate
This commit is contained in:
Steven Penny 2014-03-08 23:09:09 -06:00
parent b42a6cb813
commit 663a6d55ae
1 changed files with 57 additions and 56 deletions

113
apt-cyg
View File

@ -68,19 +68,21 @@ function findworkspace()
cache=/setup
# work wherever setup worked last, if possible
if test -e /etc/setup/last-cache
if [ -e /etc/setup/last-cache ]
then
tmp="`head -1 /etc/setup/last-cache`"
cache="`cygpath -au "$tmp"`"
tmp=$(</etc/setup/last-cache)
cache=$(cygpath -au "$tmp")
fi
if test -e /etc/setup/last-mirror
if [ -e /etc/setup/last-mirror ]
then
mirror="`head -1 /etc/setup/last-mirror`"
mirror=$(</etc/setup/last-mirror)
fi
mirrordir="`echo "$mirror" | sed -e "s/:/%3a/g" -e "s:/:%2f:g"`"
mirrordir=$(sed '
s / %2f g
s : %3a g
' <<< "$mirror")
echo Working directory is $cache
echo Mirror is $mirror
mkdir -p "$cache/$mirrordir"
@ -89,36 +91,32 @@ function findworkspace()
function getsetup()
{
if test "$noscripts" == "0" -a "$noupdate" == "0"
(( noscripts || noupdate )) && return
touch setup.ini
mv setup.ini setup.ini-save
wget -N $mirror/$ARCH/setup.bz2
if [ -e setup.bz2 ]
then
touch setup.ini
mv setup.ini setup.ini-save
wget -N $mirror/$arch/setup.bz2
if test -e setup.bz2 && test $? -eq 0
bunzip2 setup.bz2
mv setup setup.ini
echo Updated setup.ini
else
wget -N $mirror/$ARCH/setup.ini
if [ -e setup.ini ]
then
bunzip2 setup.bz2
mv setup setup.ini
echo Updated setup.ini
else
wget -N $mirror/$arch/setup.ini
if test -e setup.ini && test $? -eq 0
then
echo Updated setup.ini
else
mv setup.ini-save setup.ini
echo Error updating setup.ini, reverting
fi
mv setup.ini-save setup.ini
echo Error updating setup.ini, reverting
fi
fi
}
function checkpackages()
{
if test "-$packages-" = "--"
then
echo Nothing to do, exiting
exit 0
fi
(( ${#packages} )) && return
echo Nothing to do, exiting
exit
}
# process options
@ -131,7 +129,7 @@ command=""
filepackages=""
packages=""
while test $# -gt 0
while (( $# ))
do
case "$1" in
@ -166,7 +164,7 @@ do
;;
--file | -f)
if ! test "-$2-" = "--"
if (( ${#2} ))
then
file="$2"
dofile=1
@ -178,26 +176,26 @@ do
;;
update | show | find | describe | packageof | install | remove)
if test "-$command-" = "--"
if (( ${#command} ))
then
command=$1
packages+=" $1"
else
packages="$packages $1"
command=$1
fi
shift
;;
*)
packages="$packages $1"
packages+=" $1"
shift
;;
esac
done
if test $dofile = 1
if (( dofile ))
then
if test -f "$file"
if [ -f "$file" ]
then
filepackages+=$(awk '{printf " %s", $0}' "$file")
else
@ -248,17 +246,20 @@ case "$command" in
checkpackages
for pkg in $packages
do
key=`which "$pkg" 2>/dev/null | sed "s:^/::"`
if test "-$key-" = "--"
key=$(type -P "$pkg" | sed s./..)
if (( ! ${#key} ))
then
key="$pkg"
key=$pkg
fi
for manifest in /etc/setup/*.lst.gz
do
found=$(gzip -cd $manifest | grep -c "$key")
if test $found -gt 0
if (( found ))
then
package=`echo $manifest | sed -e "s:/etc/setup/::" -e "s/.lst.gz//"`
package=$(sed '
s,/etc/setup/,,
s,.lst.gz,,
' <<< $manifest)
echo Found $key in the package $package
fi
done
@ -273,7 +274,7 @@ case "$command" in
do
already=`grep -c "^$pkg " /etc/setup/installed.db`
if test $already -ge 1
if (( already ))
then
echo Package $pkg is already installed, skipping
continue
@ -295,7 +296,7 @@ case "$command" in
}
' RS='\n\n@ ' FS='\n' package="$pkg" setup.ini > "release/$pkg/desc"
desc=$(<"release/$pkg/desc")
if test "-$desc-" = "-Package not found-"
if [[ $desc = 'Package not found' ]]
then
echo Package $pkg not found or ambiguous name, exiting
rm -r "release/$pkg"
@ -308,7 +309,7 @@ case "$command" in
# pick the latest version, which comes first
install=$(awk '/^install: / {print $2; exit}' "release/$pkg/desc")
if test "-$install-" = "--"
if (( ! ${#install} ))
then
echo 'Could not find "install" in package description: obsolete package?'
exit 1
@ -321,7 +322,7 @@ case "$command" in
# check the md5
digest=$(awk '/^install: / {print $4; exit}' desc)
digactual=$(md5sum $file | awk NF=1)
if ! test $digest = $digactual
if [ $digest != $digactual ]
then
echo MD5 sum did not match, exiting
exit 1
@ -356,31 +357,31 @@ case "$command" in
}
' rq='^requires: ' "release/$pkg/desc")
warn=0
if ! test "-$requires-" = "--"
if (( ${#requires} ))
then
echo Package $pkg requires the following packages, installing:
echo $requires
for package in $requires
do
already=`grep -c "^$package " /etc/setup/installed.db`
if test $already -ge 1
if (( already ))
then
echo Package $package is already installed, skipping
continue
fi
apt-cyg --noscripts install $package
(( $? )) && warn=1
(( $? && warn++ ))
done
fi
if ! test $warn = 0
if (( warn ))
then
echo "Warning: some required packages did not install, continuing"
echo 'Warning: some required packages did not install, continuing'
fi
# run all postinstall scripts
pis=`ls /etc/postinstall/*.sh 2>/dev/null | wc -l`
if test $pis -gt 0 && ! test $noscripts -eq 1
if (( pis && ! noscripts ))
then
echo Running postinstall scripts
for script in /etc/postinstall/*.sh
@ -400,7 +401,7 @@ case "$command" in
do
already=`grep -c "^$pkg " /etc/setup/installed.db`
if test $already = 0
if (( ! already ))
then
echo Package $pkg is not installed, skipping
continue
@ -408,13 +409,13 @@ case "$command" in
dontremove="cygwin coreutils gawk bzip2 tar wget bash"
for req in $dontremove
do
if test "-$pkg-" = "-$req-"
if [[ $pkg = $req ]]
then
echo apt-cyg cannot remove package $pkg, exiting
exit 1
fi
done
if ! test -e "/etc/setup/$pkg.lst.gz"
if [ ! -e "/etc/setup/$pkg.lst.gz" ]
then
echo Package manifest missing, cannot remove $pkg. Exiting
exit 1
@ -423,7 +424,7 @@ case "$command" in
# run preremove scripts
if test -e "/etc/preremove/$pkg.sh"
if [ -e "/etc/preremove/$pkg.sh" ]
then
"/etc/preremove/$pkg.sh"
rm "/etc/preremove/$pkg.sh"