shell 时用$?查看是否执行成功,成功是0,而在函数里是只要有没有成功,那么整个函数的$?就会是1,于是用它和||结合起来可以实现Shell函数是否成功执行的一个判断并提示。
cat fun.sh
sh fun.sh
hello world...\n
ls: cannot access /tmp2: No such file or directory
ls /tmp2 failed
今天重新再翻看了一下shell程序相关的资料。准备重新将shell再提高一下。
将http://seo-dic.com.cn/archives/2496 提到的《Advanced Bash-Scripting Guide》《高级bash脚本编程指南》上附带的源码重新再看一次。
为了方便查看。我将所有的源代码全部导到一个文件中,再一一查看。省去了每次只打开一个脚本的麻烦。同时,将几个经常用的shell脚本函数稍做了下列举。希望后面可以用得到。
find -type f -name "*.sh" -exec cat {} >>sh.all \;
检查目录是否存在,若不存在,则创建相应目录。
# Check for and create datadir if necessary:
if test ! -d $datadir
then
mkdir $datadir
fi
shell参数判断,若不符合则显示使用方式及退出。
E_BADARGS=65
case $# in
0|1) # The vertical bar means "or" in this context.
echo "Usage: `basename $0` old_file_suffix new_file_suffix"
exit $E_BADARGS # If 0 or 1 arg, then bail out.
;;
esac
另例:
E_OPTERR=65
if [ "$#" -eq 0 ]
then # Script needs at least one command-line argument.
echo "Usage $0 -[options a,b,c]"
exit $E_OPTERR
fi
检查是用户是否是root权限的方式:
ROOT_UID=0 # Root has $UID 0.
E_WRONG_USER=65 # Not root?
E_NOSUCHUSER=70
SUCCESS=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo; echo "Only root can run this script."; echo
exit $E_WRONG_USER
else
echo
echo "You should know better than to run this script, root."
echo "Even root users get the blues... "
echo
fi
进行用户目录判断:
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non-root exit error.
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory, before messing with log file.
cat fun.sh
sh fun.sh
hello world...\n
ls: cannot access /tmp2: No such file or directory
ls /tmp2 failed
今天重新再翻看了一下shell程序相关的资料。准备重新将shell再提高一下。
将http://seo-dic.com.cn/archives/2496 提到的《Advanced Bash-Scripting Guide》《高级bash脚本编程指南》上附带的源码重新再看一次。
为了方便查看。我将所有的源代码全部导到一个文件中,再一一查看。省去了每次只打开一个脚本的麻烦。同时,将几个经常用的shell脚本函数稍做了下列举。希望后面可以用得到。
find -type f -name "*.sh" -exec cat {} >>sh.all \;
检查目录是否存在,若不存在,则创建相应目录。
# Check for and create datadir if necessary:
if test ! -d $datadir
then
mkdir $datadir
fi
shell参数判断,若不符合则显示使用方式及退出。
E_BADARGS=65
case $# in
0|1) # The vertical bar means "or" in this context.
echo "Usage: `basename $0` old_file_suffix new_file_suffix"
exit $E_BADARGS # If 0 or 1 arg, then bail out.
;;
esac
另例:
E_OPTERR=65
if [ "$#" -eq 0 ]
then # Script needs at least one command-line argument.
echo "Usage $0 -[options a,b,c]"
exit $E_OPTERR
fi
检查是用户是否是root权限的方式:
ROOT_UID=0 # Root has $UID 0.
E_WRONG_USER=65 # Not root?
E_NOSUCHUSER=70
SUCCESS=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo; echo "Only root can run this script."; echo
exit $E_WRONG_USER
else
echo
echo "You should know better than to run this script, root."
echo "Even root users get the blues... "
echo
fi
进行用户目录判断:
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non-root exit error.
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory, before messing with log file.
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2560/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2018-6-11 16:49
评论列表