From 6b90bb84fb0934e8c3aacffee2b843afc7917d3e Mon Sep 17 00:00:00 2001 From: Jeffrey S. Haemer Date: Jul 04 2016 20:16:04 +0000 Subject: Untested zsh -> bash port. There aren't tests, and I haven't had any experience testing this. Besides. I made the changes on a Mac, with homebrew as my package manager, so my bash 4 is bash 4.3, in /usr/local/bin. --- diff --git a/quick-fedora-mirror b/quick-fedora-mirror index 69d6280..7582e80 100755 --- a/quick-fedora-mirror +++ b/quick-fedora-mirror @@ -1,4 +1,4 @@ -#!/bin/zsh +#!/bin/bash export LANG=C # Simple script to grab the file list from Fedora and rsync everything that's # changed since the last time we pulled. @@ -9,6 +9,12 @@ export LANG=C # Variables in upper case are user configurables. +# Lines marked BASHISM are bash-only +# Lines marked AASHISM are for BASH or ZSH (AA == "Associative Array") +# Lines marked ZSHISM are zsh-only + +# It's safe to assume that Fedora systems will have bash installed by default. +# They may, however, have to be upgraded to bash >= 4.2 # Do this absolutely first starttime=$(date +%s) # Paranoia @@ -16,12 +22,21 @@ starttime=$(($starttime-5)) # Utility functions -db1 () { (( VERBOSE >= 1 )) && echo $* } -db2 () { (( VERBOSE >= 2 )) && echo $* } -db3 () { (( VERBOSE >= 3 )) && echo '>>' $* } -db4 () { (( VERBOSE >= 4 )) && echo '>>>>' $* } -sep () { (( VERBOSE >= 2 )) && echo '============================================================' } -db2f () { (( VERBOSE >= 2 )) && printf $* } +die() { echo "$@" >&2; exit 1; } + +check-shell() { + (( BASH_VERSINFO[0] >= 4 )) && return + die "Bash version '$BASH_VERSION' must be >= 4 to use bash associative arrays." +} + +check-shell # might as well find out right away + +db1 () { (( VERBOSE >= 1 )) && echo $*; } +db2 () { (( VERBOSE >= 2 )) && echo $* ;} +db3 () { (( VERBOSE >= 3 )) && echo '>>' $*; } +db4 () { (( VERBOSE >= 4 )) && echo '>>>>' $*; } +sep () { (( VERBOSE >= 2 )) && echo '============================================================'; } +db2f () { (( VERBOSE >= 2 )) && printf $*; } finish () { if (( $VERBOSE >= 1 )); then @@ -40,7 +55,7 @@ do_rsync () { local src=$1 local dest=$2 local files=$3 - local opts=$4 + local opts="$4" local sleep=1 local log=rsync.log local errlog=rsync-error.log @@ -58,30 +73,24 @@ do_rsync () { flopts=(--files-from=$files) while true; do - # ZSHISM: (P) flag to act on a variable by name. Sadly, bash has - # broken array handling. bash 4.3 has local -n for this. Older bash - # needs hacks, or eval. More info: - # https://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash - # Or just use a freaking global. - - # We have to do this separately because you can't redirect to /dev/stderr when running under sudo. + # We have to do this separately because you cannot redirect to /dev/stderr when running under sudo. if (( VERBOSE >= 5 )); then - db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest - $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest + db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest + $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest rsyncreturn=$? elif (( VERBOSE >= 2 )); then - db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest \>\> $log - $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest >> $log + db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest \>\> $log + $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest >> $log rsyncreturn=$? else - db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest \>\> $log 2\>\> $errlog - $RSYNC $RSYNCOPTS $verboseopts $flopts ${(P)opts} $src $dest >> $log 2>> $errlog + db3 Calling $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest \>\> $log 2\>\> $errlog + $RSYNC $RSYNCOPTS $verboseopts $flopts $opts $src $dest >> $log 2>> $errlog rsyncreturn=$? fi (( rsyncreturn != 5 && rsyncreturn != 10 )) && break - db2 rsync failed: sleeping for $sleep + db2 rsync failed: sleeping $sleep seconds sleep $sleep sleep=$(( sleep*2 )) if (( sleep > 1000 )); then @@ -146,14 +155,14 @@ while [[ $# > 0 ]]; do done # Mapping from module names to directories under fedora-buffet -# ZSHISM (initialize associative array) +# BASHISM (initialize associative array) typeset -A MODULEMAPPING MODULEMAPPING=( - fedora-alt alt - fedora-archive archive - fedora-enchilada fedora - fedora-epel epel - fedora-secondary fedora-secondary + [fedora-alt]=alt + [fedora-archive]=archive + [fedora-enchilada]=fedora + [fedora-epel]=epel + [fedora-secondary]=fedora-secondary ) # Default arguments; override in quick-fedora-mirror.conf @@ -252,8 +261,8 @@ fi if [[ -n $MIRRORBUFFET ]]; then # We want to mirror everything, so save the admin from listing the # individual modules. - # ZSHISM (get keys from an associative array with (k)) - MODULES=(${(k)MODULEMAPPING}) + # BASHISM (get keys from an associative array) + MODULES=( ${!MODULEMAPPING[@]} ) fi @@ -261,29 +270,29 @@ fi # ========================= sep db2 Downloading file lists -# ZSHISM (declare associative array) +# AASHISM (declare associative array) typeset -A fltimes for module in $MODULES; do - # ZSHISM? (associative array indexing) - moduledir=$MODULEMAPPING[$module] + # BASHISM? (associative array indexing) + moduledir=${MODULEMAPPING[$module]} mkdir $moduledir flname=${FILELIST/'$mdir'/$moduledir} - # ZSHISM (assign assoc. array value) + # AASHISM (assign assoc. array value) fltimes[$module]=0 if [[ -f $DESTD/$moduledir/$flname ]]; then cp -p $DESTD/$moduledir/$flname $moduledir # So that we can diff it later cp -p $moduledir/$flname $flname.old - # ZSHISM (assign assoc. array value) + # AASHISM (assign assoc. array value) fltimes[$module]=$(stat --format=%Y $moduledir/$flname) fi echo $moduledir/$flname >> filelist-transferlist done -extra=(--no-dirs --relative) -do_rsync $REMOTE/$MASTERMODULE/ . filelist-transferlist extra +extra="--no-dirs --relative" +do_rsync $REMOTE/$MASTERMODULE/ . filelist-transferlist "$extra" # rsync won't transfer those files to the current directory, so move them and # clean up. @@ -293,8 +302,8 @@ rmdir * 2> /dev/null # File list generation # ==================== for module in $MODULES; do - # ZSHISM? (associative array indexing) - moduledir=$MODULEMAPPING[$module] + # BASHISM? (associative array indexing) + moduledir=${MODULEMAPPING[$module]} fl=${FILELIST/'$mdir'/$moduledir} totallines=0 @@ -303,7 +312,7 @@ for module in $MODULES; do # all of them. Copy in the old file from our repo to speed up rsync in case # most of it didn't change if [[ -z $alwayscheck && -f $fl.old && $(stat --format=%Y $fl.old) == $(stat --format=%Y $fl) ]]; then - db2 No change in file list mtime. Skipping $module. + db2 No change to file list mtime. Skipping $module. continue fi