【发布时间】:2011-08-30 09:30:03
【问题描述】:
我编写了一个小型库函数来帮助我在脚本所有者不是 root 时退出:
#!/bin/bash
# Exit for non-root user
exit_if_not_root() {
if [ "$(id -u)" == "0" ]; then return; fi
if [ -n "$1" ];
then
printf "%s" "$1"
else
printf "Only root should execute This script. Exiting now.\n"
fi
exit 1
}
这里我从另一个文件中调用它:
#!/bin/bash
source ../bashgimp.sh
exit_if_not_root "I strike quickly, being moved. But thou art not quickly moved to strike.\n You're not root, exiting with custom message."
输出是:
I strike quickly, being moved. But thou art not quickly moved to strike.\n You're not root, exiting with custom message.
如何让换行符正确显示?
【问题讨论】: