shell学习笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell学习笔记相关的知识,希望对你有一定的参考价值。

一、

1、利用系统函数模拟实现系统脚本启动特殊颜色效果

1.1 查看系统函数库

[[email protected] ~]# ll /etc/init.d/functions 
-rw-r--r--. 1 root root 13948 Sep 16  2015 /etc/init.d/functions
[[email protected] ~]# 

1.2 脚本

[[email protected] day5]# vim test_startup.sh

#!/bin/sh

# Source function library.
[ -f /etc/init.d/functions ] && . /etc/init.d/functions

if [ "$1" == "start" ];then
    action "nginx starting." /bin/true
elif [ "$1" == "stop" ];then 
    action "nginx is stopped." /bin/true
else
    action "nginx start" /bin/false
fi

测试
[[email protected] day5]# sh test_startup.sh 
nginx start                                                [FAILED]

[[email protected] day5]# sh test_startup.sh start
nginx starting.                                            [  OK  ]
[[email protected] day5]# sh test_startup.sh stop
nginx is stopped.                                          [  OK  ]
[[email protected] day5]# 

提示输入参数:
#!/bin/sh

# Source function library.
[ -f /etc/init.d/functions ] && . /etc/init.d/functions

if [ "$1" == "start" ];then
    action "nginx starting." /bin/true
elif [ "$1" == "stop" ];then
    action "nginx is stopped." /bin/true
else
    echo "USAGE:$0 {START|STOIP}"
    #action "nginx start" /bin/false
    exit
fi

2、检查文件是否被恶意篡改

2.1 方法1

判断里面的值

[[email protected] day5]# vim judge.sh 

#!/bin/sh
. /etc/init.d/functions

if [ `cat a.log | wc -L` -ne 1 ];then
    action "a.log" /bin/false
else
    action "a.log" /bin/true
fi

测试:
[[email protected] day5]# sh judge.sh 
a.log                                                      [  OK  ]
[[email protected] day5]# echo 111 > a.log 
[[email protected] day5]# sh judge.sh 
a.log                                                      [FAILED]
[[email protected] day5]#

多文件测试:
[[email protected] day5]# vim judge.sh 

#!/bin/sh
. /etc/init.d/functions
for f in `ls *.log`
do

    if [ `cat $f | wc -L` -ne 1 ];then
        action "$f" /bin/false
    else
        action "$f" /bin/true
    fi
done

[[email protected] day5]# sh judge.sh 
a.log                                                      [FAILED]
b.log                                                      [  OK  ]
[[email protected] day5]# 

记住默认长度:
[[email protected] day5]# for f in `ls *.log`;do wc -L $f;done > c.txt

[[email protected] day5]# cat judge.sh 
#!/bin/sh
. /etc/init.d/functions
for f in `ls *.log`
do

    if [ `cat $f | wc -L` -ne `grep $f c.txt | awk ‘{print $1}‘` ];then
        action "$f" /bin/false
    else
        action "$f" /bin/true
    fi
done

2.2 杂项

项目上线的时候,记录所有网页文件的内容长度:

[[email protected] day5]# for f in *.html;do wc -L $f;done > c.txt 
[[email protected] day5]# cat c.txt 
4 a.html
10 b.html

[[email protected] day5]# cat judge.sh 
#!/bin/sh
. /etc/init.d/functions
for f in `ls *.html`
do

    if [ `cat $f | wc -L` -ne `grep $f c.txt | awk ‘{print $1}‘` ];then
    action "$f" /bin/false
    else
    action "$f" /bin/true
    fi
done
[[email protected] day5]# sh judge.sh 
a.html                                                     [  OK  ]
b.html                                                     [  OK  ]

2.3 md5比较

a.建立指纹库
[[email protected] day5]# md5sum * > zhiwen.log
d8e8fca2dc0f896fd7cb4cb0031ba249  a.html
d8e8fca2dc0f896fd7cb4cb0031ba249  a.log
d4790652b48d41469dd26dd97c71a894  b.html
d4790652b48d41469dd26dd97c71a894  b.log
fac9b0bb2ed61fbda507fcbd18184f85  c.txt
47465a40feb7a298f96909e397b8008a  judge.sh
b4da16b2efbef8d5800be2693c58457b  test_startup.sh

修改文件:
[[email protected] day5]# md5sum -c zhiwen.log 
a.html: OK
a.log: OK
b.html: OK
b.log: OK
c.txt: OK
judge.sh: OK
test_startup.sh: OK
[[email protected] day5]# echo oldboy > b.log
[[email protected] day5]# md5sum -c zhiwen.log 
a.html: OK
a.log: OK
b.html: OK
b.log: FAILED
c.txt: OK
judge.sh: OK
test_startup.sh: OK
md5sum: WARNING: 1 computed checksum did NOT match

b.实例脚本:
[[email protected] day5]# mkdir oldboy
[[email protected] day5]# touch oldboy/{1..9}.txt
[[email protected] day5]# ls oldboy/
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[[email protected] day5]# vim md5check01.sh 

#!/bin/sh
. /etc/init.d/functions

[[email protected] day5]# vim md5check01.sh 

#!/bin/sh

[ ! -f md5zhiwen.log ] && md5sum oldboy/* > md5zhiwen.log
md5sum -c md5zhiwen.log | grep -i ‘FAILED‘ > error.log
if [ `cat error.log | wc -l` -ge 1  ];then
    echo "`cat error.log`"
fi

测试:
[[email protected] day5]# echo 3 > oldboy/3.txt 
[[email protected] day5]# 
[[email protected] day5]# 
[[email protected] day5]# sh md5check01.sh 
md5sum: WARNING: 2 computed checksums did NOT match
oldboy/1.txt: FAILED
oldboy/3.txt: FAILED

c.脚本2
vim :g/ggg/s//root/g

生成指纹库:
[[email protected] day5]# find /server/scripts/day5/ -type f -name "*" -exec md5sum {}>/root/checkmd5.db \;

[[email protected] day5]# vim md5check02.sh 

#!/bin/bash

CHECK_DIR=/server/scripts/day5
if [ -e /root/checkmd5.db ];then
    md5sum -c /root/checkmd5.db > /root/result.db 2>/dev/null
    egrep -i "FAILED" /root/result.db > /root/err.log
    if [ -s /root/err.log ];then
        mail -s "`uname -n ` $(date +%F) web is error" [email protected] < /root/err.log
    else
        echo "file is not modife" | mail -s "web is true" [email protected]
    fi
fi

测试:
[[email protected] day5]# echo "djfldjfld" > a.log 
[[email protected] day5]# sh -x md5check02.sh 
+ CHECK_DIR=/server/scripts/day5
+ ‘[‘ -e /root/checkmd5.db ‘]‘
+ md5sum -c /root/checkmd5.db
+ egrep -i FAILED /root/result.db
+ ‘[‘ -s /root/err.log ‘]‘
++ uname -n
++ date +%F
+ mail -s ‘master4.com 2018-01-14 web is error‘ [email protected]

放到计划任务:
[[email protected] day5]# crontab -e

#md5 check etc
***** /bin/sh /server/scripts/day5/md5check02.sh > /dev/null 2>&1

3、监听端口的联通性

3.1 telnet

[[email protected] ~]# telnet 10.201.106.134 80
Trying 10.201.106.134...
Connected to 10.201.106.134.
Escape character is ‘^]‘.

[[email protected] ~]# echo -e "\n" | telnet 10.201.106.134 80 | grep Connected
Connected to 10.201.106.134.
Connection closed by foreign host.

[[email protected] ~]# echo -e "\n" | telnet 10.201.106.134 80 | grep Connected | wc -l
Connection closed by foreign host.
1

3.2 namp

[[email protected] ~]# nmap 10.201.106.134 -p 80 | grep open
80/tcp open  http
[[email protected] ~]# nmap 10.201.106.134 -p 80 | grep open | wc -l
1
[[email protected] ~]# 

3.3 nc

[[email protected] ~]# nc -w 2 10.201.106.134 80 && echo ok || echo no
ok
[[email protected] ~]# nc -w 2 10.201.106.134 80 && echo ok || echo no
no
[[email protected] ~]#

4、监控memcached

http://oldboy.blog.51cto.com/2561410/942530

二、case

1、例子

[[email protected] day5]# cat case-1.sh 
#!/bin/bash
# this script is created by xsz
# e-mail:[email protected]
# qqinfo:666
# version:1.1

read -p "Please input a number:" ans
case "$ans" in
1)
    echo "the num you input is 1"
;;
2)
    echo "the num you input is 2"
;;
[3-9])
    echo "the num you input is $ans"
;;
*)
    echo "the num you input must be less 9."
    exit
esac

测试
[[email protected] day5]# sh case-1.sh
Please input a number:1
the num you input is 1
[[email protected] day5]# sh case-1.sh
Please input a number:2
the num you input is 2
[[email protected] day5]# sh case-1.sh
Please input a number:8
the num you input is 8

[[email protected] day5]# cat case-if.sh 
#!/bin/sh

read -p "please input a number:" ans
if [ $ans -eq 1 ];then
     echo "the num you input is 1"  
elif [ $ans -eq 2 ];then
    echo "the num your input 2"
elif [ $ans -ge 3 -a $ans -le 9 ];then
    echo "the num you input is $ans"
else
    echo "the num you input must be less 9"
    exit
fi

2、例子2-选择喜欢的水果并配色

[[email protected] day5]# cat case-2.sh 
#!/bin/bash
# xsz
# e-mail:[email protected]
# qq:666
# function:case example
# version:1.1
# color defined

RED_COLOR=‘\E[1;31m‘
GREEN_COLOR=‘\E[1;32m‘
YELLOW_COLOR=‘\E[1;33m‘
BLUE_COLOR=‘\E[1;34m‘
RES=‘\E[0m‘

read -p "Please input the fruit name you like :" ans
case "$ans" in
apple | APPLE)
    echo -e "the fruit name you like is ${RED_COLOR}"$ans."${RES}"
;;

banana | BANANA)
    echo -e "the fruit name you like is ${YELLOW_COLOR}$ans.${RES}"
;;

pear | PEAR)
    echo -e "the fruit name you like is ${GREEN_COLOR}"$ans."${RES}"
;;

*)
    echo -e "Here is not the fruit name you like--${BLUE_COLOR}"$ans."${RES}"
    exit
;;

esac

3、给字符串显示不同颜色

3.1 直接加颜色

[[email protected] day5]# cat color.sh 
echo -e "\033[30m 黑色字 oldboy trainning \033[0m"
echo -e "\033[31m 红色字 oldboy trainning \033[0m"
echo -e "\033[32m 绿色字 oldboy trainning \033[0m"
echo -e "\033[33m ×××字 oldboy trainning \033[0m"
echo -e "\033[34m 蓝色字 oldboy trainning \033[0m"
echo -e "\033[35m 紫色字 oldboy trainning \033[0m"
echo -e "\033[36m 天蓝字 oldboy trainning \033[0m"
echo -e "\033[37m 白色字 oldboy trainning \033[0m"

3.2 脚本,定义变量

[[email protected] day5]# cat color2.sh 
#!/bin/sh
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‘

echo -e "${RED_COLOR}===red color===${RES}"
echo -e "${YELLOW_COLOR}===yellow color===${RES}"
echo -e "${BLUE_COLOR}===blue color===${RES}"
echo -e "${GREEN_COLOR}===green color===${RES}"
echo -e "${PINK}===pink color===${RES}"
[[email protected] day5]# 
[[email protected] day5]# sh color2.sh 
===red color===
===yellow color===
===blue color===
===green color===
===pink color===
[[email protected] day5]# 

3.3 参考function的脚本

[[email protected] day5]# cat color3.sh 
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[1;39m"
echo ---oldboy trainning--- && $SETCOLOR_SUCCESS
echo ---oldboy trainning--- && $SETCOLOR_FAILURE
echo ---oldboy trainning--- && $SETCOLOR_WARNING
echo ---oldboy trainning--- && $SETCOLOR_NORMAL

3.4 给内容加颜色的函数

在脚本命令行传2个参数,给指定内容(第一个参数)加指定颜色(第二个颜色)

[[email protected] day5]# cat color4.sh 
#!/bin/bash

RED_COLOR=‘\E[1;31m‘
GREEN_COLOR=‘\E[1;32m‘
YELLOW_COLOR=‘\E[1;33m‘
BLUE_COLOR=‘\E[1;34m‘
PINK_COLOR=‘\E[1;35m‘
RES=‘\E[0m‘

if [ $# -ne 2 ];then
    echo "Usage $0 content {red|yellow|blue|green|pink}"
    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 color {red|yellow|blue|green|pink}"
    exit
    ;;

esac

改造成read脚本:
[[email protected] day5]# cat color-read.sh 
#!/bin/bash

RED_COLOR=‘\E[1;31m‘
GREEN_COLOR=‘\E[1;32m‘
YELLOW_COLOR=‘\E[1;33m‘
BLUE_COLOR=‘\E[1;34m‘
PINK_COLOR=‘\E[1;35m‘
RES=‘\E[0m‘

read -t 9 -p "Please input tow char,format:content color:" content color
[ -z "$content" -o -z "$color" ] && {
    echo "Usage $0 content {red|yellow|blue|green|pink}"
    exit
}

case "$color" in
red|RED)
    echo -e "${RED_COLOR}$color${RES}"
    ;;

yellow|YELLOW)
    echo -e "${YELLOW_COLOR}$color${RES}"
    ;;

green|GREEN)
    echo -e "${GREEN_COLOR}$color${RES}"
    ;;

blue|BLUE)
    echo -e "${BLUE_COLOR}$color${RES}"
    ;;

pink|PINK)
    echo -e "${PINK_COLOR}$color${RES}"
    ;;

*)
    echo "Usage color {red|yellow|blue|green|pink}"
    exit
    ;;

esac

3.5 背景颜色

[[email protected] day5]# cat color5.sh 
echo -e "\033[40;37m 黑底白字 welcome \033[0m"
echo -e "\033[41;37m 红底白字 welcome \033[0m"
echo -e "\033[42;37m 绿底白字 welcome \033[0m"
echo -e "\033[43;37m 黄底白字 welcome \033[0m"
echo -e "\033[44;37m 蓝底白字 welcome \033[0m"
echo -e "\033[45;37m 紫底白字 welcome \033[0m"
echo -e "\033[46;37m 天蓝白字 welcome \033[0m"
echo -e "\033[47;30m 白底黑字 welcome \033[0m"
[[email protected] day5]# 

技术分享图片

三、shell函数语法与案列

1、范例1

[[email protected] day5]# cat func01.sh 
#!/bin/bash

oldboy() {
 echo "I am oldboy."
}

oldboy
[[email protected] day5]# sh func01.sh 
I am oldboy.
[[email protected] day5]# 

2、使用其它脚本调用上面的函数脚本

[[email protected] day5]# cat func01.sh 
#!/bin/bash

oldboy() {
 echo "I am oldboy."
}

[[email protected] day5]# cat exec.sh 
. ./func01.sh
oldboy

[[email protected] day5]# sh exec.sh 
I am oldboy.
[[email protected] day5]# 

3、在脚本中通过函数给指定的字符串内容加指定颜色

[[email protected] day5]# cat color-test.sh 
#!/bin/bash

echo_chars() {

    RED_COLOR=‘\E[1;31m‘
    GREEN_COLOR=‘\E[1;32m‘
    YELLOW_COLOR=‘\E[1;33m‘
    BLUE_COLOR=‘\E[1;34m‘
    PINK_COLOR=‘\E[1;35m‘
    RES=‘\E[0m‘

    if [ $# -ne 2 ];then
        echo "Usage $0 content {red|yellow|blue|green|pink}"
        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 1 {red|yellow|blue|green|pink}"
        exit
        ;;

    esac

}

echo_chars xsz blue
echo_chars xsz yellow
echo_chars xsz green
echo_chars xsz red
echo_chars xsz xxue

4、通过case实现rsync服务启动脚本

4.1 脚本1

[[email protected] day5]# rsync --daemon

[[email protected] day5]# 
[[email protected] day5]# ps -ef | grep rsync
root      6884     1  0 23:32 ?        00:00:00 rsync --daemon

[[email protected] day5]# lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   6884 root    4u  IPv4 304578      0t0  TCP *:rsync (LISTEN)
rsync   6884 root    5u  IPv6 304579      0t0  TCP *:rsync (LISTEN)

[[email protected] day5]# cat /etc/rsyncd.conf

uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /va/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.201.106.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
# rsync_config___________end
[oldboy]
path = /oldboy/
[data]
path = /data/
[backup]
path = /backup/
# rsync_config___________end

[[email protected] day5]# vim /etc/rsyncd.password

rsync_bakup:rsync1234

启动脚本:
[[email protected] day5]# cat rsyncd 
#!/bin/bash

case "$1" in
    start)
    rsync --daemon
    [ $? -eq 0 ] && echo "rsync started successful"
;;
    stop)
    pkill rsync;sleep 1
    [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ] &&    echo "rsync stopped successful"
;;
    restart)
    pkill rsync
    sleep 1
    rsync --daemon
    [ $? -eq 0 ] && echo "rsync restarted successful"
;;
    status)
    pid_status=`lsof -i :873 | awk ‘{print $2}‘ | grep [0-9] | uniq`
    [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running."
;;
    *)
    echo "Usage:$0 {start|stop|restart}"
;;    

esac

[[email protected] day5]# sh rsyncd start
rsync started successful
[[email protected] day5]# sh rsyncd stop
rsync stopped successful
[[email protected] day5]# sh rsyncd restart
rsync restarted successful
[[email protected] day5]# sh rsyncd status
rsync is running,pid(8987)

4.2 脚本2

上面脚本改进:

[[email protected] day5]# cat rsyncd02 
#!/bin/bash
. /etc/init.d/functions

case "$1" in
    start)
    [ -e /var/run/rsyncd.pid ] && echo "rsync is staring" && exit
    rsync --daemon
    if [ $? -eq 0 ];then
      action "rsync started" /bin/true
    else
      action "rsync started" /bin/false
    fi
;;
    stop)
    pkill rsync;sleep 1
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then
       action  "rsync stopped" /bin/true
    else
        action  "rsync stopped" /bin/false
    fi
;;
    restart)
    pkill rsync
    sleep 1
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then
        flag=0
        action  "rsync stopped" /bin/true
    else
        action  "rsync stopped" /bin/false
    fi
    rsync --daemon
    if [ $? -eq 0 -a $flag -eq 0 ];then
       action "rsync restarted" /bin/true
    else
       action "rsync restarted" /bin/false
    fi
;;
    status)
    pid_status=`lsof -i :873 | awk ‘{print $2}‘ | grep [0-9] | uniq`
    [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running."
;;
    *)
    echo "Usage:$0 {start|stop|restart}"
;;    

esac

测试:
[[email protected] day5]# sh rsyncd start
rsync started                                              [  OK  ]
[[email protected] day5]# sh rsyncd start
rsync is staring
[[email protected] day5]# 
[[email protected] day5]# sh rsyncd restart
rsync stopped                                              [  OK  ]
rsync restarted                                            [  OK  ]

4.3 脚本3、函数化脚本

继续改进:
[[email protected] ~]# cat /etc/init.d/rsd
#!/bin/bash
. /etc/init.d/functions

start () {
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -ge 1 ];then
    echo "rsyncd running."
    exit
    fi
    rsync --daemon
    if [ $? -eq 0 ];then
    action "rsync started" /bin/true
    else
        action "rsync started" /bin/false
    fi
}

stop(){
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then
        echo "rsyncd is stopped."
        exit
    fi
    pkill rsync
    sleep 1
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then
        action  "rsync stopped" /bin/true
    else
        action  "rsync stopped" /bin/false
    fi
}

restart() {
    if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then
    start
    else
    stop
    start
    fi
}

status() {
    pid_status=`lsof -i :873 | awk ‘{print $2}‘ | grep [0-9] | uniq`
    [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running."
}

case "$1" in
    start)
    start
    RETUVAL=$?
;;
    stop)
    stop
    RETUVAL=$?
;;
    restart)
    restart
    RETUVAL=$?
;;
    status)
    status
    RETUVAL=$?
;;
    *)
    echo "Usage:$0 {start|stop|restart}"
;;    

esac
exit $RETUVAL

需要改名否则会异常
[[email protected] day5]# mv rsyncd rsd

给文件增加执行权限:
[[email protected] day5]# chmod +x rsd

拷贝到启动路径:
[[email protected] day5]# cp rsd /etc/init.d/

之前的系统环境是centos7,默认调用systemctl需要拷贝脚本和配置到centos6运行测试:

[[email protected] ~]# /etc/init.d/rsd start
rsync started                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd start
rsyncd running.
[[email protected] ~]# /etc/init.d/rsd start
rsyncd running.
[[email protected] ~]# /etc/init.d/rsd stop
rsync stopped                                              [  OK  ]

[[email protected] ~]# /etc/init.d/rsd start
rsync started                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd restart
rsync stopped                                              [  OK  ]
rsync started                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd status
rsync is running,pid(11520)
[[email protected] ~]# 

测试:
[[email protected] ~]# /etc/init.d/rsd status
rsync is running,pid(11701)
[[email protected] ~]# /etc/init.d/rsd stop
rsync stopped                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd start
rsync started                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd status
rsync is running,pid(11762)
[[email protected] ~]# /etc/init.d/rsd restart
rsync stopped                                              [  OK  ]
rsync started                                              [  OK  ]
[[email protected] ~]# /etc/init.d/rsd status
rsync is running,pid(11804)
[[email protected] ~]# 

放到系统自启动服务:
[[email protected] ~]# vim /etc/init.d/rsd 

#!/bin/bash
. /etc/init.d/functions
# chkconfig: 2345 21 81

[[email protected] ~]# chkconfig --add rsd

[[email protected] ~]# chkconfig --list | grep rsd
rsd             0:off   1:off   2:on    3:on    4:on    5:on    6:off
[[email protected] ~]# 

[[email protected] ~]# chkconfig --level 234 rsd on
[[email protected] ~]# chkconfig --list rsd
rsd             0:off   1:off   2:on    3:on    4:on    5:off   6:off

测试:
[[email protected] ~]# service rsd status
rsync is running,pid(11804)
[[email protected] ~]# service rsd stop
rsync stopped                                              [  OK  ]
[[email protected] ~]# service rsd start
rsync started                                              [  OK  ]
[[email protected] ~]# service rsd status
rsync is running,pid(11980)
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# service rsd restart
rsync stopped                                              [  OK  ]
rsync started                                              [  OK  ]

4.4 mysql多实例启动关闭脚本

[[email protected] 3306]# cat my.cnf 
[mysqld]
datadir=/data/3306/
socket=/data/3306/mysql.sock
user=mysql
symbolic-links=0
port=3306
innodb_file_per_table= 1

[mysqld_safe]
log-error=/data/3306/mysqld.log
pid-file=/data/3306/mysqld.pid

[[email protected] 3307]# cat my.cnf 
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
user=mysql
symbolic-links=0
port=3307
innodb_file_per_table= 1

[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-file=/data/3307/mysqld.pid

初始化实例
/usb/bin/mysql_install_db  --datadir=/data/3306
/usb/bin/mysql_install_db  --datadir=/data/3307

后台运行:
[[email protected] 3306]# /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &

[[email protected] 3307]# /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &

[[email protected] mysql]# netstat -tanp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3275/mysqld         
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      3428/mysqld         
[[email protected] mysql]# 

脚本1:通过sock判断:
[[email protected] 3306]# cat mysql3306 
#!/bin/sh
########################
# this scripts is created by xsz at 2018-02-12
# xsz QQ:66
# function:mysqld start scripts
########################

#init
port=3306
mysql_user="root"
mysql_pwd="test3306"
CmdPath="/usr/bin"
mysql_sock="/data/${port}/mysql.sock"

#startup function
function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
    printf "Starting MySQL...\n"
    /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
    else
    print "MySQL is running...\n"
    exit
    fi
}

#stop function
function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
    printf "MySQL is stopped...\n"
    exit
    else
    printf "Stoping MySQL...\n"
    ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown

    fi
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 1
    function_start_mysql
}

case "$1" in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql3306 {start|stop|restart}\n"
esac

[[email protected] 3306]# ./mysql3306 stop
Stoping MySQL...
[[email protected] 3306]# ./mysql3306 start
Starting MySQL...
[[email protected] 3306]# ./mysql3306 restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...

脚本2:通过端口判断:
[[email protected] 3306]# cat mysql-port3306 
#!/bin/sh
########################
# this scripts is created by xsz at 2018-02-12
# xsz QQ:66
# function:mysqld start scripts
########################

#init
port=3306
mysql_user="root"
mysql_pwd="test3306"
CmdPath="/usr/bin"
mysql_sock="/data/${port}/mysql.sock"

#startup function
function_start_mysql()
{
    if [ `netstat -lnt | grep $port | wc -l` -eq 0 ];then
    printf "Starting MySQL...\n"
    /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
    else
    print "MySQL is running...\n"
    exit
    fi
}

#stop function
function_stop_mysql()
{
    if [ `netstat -lnt | grep $port | wc -l` -eq 0 ];then
    printf "MySQL is stopped...\n"
    exit
    else
    printf "Stoping MySQL...\n"
    ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown

    fi
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 1
    function_start_mysql
}

case "$1" in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql3306 {start|stop|restart}\n"
esac

以上是关于shell学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

Shell脚本(学习笔记1)

shell脚本编程学习笔记-函数

16/11/2017 shell学习笔记

linux学习笔记--工程师技术:shell脚本基础

《用Python玩转数据》学习笔记