Scripting

帶有“set -e”退出的 Slackbuild 腳本..沒有錯誤!

  • July 22, 2019

有了這個 slackbuild:

#!/bin/sh

PRGNAM=libnfsidmap
VERSION=${VERSION:-0.25}
BUILD=${BUILD:-3}
TAG=${TAG:-_mg}

if [ -z "$ARCH" ]; then
 case "$( uname -m )" in
       i?86) ARCH=i486 ;;
       arm*) ARCH=arm ;;
          *) ARCH=$( uname -m ) ;;
 esac
fi


CWD=$(pwd)
TMP=${TMP:-/tmp/txz}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
 SLKCFLAGS="-O2 -march=i486 -mtune=i686"
 LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
 SLKCFLAGS="-O2 -march=i686 -mtune=i686"
 LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
 SLKCFLAGS="-O2 -fPIC"
 LIBDIRSUFFIX="64"
fi

set -e

SOURCE="http://www.citi.umich.edu/projects/nfsv4/linux/libnfsidmap/libnfsidmap-$VERSION.tar.gz"

if [ ! -e $PRGNAM-$VERSION.tar.gz ];
then
   wget -c $SOURCE
else
   echo "already download"
fi


rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xzvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
 --prefix=/usr \
 --libdir=/usr/lib${LIBDIRSUFFIX} \
 --sysconfdir=/etc \
 --localstatedir=/var \
 --mandir=/usr/man \
 --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG
mkdir $PKG/etc
install -m 644 $CWD/idmapd.conf $PKG/etc

( cd $PKG
 find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null || true
 find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null
)

( cd $PKG/usr/man
 find . -type f -exec gzip -9 {} \;
 for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
 $TMP/$PRGNAM-$VERSION/README $TMP/$PRGNAM-$VERSION/AUTHORS $TMP/$PRGNAM-$VERSION/COPYING \
   $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc
find $PKG/usr/doc -type f -exec chmod 0644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $CWD/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.txz

我編譯

sudo sh *ld 

並且…它在 make 之後退出,即使行後沒有錯誤(!)

make install DESTDIR=$PKG

如果我刪除 set -e,請繼續並正確創建我的包。為什麼用 set -e 退出?據我所知, set -e 的意思是“如果發生錯誤,則退出腳本”。發行版是 slackware 14.2,bash 是 4.3.48,我也嘗試 bash script.sh.. 但還是一樣。

找到的解決方案 有罪的線是..

( cd $PKG
 find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null || true
 find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null
)

即使沒有發生錯誤(在這種情況下被 2> 隱藏),如果不需要剝離或文件失去,腳本返回非 0 狀態並退出(設置 -e)。有了這個修正

( cd $PKG
 find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null || true
 find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
   xargs strip --strip-unneeded 2> /dev/null || true
)

一切正常。

所以我錯過了|| true最新的帶狀線。

引用自:https://unix.stackexchange.com/questions/473832