第二部分 Linux Shell高级编程技巧——第二章 Shell工具

笔记

#Shell工具
#日志文件
#编辑
#vi datelog.sh
#查看文件内容
[root@localhost 0418]# cat datelog.sh 
#!/bin/bash
#datelog.sh
#当前的日期
current_date=`date "+%Y%m%d"`
#今天的日志文件名
todaylog="log/${current_date}.log}
#如果日志文件不存在,创建一个
if [ ! -f $todaylog ]
then
        touch $todaylog
fi
#输出日志到日志文件
log_time_format=`date "+%Y-%m-%d %T"`
echo "${log_time_format} 命令开始" >>$todaylog
#
# command blocks
sleep 4
#
#输出日志到日志文件
log_time_format=`date "+%Y-%m-%d %T"`
echo "${log_time_format} 命令结束" >>$todaylog
#执行
[root@localhost 0418]# ./datelog.sh   
#查看当前日期
[root@localhost 0418]# date
Thu Apr 18 11:35:52 CST 2013
You have new mail in /var/spool/mail/root
#查看date帮助手册
[root@localhost 0418]# man date
#编辑
vi processlog.sh
#改变权限
[root@localhost 0418]# chmod 755 processlog.sh
#启动httpd服务
[root@localhost 0418]# service httpd start  
Starting httpd: perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "en_US.en"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
                                                           [  OK  ]
#查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -aux|grep "/usr/sbin/httpd"|grep -v " grep "
root     19202  1.9  3.3 19656 8624 ?        S    12:09   0:00 /usr/sbin/httpd
#查看httpd进程详细信息并去除grep、打印进程号
[root@localhost 0418]# ps -aux|grep "/usr/sbin/httpd"|grep -v "grep"|awk '{print $2}'
19202
#执行
[root@localhost 0418]# ./processlog.sh 
#查看
[root@localhost 0418]# cat /t
tftpboot  tmp   
#查看tmp目录,发现19304.txt文件    
[root@localhost 0418]# cat /tmp/
.X0-lock
.X11-unix
.fam_socket
.font-unix
.gdm_socket
.iroha_unix
19304.txt
OSL_PIPE_0_SingleOfficeIPC_e31798b4554fdedd4fea48cd4e93b7
VMwareDnD
jd_sockV4
orbit-root
orbit-student
ssh-XXCs45Ko
vmware-root
#查看19304.txt内容
[root@localhost 0418]# cat /tmp/19304.txt 
19202
#手动删除
[root@localhost 0418]# rm -f /tmp/19304.txt 
#执行
[root@localhost 0418]# ./processlog.sh 
rm -f /tmp/19421.txt
#重新查看,发现没有内容
[root@localhost 0418]# cat /tmp/19421.txt
cat: /tmp/19421.txt: No such file or directory

#信号
#查看所有信号
[root@localhost 0418]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     17) SIGCHLD
18) SIGCONT     19) SIGSTOP     20) SIGTSTP     21) SIGTTIN
22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO
30) SIGPWR      31) SIGSYS      33) SIGRTMIN    34) SIGRTMIN+1
35) SIGRTMIN+2  36) SIGRTMIN+3  37) SIGRTMIN+4  38) SIGRTMIN+5
39) SIGRTMIN+6  40) SIGRTMIN+7  41) SIGRTMIN+8  42) SIGRTMIN+9
43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13
47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-14 50) SIGRTMAX-13
51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10 54) SIGRTMAX-9
55) SIGRTMAX-8  56) SIGRTMAX-7  57) SIGRTMAX-6  58) SIGRTMAX-5
59) SIGRTMAX-4  60) SIGRTMAX-3  61) SIGRTMAX-2  62) SIGRTMAX-1
63) SIGRTMAX
#查看httpd进程详细信息
[root@localhost 0418]# ps -ef |grep "httpd"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
apache   19210 19202  0 12:09 ?        00:00:00 [httpd]
apache   19211 19202  0 12:09 ?        00:00:00 [httpd]
apache   19212 19202  0 12:09 ?        00:00:00 [httpd]
root     19533  2658  0 12:23 pts/0    00:00:00 grep httpd
#查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
apache   19210 19202  0 12:09 ?        00:00:00 [httpd]
apache   19211 19202  0 12:09 ?        00:00:00 [httpd]
apache   19212 19202  0 12:09 ?        00:00:00 [httpd]
#杀死进程19212
[root@localhost 0418]# kill -s SIGKILL 19212
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
apache   19210 19202  0 12:09 ?        00:00:00 [httpd]
apache   19211 19202  0 12:09 ?        00:00:00 [httpd]
#功能同上
[root@localhost 0418]# kill -9 19211   
#重新查看httpd进程详细信息并去除grep ,发现没有该进程,说明被终止    
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
apache   19210 19202  0 12:09 ?        00:00:00 [httpd]
#挂起进程
[root@localhost 0418]# kill -s SIGHUP 19210 
#查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
#重新查看查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19209 19202  0 12:09 ?        00:00:00 [httpd]
#杀死子进程或者父进程下的所有子进程
[root@localhost 0418]# kill -1 19209
#查看httpd进程详细信息并去除grep      
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19616 19202  0 12:26 ?        00:00:00 [httpd]
#查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
apache   19205 19202  0 12:09 ?        00:00:00 [httpd]
apache   19206 19202  0 12:09 ?        00:00:00 [httpd]
apache   19207 19202  0 12:09 ?        00:00:00 [httpd]
apache   19208 19202  0 12:09 ?        00:00:00 [httpd]
apache   19616 19202  0 12:26 ?        00:00:00 [httpd]
#结束进程号为19202的进程
[root@localhost 0418]# kill -1 19202
#查看httpd进程详细信息并去除grep
[root@localhost 0418]# ps -ef |grep "httpd"|grep -v "grep"
root     19202     1  0 12:09 ?        00:00:00 /usr/sbin/httpd
exit crtl+D退出当前shell


#trap捕捉信号
#建立文件夹0419
mkdir 0419
#进入该目录
cd 0419
#拷贝0418所有文件到当前目录
[root@localhost 0419]# cp ../0418/* .
#编辑
[root@localhost 0419]# vi trap1.sh
#查看内容
[root@localhost 0419]# cat trap1.sh 
#!/bin/bash
#trap1.sh
#注意此处为单引号,不是反引号
trap ‘exitprocess’ 2
LOOP=0
function exitprocess()
{
        echo "You just hit <CRTL-C>, at number $LOOP"
        echo "I will now exit"
        exit 1
}
while :
do
        LOOP=$[$LOOP+1]
        echo $LOOP
done
#改变权限
[root@localhost 0419]# chmod 755 trap1.sh
#执行
[root@localhost 0419]# ./trap1.sh
#另一个终端查看进程
[root@localhost 0419]# ps –ef
root      4221  2113  0 15:57 ?        00:00:00 /usr/sbin/sshd
root      4241  4221  0 15:58 pts/1    00:00:00 -bash
root      4834  4180  6 16:19 pts/0    00:00:00 /bin/bash ./trap1.sh
#结束进程
kill -2 4834
#执行终端打印信息
You just hit <CRTL-C>, at number 38383
I will now exit
#重新编辑
[root@localhost 0419]# vi trap1.sh
#查看内容,增加休眠调用
[root@localhost 0419]# cat trap1.sh 
#!/bin/bash
#trap1.sh
trap ‘exitprocess’ 2
LOOP=0
function exitprocess()
{
        echo "You just hit <CRTL-C>, at number $LOOP"
        echo "I will now exit"
        exit 1
}
while :
do
        LOOP=$[$LOOP+1]
        echo $LOOP
		 #增加代码
        sleep 1
done
#执行
[root@localhost 0419]# ./trap1.sh
root      5245  4180  0 16:35 pts/0    00:00:00 /bin/bash ./trap1.sh
#另一个终端,结束进程
kill -4 5245
#当前终端,显示信息
Illegal instruction
#不做任何处理
kill -3 5305
#会做处理
kill -2 5305
#拷贝文件
[root@localhost 0419]# cp trap1.sh trap2.sh
#查看内容
[root@localhost 0419]# cat trap2.sh 
#!/bin/bash
#trap2.sh
LOOP=0
trap 'exitprocess' 2
HOLD1=/tmp/ho1d1.$$
HOLD2=/tmp/hold2.$$
function exitprocess()
{
        echo -e "\nRecived Interrupt ..."
        echo -n "Do you really wish to exit?(Y?N)"
        read ANS
        case $ANS in
        Y|y)
                rm_tmp_file
                ;;
        N|n)
                ;;
        *)
                exitprocess
                ;;
        esac

}
function rm_tmp_file()
{
        echo "<CRTL-C> detected .. Now cleaning up ...wait"
        rm /tmp/*.$$ 2>/dev/null
        exit 1
}
while :
do
        LOOP=$[$LOOP+1]
        echo $LOOP
        df>>$HOLD1
        ps -xa >>$HOLD2
        sleep 1
done
#执行
[root@localhost 0419]# ./trap2.sh 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

Recived Interrupt ...
Do you really wish to exit?(Y?N)N
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

Recived Interrupt ...
Do you really wish to exit?(Y?N)y
<CRTL-C> detected .. Now cleaning up ...wait
#查看tmp目录下的内容
ls -al /tmp/
-rw-r--r--    1 root     root         3816 Apr 19 16:50 ho1d1.5503
-rw-r--r--    1 root     root         3816 Apr 19 16:50 ho1d2.5503
#另一个终端查看进程
ps –ef
#结束进程
kill -2 5503

#eval
#执行
[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`
-bash: total: command not found
#编辑
vi myfile
#查看内容
[root@localhost 0419]# cat myfile 
ls –al
#执行
[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`
total 32
drwxr-xr-x    2 root     root         4096 Apr 19 16:54 .
drwxr-xr-x   15 root     root         4096 Apr 19 15:58 ..
-rwxr-xr-x    1 root     root          431 Apr 19 15:59 awkif.sh
-rwxr-xr-x    1 root     root          435 Apr 19 15:59 datelog.sh
-rw-r--r--    1 root     root            7 Apr 19 16:54 myfile
-rwxr-xr-x    1 root     root          391 Apr 19 15:59 processlog.sh
-rwxr-xr-x    1 root     root          210 Apr 19 16:35 trap1.sh
-rwxr-xr-x    1 root     root          484 Apr 19 16:45 trap2.sh
#重新编辑
[root@localhost 0419]# vi myfile 
#查看内容
[root@localhost 0419]# cat myfile 
ls
#重新执行
[root@localhost 0419]# MYFILE="cat myfile"; `eval $MYFILE`
awkif.sh  datelog.sh  myfile  processlog.sh  trap1.sh  trap2.sh
#可以不使用变量
[root@localhost 0419]# eval `cat myfile` 
awkif.sh  datelog.sh  myfile  processlog.sh  trap1.sh  trap2.sh

#logger
#查看messages内容
[root@localhost 0419]# cat /var/log/messages
#向日志文件增加内容
[root@localhost 0419]# logger -i "chinaitlab shenzhen"
Apr 19 17:01:09 localhost root[6179]: chinaitlab shenzhen
[root@localhost 0419]# logger -p 19 -i "chinaitlab shenzhen"
#再次查看
[root@localhost 0419]# cat /var/log/messages
Apr 19 17:01:09 localhost root[6179]: chinaitlab shenzhen
Apr 19 17:01:56 localhost root[6199]: chinaitlab shenzhen
#向日志文件增加内容
[root@localhost 0419]# logger -p 1 -i "chinaitlab shenzhen"
#再次查看
[root@localhost 0419]# cat /var/log/messages
Apr 19 17:01:09 localhost root[6179]: chinaitlab shenzhen
Apr 19 17:01:56 localhost root[6199]: chinaitlab shenzhen
Apr 19 17:02:25 localhost root[6201]: chinaitlab shenzhen


 

附图

 

 

 

 

 

 

 

 

 

katoon Sina   CSDN
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]
posted @ 2013-04-19 22:03  javawebsoa  Views(184)  Comments(0Edit  收藏  举报