linux shell学习笔记 4
时间:2007-03-11 来源:wangbinde
[root@localhost ~]# awk -F '#' '{if (gsub("s","S")) print $0}' grade.txt
85#Senior
75#Senior
97#Senior
76#Senior
65#Senior
you have new mail in /var/Spool/mail/root
[root@localhost ~]# awk -F '#' '{if (gsub("s","S" $2)) print $0}' grade.txt
85#Sseniorenior
75#Sseniorenior
97#Sseniorenior
76#Sseniorenior
65#Sseniorenior
you have new mail in /var/Spool/mail/root
[root@localhost ~]# awk -F '#' '{if (gsub("s","S",$2)) print $0}' grade.txt
85 Senior
75 Senior
97 Senior
76 Senior
65 Senior
[root@localhost ~]# awk -F '#' '{print (index ($2,"e")),$2}' grade.txt
2 senior 打印e在senior中第一次出现的位置号
0 junior
2 senior
0 junior
0 junior
2 senior
0 junior
2 senior
0 junior
2 senior
0
\b |
退格键 |
\t |
Tab键 |
\f |
走纸换页 |
\ddd |
八进制值 |
\n |
新行 |
\c |
任意其他特殊字符,例如\\为反斜线符号 |
\r |
回车键 |
[root@localhost ~]# awk -F '#' '{print (index ($2,"e")),"\t",$2}' grade.txt
2 senior
0 junior
2 senior
0 junior
0 junior
2 senior
0 junior
2 senior
0 junior
2 senior
0
Printf的修饰符
%c |
ASCII字符 |
%d |
整数 |
%f |
浮点数,例如 (123,44) |
%e |
|
%f |
新行 |
%g |
Awk决定使用那种浮点数转换e或者f |
%0 |
八进制数 |
%s |
字符串 |
%x |
十六进制数 |
[root@localhost ~]# awk -F '#' '{printf "%c\n",$1}' grade.txt 用ascii码的方式来表示$1
U
W
K
K
V
a
8
L
4
A
Y
[root@localhost ~]# awk -F '#' '{printf "%c\t%d",$1,$1}' grade.txt 分别以%c和%d的格式显示grade.txt文件中的$1
U 85W 87K 75K 75V 86a 978 56L 764 52A 6
5y 0
[root@localhost ~]# awk -F '#' '{printf "%c\t%d\n",$1,$1}' grade.txt
U 85
W 87
K 75
K 75
V 86
a 97
8 56
L 76
4 52
A 65
y 0
[root@localhost ~]# awk -F '#' '{printf "%c\t%d\t%f\n",$1,$1,$1}' grade.txt 分别以ascii码,整数,浮点数来表示$1
U 85 85.000000
W 87 87.000000
K 75 75.000000
K 75 75.000000
V 86 86.000000
a 97 97.000000
8 56 56.000000
L 76 76.000000
4 52 52.000000
A 65 65.000000
y 0 0.000000
[root@localhost ~]# awk 'BEGIN {print split("ask#as#k",array2,"#")}'
3
[root@localhost ~]# awk 'BEGIN {print split("ask#as#k",array2,"#");print array2[1],"\t",array2[2],"\t",array2[3]}'
3
ask as k
[root@localhost ~]# cat awk_array.sh
#!/bin/awk -f
#awk_array.sh
BEGIN{
FS="#"
score["0-60"]=0
score["60-70"]=0
score["70-80"]=0
score["80-90"]=0
score["90-100"]=0
student["junior"]=0
student["senior"]=0
}
{
{if ($1<60)
score["0-60"]++
}
{if ($1<70 && $1>=60)
score["60-70"]++
}
{if ($1<80 && $1>=70)
score["70-80"]++
}
{if ($1<90 && $1>=80)
score["80-90"]++
}
{if ($1<=100 && $1>=90)
score["90-100"]++
}
}
{
for (senior_junior in student)
{if ($2==senior_junior)
student[senior_junior]++
} $2的值是其中senior或junior之一,就让student senior或junior加一
}
END{
{for (number in score) print "The score",number,"has",score[number],"students"}
{for (student_junior in student) print "the class has",student[senior_junior],senior_junior,"students"}
}
[root@localhost ~]# cat grade.txt
85#senior
87#junior
75#senior
75#junior
86#junior
97#senior
56#junior
76#senior
52#junior
65#senior
you have new mail in /var/spool/mail/root
[root@localhost ~]# ./awk_array.sh grade.txt
The score 0-60 has 2 students
The score 70-80 has 3 students
The score 90-100 has 1 students
The score 60-70 has 1 students
The score 80-90 has 3 students
the class has 5 junior students
the class has 5 junior students
[root@localhost ~]# ./menu.sh 此脚本是创建一个交互式的菜单
DATE:2007-03-10 06:29:15
=======================================================
CREATE MINISITE IN CHINAITLAB.COM
=======================================================
** 1)ADD MINISITE ACCOUNT **
** 2)ADD DOMAIN IN CHINAITLAB.COM **
** 3)ADD DATABASE IN MYSQL **
** 4)ADD VIRTUAL HOST IN APACHE **
** 5)BACKUP MINISITE **
** 6)DELETE MINISITE **
** 7)EXIT **
=======================================================
-n Please choose [1-7]:
7
[root@localhost ~]# cat menu.sh
#!/bin/bash
loopvar=2
###############
# main menu #
###############
main_menu()
{
echo
echo
dis_mainmenu="CREATE MINISITE IN CHINAITLAB.COM"
curdate=`date "+%Y-%m-%d %T"`
cat <<mayday
DATE:$curdate
=======================================================
$dis_mainmenu
=======================================================
** 1)ADD MINISITE ACCOUNT **
** 2)ADD DOMAIN IN CHINAITLAB.COM **
** 3)ADD DATABASE IN MYSQL **
** 4)ADD VIRTUAL HOST IN APACHE **
** 5)BACKUP MINISITE **
** 6)DELETE MINISITE **
** 7)EXIT **
=======================================================
mayday
}
while [ $loopvar -gt 0 ]
do
main_menu
echo -n" Please choose [1-7]:"
read main_choice
case $main_choice in
7)
exit
;;
*)
clear
continue
;;
esac
done
九 shell的工具
* 日志文件
* 信号
* trap捕捉信号
* eval
* logger
[root@localhost ~]# cat datelog.sh
#!/bin/bash
#当前日期
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
#comman blocks
sleep 4
#输出日志到日志文件
log_time_format=`date "+%Y-%m-%d %T"`
echo "${log_time_format} 命令结束">>$todaylog
[root@localhost ~]# ./datelog.sh
[root@localhost ~]# cd log
[root@localhost log]# cat 20070310.log
2007-03-10 17:29:00 命令开始
2007-03-10 17:29:04 命令结束 创建以日期为标识的log