在Linux上编写和测试的以下bash脚本在调用时甚至不会在OS X上启动。
#!/bin/bash
#
# Some comments
#
#
function usage {
echo ""
echo "Usage: thisscript <SOURCE_DIRECTORY> <TARGET_DIRECTORY>"
echo ""
echo "<SOURCE_DIRECTORY> the directory where the this "
echo " directory resides (default is /usr/dis)"
echo ""
echo "<TARGET_DIRECTORY> the destination directory"
echo ""
}
function notDarwin {
mv -f $CUR_DIR/* $NEW_DIR/
ln -sf "$NEW_DIR/ee/sc/scrp" "/usr/bin/scrp"
ln -sf "$NEW_DIR/ee/etc/conffile.conf" "/etc/conffile.conf"
exit 0
}
function isDarwin {
mv -f $CUR_DIR/* $NEW_DIR/
ln -sf "$NEW_DIR/ee/sc/scrp" "/usr/local/bin/scrp"
cp "$NEW_DIR/ee/etc/conffile.conf" "/etc/conffile.conf"
exit 0
}
#
# =============================================
# ================== MAIN =====================
# =============================================
#
CUR_DIR=${1%/}
NEW_DIR=${2%/}
if [ ! -d "$CUR_DIR" ]; then
echo ""
echo "blah blah"
usage
exit 1
fi
if [ ! -d "$NEW_DIR" ]; then
echo ""
echo "The target directory supplied does not exist. Creating target directory $NEW_DIR"
mkdir "$NEW_DIR"
if [ $? -ne 0 ]; then
echo "Could not create target directory. Exiting..."
exit 1
else
echo "Directory $NEW_DIR created"
fi
echo ""
fi
UNAME=$(uname)
if [ $UNAME == "Darwin" ]; then
isDarwin
else
notDarwin
fi
使用bash 3.2
在macOS上以sudo bash script.sh "arg1" "arg2"
运行时会抛出以下语法错误
'script.sh: line 7: syntax error near unexpected token `{
'script.sh: line 7: `function usage {
我对OS X很新,也许有一个我想念的问题。该脚本在Linux上运行良好......
由于
答案 0 :(得分:1)
Linux和现代OS X期望行以LF(换行)字符结束。如果你的行以CR + LF结尾,那么你会遇到问题。
其他一些通用指针:
function
语法是非标准的。您应该使用所有符合POSIX标准的shell支持的标准语法:
变化:
function usage {
为:
usage() {
我怀疑一切都会好起来。
顺便说一下,你应该引用所有的参数扩展(你错过了一对)。使用小写变量名称也被认为是一种好习惯,因为shell使用大写变量名称,因此存在与它们发生冲突的风险。