shell 函数
最后更新于
最后更新于
print () {
echo "welcome to ayitula"function hello {
echo "hello world"
}
hello就是函数名#!/bin/bash
N1 () {
echo "`date +%F`"
}
N2 () {
echo -e "\t\t\t\twelcome to ayitula"
echo -e "\n"
}
N3 () {
echo "1) 剪子"
echo "2) 石头"
echo "3) 布"
}
N4 () {
echo -e "\n\n\n"
read -p "请输入代码: " DL
}
#方便调整代码执行顺序
N2
#代码重复调用
N1
N1
N3
N4
输出
[root@www ~]# sh x2
welcome to ayitula
2019-02-19
2019-02-19
1) 剪子
2) 石头
3) 布
请输入代码: 1 #!/bin/bash
#ngingx service manager scripts
#=====================variables
nginxdoc="/usr/local/nginx"
nginxd="$nginxdoc/sbin/nginx"
pid="$nginxdoc/logs/nginx.pid"
conf="$nginxdoc/conf/nginx.conf"
#====================function
mystart () {
if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
echo "nginx already run..."
exit 0
else
if $nginxd ;then
echo -e "nginx start\t\t\t\t[\033[32m OK \033[0m]"
else
echo -e "nginx start\t\t\t\t[\033[31m FAIL \033[0m]"
fi
fi
}
mystop () {
if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
if killall -s QUIT $nginxd;then
echo -e "nginx stop\t\t\t\t[\033[32m OK \033[0m]"
else
echo -e "nginx stop\t\t\t\t[\033[31m FAIL \033[0m]"
fi
else
echo "nginx already stop...."
fi
}
myrestart () {
mystop;sleep 2 ;mystart
}
myreload () {
if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
if killall -s HUP $nginxd;then
echo -e "nginx reload\t\t\t\t[\033[32m OK \033[0m]"
else
echo -e "nginx reload\t\t\t\t[\033[31m FAIL \033[0m]"
fi
else
echo "nginx is stop...."
fi
}
mystatus () {
if [ -f $pid ] && pstree -p |grep `cat $pid` &>/dev/null;then
echo "nginx is open"
else
echo "nginx is stop"
fi
}
#===================main
#calld function
case $1 in
start|START)
mystart
;;
stop) mystop ;;
restart) myrestart ;;
reload) myreload ;;
status) mystatus ;;
esac