1.取得脚本所在绝对路径
script_dir=$(pwd $0)
script_dir=$(cd `dirname $0`;pwd)
2.awk取磁盘分区使用量的特殊情况
df -Ph |awk '{print $5}'
df -h来查看分区使用量,有一种特殊情况是磁盘的filesystem很长,一般是lv名字,会占据两行,这对于awk来说是比较无奈的,举个栗子:
[dissh_moni@localhost ~]$ df /dev/mapper*
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/ddf1_4c5349202020202010000055000000004711471100001450p5
40315256 5231124 33036184 14% /
tmpfs 8228656 0 8228656 0% /dev/shm
/dev/mapper/ddf1_4c5349202020202010000055000000004711471100001450p1
495844 158972 311272 34% /boot
/dev/mapper/ddf1_4c5349202020202010000055000000004711471100001450p4
55072164 22256368 30018280 43% /data
/dev/mapper/ddf1_4c5349202020202010000055000000004711471100001450p2
40316280 889360 37378920 3% /opt
/dev/sdb 8642088460 6653495336 1549600228 82% /moives
这时候就需要利用df -Ph 命令,这个命令会让输出格式标准一点,df其他的使用方法,可以自己百度一下
3.输出重定向
正常输出(STDOUT )的重定向 用 ‘>’ 或 ‘1>’,错误输出(STDERR )的重定向用’2>’
echo 'okokok' > ok.txt #正常输出重定向到ok.txt
ls /notdir 2> test.txt #ls一个不存在的目录,输出会重定向到test.txt
./test.sh 1>ok.txt 2>bad.txt #脚本test.sh的STDOUT,STDERR分开输出
./test.sh 1>ok.txt 2>&1 #STDERR重定向STDOUT中
如果你愿意,可以将STDERR 和 STDOUT 的输出重定向到一个输出文件,为此,bash 提供了特殊的重定向符号 &>
ls file nofile &> /dev/null
我们如何在脚本里面重定向呢?没有什么特别之处,和普通重定向一样。
#!/bin/bash
#redirecting output to different locations
echo "now redirecting all output to another location" &>/dev/null
问题就来了,如果我们要将所有的输出都重定向到某个文件呢?我们都不希望每次输出的时候都重定向一下吧,正所谓,山人自有妙计。我们可以用exec 来永久重定向,如下所示:
#!/bin/bash
#redirecting output to different locations
exec 2>testerror
echo "This is the start of the script"
echo "now redirecting all output to another location"
exec 1>testout
echo "This output should go to testout file"
echo "but this should go the the testerror file" >& 2
输出结果如下所示:
This is the start of the script
now redirecting all output to another location
lalor@lalor:~/temp$ cat testout
This output should go to testout file
lalor@lalor:~/temp$ cat testerror
but this should go the the testerror file
lalor@lalor:~/temp$
以追加的方式重定向:
exec 3 >> testout
取消重定向:
exec 3> -
4.捕捉信号(trap):
在Shell程序运行的时候,可能收到各种信号,有的来自于操作系统,有的来自于键盘,而该Shell在收到信号后就立刻终止运行。但是在有些时候,你可能并不希望在信号到达时,程序就立刻停止运行并退出。而是他能希望忽略这个信号而一直在运行,或者在退出前作一些清除操作。trap命令就允许你控制你的程序在收到信号以后的行为。
其格式如下:
trap 'command; command' signal-number
trap 'command; command' signal-name
trap signal-number
trap signal-name
后面的两种形式主要用于信号复位,即恢复处理该信号的缺省行为。还需要说明的是,如果trap后面的命令是使用单引号括起来的,那么该命令只有在捕获到指定信号时才被执行。如果是双引号,则是在trap设置时就可以执行变量和命令替换了。
下面是系统给出的信号数字和信号名称的对照表:
1)SIGHUP 2)SIGINT 3)SIGQUIT 4)SIGILL 5)SIGTRAP 6)SIGABRT 7)SIGBUS 8)SIGFPE
9)SIGKILL 10) SIGUSR1 11)SIGEGV 12)SIGUSR2 13)SIGPIPE 14)SIGALRM 15)SIGTERM 17)SIGCHLD
18)SIGCONT 19)SIGSTOP ... ...
见如下示例脚本:
trap 'rm tmp*;exit 1' 1 2 15
#该命令表示在收到信号1、2和15时,该脚本将先执行rm tmp*,然后exit 1退出
trap 2 #当收到信号2时,将恢复为以前的动作,即退出。
trap " " 1 2 #当收到信号1和2时,将忽略这两个信号。
trap - #表示恢复所有信号处理的原始值。
trap 'trap 2' 2 #在第一次收到信号2时,执行trap 2,这时将信号2的处理恢复为缺省模式。在收到信号2时,Shell程序退出。
#test2.sh
trap 'echo "Control+C will not terminate $0."' 2
#捕获信号2,即在键盘上按CTRL+C。
trap 'echo "Control+\ will not terminate $0."' 3
#捕获信号3,即在键盘上按CTRL+\。
echo "Enter stop to quit shell."
while true
do
echo -n "Go Go...."
read
if [[ $REPLY == [Ss]top ]] #直到输入stop或Stop才退出
then
break
fi
done
执行过程:
/> . ./test2.sh
Enter stop to quit shell.
Go Go….^CControl+C will not terminate -bash.
^\Control+\ will not terminate -bash.
stop