总结
方法
01、case条件语句:
case "变量" in 值1) 指令1... ;; 值2) 指令2... ;; *) 指令3... esac
02、给字体加颜色:
echo -e "\E[1;31m红色字oldboy\E[0m" echo -e "\033[31m红色字oldboy \033[0m" echo -e可以识别转义字符, \E可以使用\033替代。 “[1”数字1表示加粗显示(可以加不同的数字,以代表不同的意思,详细信息可用man console_codes获得)。 31m表示为红色字体,可以换成不同的数字,以代表不同的意思。 “红色字oldboy”表示待设置的内容。 “[0m”表示关闭所有属性,可以换成不同的数字,以代表不同的意思。有关ANSI控制码的说明如下。 \33[0m表示关闭所有属性。 \33[1m表示设置高亮度。 \33[4m表示下划线。 \33[5m表示闪烁。 \33[7m表示反显。 \33[8m表示消隐。 \33[30m--\33[37m表示设置前景色。 \33[40m--\33[47m表示设置背景色。 参考man console_codes
03、带颜色的选择菜单:cat 9_2_3.sh
#!/bin/sh
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
function usage(){
echo "USAGE: $0 {1|2|3|4}"
exit 1
}
function menu(){
cat <<END
1.apple
2.pear
3.banana
END
}
function chose(){
read -p "pls input your choice:" fruit
case "$fruit" in
1)
echo -e "${RED_COLOR}apple${RES}"
;;
2)
echo -e "${GREEN_COLOR}pear${RES}"
;;
3)
echo -e "${YELLOW_COLOR}banana${RES}"
;;
*)
usage
esac
}
function main(){
menu
chose
}
main
04、给内容加上不同的颜色:cat 9_5_3.sh
#!/bin/bash
function AddColor(){
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'
if [ $# -ne 2 ];then
echo "Usage $0 content {red|yellow|blue|green}"
exit
fi
case "$2" in
red|RED)
echo -e "${RED_COLOR}$1${RES}"
;;
yellow|YELLOW)
echo -e "${YELLOW_COLOR}$1${RES}"
;;
green|GREEN)
echo -e "${GREEN_COLOR}$1${RES}"
;;
blue|BLUE)
echo -e "${BLUE_COLOR}$1${RES}"
;;
pink|PINK)
echo -e "${PINK_COLOR}$1${RES}"
;;
*)
echo "Usage $0 content {red|yellow|blue|green}"
exit
esac
}
function main(){
AddColor $1 $2
}
main $*
05、背景颜色:
echo -e "\033[40;37m 黑底白字oldboy\033[0m" echo -e "\033[41;37m 红底白字oldboy\033[0m" echo -e "\033[42;37m 绿底白字oldboy\033[0m" echo -e "\033[43;37m 棕底白字oldboy\033[0m" echo -e "\033[44;37m 蓝底白字oldboy\033[0m" echo -e "\033[45;37m 洋红底白字oldboy\033[0m" echo -e "\033[46;37m蓝绿底白字oldboy\033[0m" echo -e "\033[47;30m 白底黑字oldboy\033[0m"
06、etc/openvpn_authfile.conf以传参的方式添加用户:cat add-openvpn-user.sh
#!/bin/sh
#create by oldboy
#time :19:14 2012-3-21
#Source function library.
. /etc/init.d/functions
#config file path
FILE_PATH=/etc/openvpn_authfile.conf
[ ! -f $FILE_PATH ] && touch $FILE_PATH
usage(){
cat <<EOF
USAGE: `basename $0` {-add|-del|-search} username
EOF
}
#judge run user
if [ $UID -ne 0 ] ;then
echo "Youare not supper user,please call root!"
exit 1;
fi
#judge arg numbers.
if [ $# -ne 2 ] ;then
usage
exit 2
fi
case "$1" in
-a|-add)
shift
if grep "^$1$" ${FILE_PATH} >/dev/null 2>&1;then
action $"vpnuser,$1 is exist" /bin/false
exit
else
chattr -i ${FILE_PATH}
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
echo "$1" >> ${FILE_PATH}
[ $ -eq 0 ] && action $"Add $1" /bin/true
chattr +i ${FILE_PATH}
fi
;;
-d|-del)
shift
if [ `grep "\b$1\b" ${FILE_PATH}|wc -l` -lt 1 ];then
action $"vpnuser,$1 is not exist." /bin/false
exit
else
chattr -i ${FILE_PATH}
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
sed -i "/^${1}$/d" ${FILE_PATH}
[ $ -eq 0 ] && action $"Del $1" /bin/true
chattr +i ${FILE_PATH}
exit
fi
;;
-s|-search)
shift
if [ `grep -w "$1" ${FILE_PATH}|wc -l` -lt 1 ];then
echo $"vpnuser,$1 is not exist.";exit
else
echo $"vpnuser,$1 is exist.";exit
fi
;;
*)
usage
exit
;;
esac
07、Nginx Web服务管理:cat /etc/init.d/nginxd
#!/bin/sh
# chkconfig: 2345 40 98
# description: Start/Stop Nginx server
path=/application/nginx/sbin
pid=/application/nginx/logs/nginx.pid
RETVAL=0
. /etc/init.d/functions
start(){
if [ ! -f $pid ];then
#if [ `netstat -lntup|grep nginx|wc -l` -eq 0 ];then
$path/nginx
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "nginx is started" /bin/true
return $RETVAL
else
action "nginx is started" /bin/false
return $RETVAL
fi
else
echo "nginx is running"
return 0
fi
}
stop(){
if [ -f $pid ];then
#if [ `netstat -lntup|grep nginx|wc -l` -eq 0 ];then
$path/nginx -s stop
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "nginx is stopped" /bin/true
return $RETVAL
else
action "nginx is stopped" /bin/false
return $RETVAL
fi
else
echo "nginx is no running"
return $RETVAL
fi
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
stop
sleep 1
start
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
----------
chkconfig --add nginxd
chkconfig --list nginxd
08、yum安装下的Nginx启动服务cat /etc/init.d/nginx -n
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run/${prog}.pid"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $prog
retval=$
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || return 6
stop
start
}
reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}
# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
浙公网安备 33010602011771号