下面是我shell脚本里面的两个函数,第二个函数会调用第一个函数,但是在第一个函数判断退出的时候,你只能退出你所在的那个函数,而不是退出整个shell,请问有什么办法可以让我在第一个函数判断退出的时候退出整个shell脚本吗?
check_return(){
if [ $? -ne 0 ]
then
echo "$1"
exit 1
fi
}
nodes_status() {
CURRENT_STAT=$(cat $serverstatus|grep \"myState\" |awk '{print $3}'|awk -F ',' '{print $1}'|sed 's/ *$//g'|sed 's/^ *//g')
check_return "-0.9906"
...
}
nodes_status
check_return(){
if [ $? -ne 0 ]
then
echo "$1"
exit 1
fi
}
nodes_status() {
CURRENT_STAT=$(cat $serverstatus|grep \"myState\" |awk '{print $3}'|awk -F ',' '{print $1}'|sed 's/ *$//g'|sed 's/^ *//g')
check_return "-0.9906"
...
}
nodes_status
0
虽然不知道你为什么要这么退出,我写这个不知道对你有没有用
[root@bogon zhangsan]# cat testkillself
#!/bin/sh
#test kill self
#written by zhangchaoyue
#2015/20/21
echo hello
killself(){
echo "this script PID is $$"
read wait
[ $1 -eq 1 ] && kill -9 $$ &>/dev/null || exit 1
}
main(){
killself 1
# echo $?
}
main
[root@bogon zhangsan]# ./testkillself
hello
this script PID is 4199
Killed
[root@bogon zhangsan]#
[root@bogon zhangsan]# cat testkillself
#!/bin/sh
#test kill self
#written by zhangchaoyue
#2015/20/21
echo hello
killself(){
echo "this script PID is $$"
read wait
[ $1 -eq 1 ] && kill -9 $$ &>/dev/null || exit 1
}
main(){
killself 1
# echo $?
}
main
[root@bogon zhangsan]# ./testkillself
hello
this script PID is 4199
Killed
[root@bogon zhangsan]#
编辑回复