1.一个exec自身的脚本
#! /bin/sh
echo
echo "This line appears ONCE in the script,yet it keeps echoing."
echo "The PID of this instance of the script is still $$."
echo "====================Hit Contrl + C to exit ================="
sleep 2
exec $0
echo "This line will never echo!"
exit 0
2.shopt允许shell在空闲时修改shell选项,经常出现在启动文件中。
shop -s cdspell ---------使用cd命令时,允许少量的错误
cd /hpme ---- 应该是/home
pwd ----------/home
3.caller命令放入函数中,将显示调用该函数的调用者信息
4.type [cmd]
将给出命令的完整路径
5.作业控制命令
#! /bin/sh
ROOT_UID=0
E_NOTROOT=65
E_NOPARAMS=66
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
if [ -z "$1" ]
then
echo "Usage: `basename $0` find-string"
exit $E_NOPARAMS
fi
echo "Updating 'locate' database..."
echo "This may make a while ."
updatedb /usr &
wait
locate $1
exit 0
6.结束自身脚本程序
#! /bin/sh
kill && ----------------&&相当于PID
echo "This line will not echo"
exit 0
本文介绍了Shell脚本中的一些高级技巧,包括如何让脚本循环执行直至被手动终止、使用shopt来提升用户体验、通过caller命令获取函数调用堆栈信息、使用type命令查找命令的完整路径以及如何实现作业控制和自我终止。
1719

被折叠的 条评论
为什么被折叠?



