Linux shell4
来源:互联网 发布:小米手机3能用4g网络吗 编辑:程序博客网 时间:2024/06/15 00:28
第六章 shell函数
6.1
shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数
定义函数的格式为:
函数名()
{
命令1
...
}
或者
function 函数名()
{
...
}
函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中
#!/bin/bash
#hellofunction
function hello()
{
echo "Hello,today is `date`"
return 1
}
#!/bin/bash
#hello function test
function hello()
{
echo "Hello,today is `date`"
return 1
}
echo "now going to the function hello"
hello
echo "back from the function hello"
6.2 参数传递
向函数传递参数就像在脚本中使用位置变量$1,$2...$9
#!/bin/bahs
#function 参数
function hello()
{
echo "Hello,$1 today is `date`"
}
echo "now going to the function hellp"
hello chinaUxix
echo "back from the function"
6.3函数文件
#!/bin/bash
#function
#source function
. hellofun.sh #载入函数文件 .后一定要有空格
echo "now going to the function hello"
hello #执行hellofun.sh 中的函数hello
echo "back from the function"
hellofun.sh
#!/bin/bash
#hello function
function hello()
{
"Hello,today is `date`"
return 1
}
系统中函数文件的路径
more /etc/init.d/functions
6.4 检查载入函数和删除函数
察看载入函数 set
删除函数 unset
#!/bin/bash
#source function
. hellofun.sh #载入函数文件
set|grep hello
#set
echo "now going to the function"
unset hello
hello
echo "back from the function"
6.5 函数返回状态
返回的是状态值
#!/bin/bash
#hello function ruturn
. hellofun.sh #载入函数文件
echo "now going to the function"
hello
echo $? #状态值
echo "back from the function"
第7章 脚本参数传递
shift n 命令
getopts
7.1 shift
每次将参数位置向左偏移n位
#!/bin/bash
#shift test totle file lines 统计文件的行数
function usage()
{
echo "usage:`basename $0` filenemes"
}
totalline=0
if [ $# -lt 2 ];then
usage
exit #退出
fi
while [ $# -ne 0 ]
do
line = `cat $1 | wc -l`
echo "$1:${line}"
totalline=${$totalline+$line}
shift
done
echo "----------"
echo "total:${totalline}"
7.2 getopts
获得多个命令行参数
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfvc: OPTION
do
case $OPTION in
a)
ALL=true
echo "ALL is $ALL"
;;
h)
HELP=true
echo "HELP IS $HELP"
;;
f)
FILE=true;
echo "FILE IS $FILE"
;;
v)
VERBOSE=true
echo "VERBOSE IS $VERBOSE"
;;
c)
c=$OPTARG
echo "c is $c"
;;
?)
echo "`basename $0` -[a h f v] -[c value] file"
;;
esac
done
第八章 深入讨论AWK 深入讨论<<
1.1 深入讨论AWK
记录和域,模式和动作,正则表达式和元字符
条件操作符
awk内置变量
NF,NR和FILENAME
awk操作符
内置的字符串函数
字符串屏蔽序列
awk输出函数printf
awk数组
条件操作符
操作符 描述
< 小于
>= 大于等于
<= 小于等于
== 等于
!= 不等于
~ 匹配正则表达式
!~ 不匹配正则表达式
逻辑操作符
操作符 描述
&& and
|| or
! not
#!/bin/bash
#awkif
echo "210-219网段的访问量是:`awk '{if ($1~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "非210-219网段的访问量是:`awk '{if ($1!~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "2004年07月07日的访问量是:`awk '{if ($4~/^\[07\/Jul\/2004/) print $0}' www.log|wc -l`"
echo "2004年07月07日/htm/free_call.php的访问量是:`awk '{if ($4~/^\[07\/Jul\/2004/) print $0}'
www.log |awk '{if ($7=="/htm/free_call.php") print $0}'|wc -l`"
awk 内置变量
ARGC 命令行参数个数
ARGV 命令行参数排列
ENVIRON 支持队列中系统环境变量的使用
FILENAME awk浏览的文件名
FNR 浏览文件的记录数
FS 设置输入域分隔符,等价于命令行-F选项
NF 浏览记录的域个数
NR 已读的记录数
OFS 输出域分隔符
ORS 输出记录分隔符
RS 控制记录分隔符
awk -F '#' '{print NF,NR,ENVIRON["USER"],$0,FILENAME,ARGC,ARGV[0],ARGV[1]}' grade.txt
字符串函数
gsub(r,s) 在整个$0中用s替代r
gsub(r,s,t) 在整个t中用s替代r
index(s,t) 返回字符串s中字符串t的第一个位置
length(s) 返回s长度
match(s,r) 测试s是否包含匹配r的字符串
split(s,a,fs) 在fs上将s分成序列a
sprint(fmt,exp) 返回经fmt格式化后的exp
sub(r,s) 用$0中最左边最长的子串代替s
substr(s,p) 返回字符串s中从p开始的后部分
substr(s,p,n) 返回字符串s中从p开始长度为n的后部分
awk -F '#' '{if (gsub("#","||")) print $0}' grade.txt
awk -F '#' '{if (gsub("s","S",$2)) print $2}' grade.txt
awk -F '#' '{print (index($2,"u"))}' grade.txt
转义字符
\b 退格键
\t tab键
\f 走纸换页
\ddd 八进制值
\n 新行
\c 任意其他特殊字符,例如\\为反斜线符号
\r 回车键
awk -F '#' '{print (index($2,"u")),"\t",$2}' grade.txt
printf修饰符
%c ASCII字符
%d 整数
%f 浮动数,例如(123.44)
%e 浮点数,科学计数法
%f 新行
%g awk决定使用哪种浮点数转换e或者f
%o 八进制数
%s 字符串
%x 十六进制
awk -F '#' '{printf "%c\n",$1}' grade.txt
awk -F '#' '{printf "%c\t%d\n",$1,$1}' grade.txt
awk -F '#' '{printf "%c\t%f\t%d\n",$1,$1,$1}' grade.txt
awk数组,下标从1开始
awk 'BEGIN {print split("as#qw#1234",array2,"#")}'
awk 'BEGIN {split("as#qw#1234",array2,"#"); print array2[1]}'
awk 'BEGIN {split("as#qw#1234",array2,"#"); print array2[1],"\t",array2[2],"\t",array2[3]}'
#!/bin/awk -f
#awk array
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]++
}
}
END{
{for(number in score) print "The score ",number,"has",score[number],"student"}
{for(senior_junior in student) print "The class has ",student[senior_junior],senior_junior,"student"}
}
#!/bin/bash
loopvar=2
##############
# main menu #
##############
main_menu()
{
echo
echo
dis_mainmenu="CREATE MINISITE IN ROOT.COM"
curdate=`date "+%Y-%m-%d %T"`
cat <<mayday
DATE: $curdate
===============================================
$dis_mainmenu
===============================================
** 1)ADD MINISITE ACCOUNT **
** 2)ADD DOMAIN IN ROOT.COM **
** 3)ADD DATEBASE IN MYSQL **
** 4)ADD VIRTUAL HOST IN APPCHE **
** 5)BACK MINISITE **
** 6)DELETE MINISITE **
** 7)EXIT **
===============================================
mayday
}
while [ $loopvar -gt 0 ]
do
clear
main_menu
echo -n " Please choose [1-7]:"
read main_choice
case $main_choice in
7)
exit
;;
*)
continue
;;
esac
done
- Linux shell4
- linux课堂笔记--shell4正则表达式
- linux
- linux
- Linux
- Linux
- LINUX!
- Linux
- linux
- linux
- linux
- Linux
- linux
- linux
- linux
- linux
- Linux
- linux
- 排序 : 2 插入排序
- Mapreduce Strips算法 实现
- JavaSE学习 第十六章 线程
- 【专题】CSDN下载频道【2月热门资源TOP100】汇总
- this kernel requires an x86-64 cpu,but only detected an i686 cpu
- Linux shell4
- 使用strace+pstack利器分析程序性能
- 整数划分(各种类型)nyist 571
- Objective-C语法之KVO的使用
- DataList分页的实现
- Linux shell5
- UVA 12384
- IE的有条件注释详解(附实例代码)
- android 背光控制 HAL层分析