Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Apple bash to zsh script problems with error control and bash emulation



Hi all,

I’ve recently moved to zsh as Apple is pressuring us :)

My problem is that I have built a little bash script thing to work as a buildbot for some projects I’m building snapshots for.

My two problems are that my error control is no longer working in a way that it stops (exit 1) after encountering an error when in bash emulation mode, and that if I deactivate the bash emulation stuff doesn’t work.

My scripts can be found at https://github.com/DominusExult/buildbot, as an example I’ve shortened in this email my exult snapshot.sh and my functions script functions.sh.
Something goes wrong in the error(), pipestatus() and teelog() functions, I guess. But I can’t quite tell and I’ve built that script 5 years ago and am a bit rusty to what each of those function do exactly.

What I expect is that when the script runs into an error it stops, even if the error happens in one of the functions but currently the script goes on and on. This used to work before in bash.
If I get rid of the —emulate sh, my variables don’t get passed on correctly (the error I get in the shorted version of my script is "configure: error: unrecognized option: `-q --disable-data --with-macosx-static-lib-path=/opt/i386/lib’”

I hope you can help me figure this out. I’ve tried researching this on my own but the biggest hurdle was that search results pointed me at bash stuff and not zsh.

Thank you,
Dom

The scripts:

exultsnasphot.sh:
#!/bin/zsh --emulate sh
#functions
. ./functions.sh

headermain EXULT

cd ~/code/snapshots/exult
/usr/bin/git pull --rebase=true 2> >(teelog >&2) || error Git pull

#i386
ARCH=i386
SDK=10.11
DEPLOYMENT=10.7
flags
gcc
CONF_ARGS="-q --disable-data --with-macosx-static-lib-path=/opt/$ARCH/lib"
autogen
build 2>&1 | teelog -a ; pipestatus || return

deploy
{
#make fat exult binary
lipo -create -arch arm64 exult_arm64 -arch x86_64 exult_x86_64 -arch i386 exult_i386 -output exult || error lipo
} 2>&1 | teelog -a ; pipestatus || return


functions.sh:
#-------------headers-------------
headermain() {
if [ "$1" != "" ]; then
TARGET=$1
#lowercase of $TARGET
target="$(echo $TARGET | tr '[A-Z]' '[a-z]')"
#logfile
LOGFILE=~/.local/logs/${target}built.txt
else
error headermain function
fi
}

alias deploy='echo -e "$(tput setab 4)$(tput bold)$(tput setaf 3)\tdeployment\t$(tput sgr 0)"'

#-------------Error handling-------------
alias lockfile='rm -f ~/.local/"$TARGET"build1.lockfile'

error () {
if [ "$?" != "0" ]; then
if [ "$2" != "" ]; then
local i2=" $2"
fi
if [ "$3" != "" ]; then
local i3=" $3"
fi
echo -e "$(tput setab 1)$(tput bold)$(tput setaf 7)\t${1:-"Unknown Error"}${i2}${i3} failed!\t\t$(tput sgr 0)" 1>&2
lockfile
exit 1
fi
}

pipestatus() {
local S=("${PIPESTATUS[@]}")
if test -n "$*"
then test "$*" = "${S[*]}"
else ! [[ "${S[@]}" =~ [^0\ ] ]]
fi
}

teelog() {
tee $1 $LOGFILE
}

#-------------compiler & flags-------------
flags() {
SYSARCH=$(uname -m)
if  [ "$ARCH" = "" ] && [ "$SYSARCH" = "arm64" ]; then
ARCH=arm64
SDK=11.0
DEPLOYMENT=11.0
elif [ "$ARCH" = "" ] && [ "$SYSARCH" = "x86_64" ]; then
ARCH=x86_64
SDK=10.14
DEPLOYMENT=10.11
fi

export PKG_CONFIG_PATH=/opt/$ARCH/lib/pkgconfig
export PKG_CONFIG=/opt/$SYSARCH/bin/pkg-config

if [ "$ARCH" = "i386" ]; then
OPTARCH='-arch i386 -m32 -msse -msse2 -O2 '
elif [ "$ARCH" = "ppc" ]; then
OPTARCH='-arch ppc -m32 -O2 '
export PKG_CONFIG=/opt/x86_64/bin/pkg-config
elif  [ "$ARCH" = "x86_64" ]; then
OPTARCH='-m64 -msse -msse2 -O2 '
elif  [ "$ARCH" = "arm64" ]; then
OPTARCH='-O2 '
fi
OPT=' -w -force_cpusubtype_ALL '$OPTARCH
SDK=' -isysroot /opt/SDKs/MacOSX'$SDK'.sdk -mmacosx-version-min='$DEPLOYMENT' '
export MACOSX_DEPLOYMENT_TARGET=$DEPLOYMENT
export CPPFLAGS='-I/opt/'$ARCH'/include'$SDK
export CFLAGS='-I/opt/'$ARCH'/include'$SDK' '$OPT
export CXXFLAGS='-I/opt/'$ARCH'/include '$SDK' '$OPT
export LDFLAGS='-L/opt/'$ARCH'/lib'$SDK' '$OPT
export LIBTOOLFLAGS=--silent
}

gcc() {
if [ "$ARCH" != "" ]; then
export PATH=/opt/$ARCH/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export LD="/usr/bin/ld"
export RANLIB="~/code/sh/tools/ranlib"
if [ "$1" = "legacy" ]; then
export PATH=/opt/$ARCH/bin/:/opt/xcode3/usr/bin:/opt/xcode3/usr/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export CC='/opt/xcode3/usr/bin/llvm-gcc-4.2 -arch '$ARCH
export CXX='/opt/xcode3/usr/bin/llvm-g++-4.2 -arch '$ARCH
export LD="/opt/xcode3/usr/bin/ld"
export RANLIB="/opt/xcode3/usr/bin/ranlib.old"
elif [ "$1" = "oldgcc" ]; then
export PATH=/opt/$ARCH/bin/:/opt/xcode3/usr/bin:/opt/xcode3/usr/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export CC='/opt/xcode3/usr/bin/gcc-4.2 -arch '$ARCH
export CXX='/opt/xcode3/usr/bin/g++-4.2 -arch '$ARCH
export LD="/opt/xcode3/usr/bin/ld"
export RANLIB="/opt/xcode3/usr/bin/ranlib.old"
else
export CC='/usr/bin/clang -arch '$ARCH
export CXX='/usr/bin/clang++ -arch '$ARCH
fi
else
error gcc function
fi
}

#-------------command shortcuts-------------
alias autogen='./autogen.sh > /dev/null 2>&1'

alias makes='make clean  > /dev/null ; make -j9 -s AR="~/code/sh/tools/ar" > /dev/null || error $HEADER make'


config() {
if [ "$CONF_OPT" != "" ]; then
c1=$CONF_OPT
fi
if [ "$CONF_ARGS" != "" ]; then
c2=$CONF_ARGS
fi
if [ "$ARCH" = "ppc" ]; then
./configure --host=powerpc-apple-darwin $CONF_OPT $CONF_ARGS || error $ARCH configure

elif [ "$ARCH" = "i386" ]; then
./configure --host=i386-apple-darwin $CONF_OPT $CONF_ARGS || error $ARCH configure

elif [[ "$ARCH" = "arm64" ]] && [[ "$SYSARCH" != "arm64" ]]; then
./configure --host=arm-apple-darwin $CONF_OPT $CONF_ARGS || error $ARCH configure

elif [[ "$ARCH" = "x86_64" ]] && [[ "$SYSARCH" != "x86_64" ]]; then
./configure --host=x86_64-apple-darwin $CONF_OPT $CONF_ARGS || error $ARCH configure

else [ "$?" != "0" ]
./configure $CONF_OPT $CONF_ARGS || error $ARCH configure
fi
}

stripp() {
if [ "$ARCH" != "" ]; then
strip $1 -o $1_$ARCH || error $HEADER strip
else
strip $1 -o $1 || error $1 strip
fi
}

build() {
config
makes
stripp $target
}





Messages sorted by: Reverse Date, Date, Thread, Author