shell学习笔记2

四 正则表达式:
   基本元字符集的含义
   /                      屏蔽特殊元字符,使其失去特殊含义
   pattern/{n/}             匹配pattern出现的次数
   pattern/{n,/}            匹配pattern出现的次数,但最少是n次
   pattern/{n,m/}         匹配pattern出现的次数,但必须界于n和m之间
sed命令:
   sed在文本中查询文本的方式:
   ----使用行号,可以是一行,也可以是多行
   ----使用正则表达式
   x表示一行
   x,y表示多行
   /partern/ 匹配包含partern的行
   /partern/partern/ 匹配包含两个partern的行
partern/,x 在x行上查询包含partern的行
   x,/partern/通过行号和模式来查询行
   x,y!查询不包含指定行号x和y的行
   sed的基本命令:
   p打印匹配的行
   =显示文件的行号
   a/在定位行号后附加新文本信息
   i/在定位行号后插入新的文本信息
   d删除定位行
   c/用新文本替换定位的的文本
   s使用替换模式替换相应的模式
   从另一个文件中读文本
   写到一个文件
   q第一个模式匹配完后立即退出
   l显示与八进制ascii码等价的控制字符
   {}在定位行执行的命令组
   n从另一个文件中读文本下一行,并附加在下一行
   g将模式2粘贴到/partern n/
   y传送字符
五.函数
   Shell 允许将一组命令集或语句形成一个可用块,这些块称为 shell 函数
   定义函数的格式为:
   - 函数名 ()
   {
   命令 1
   …
   }
-function 函数名 ()
   {
   …
   }
   [root@localhost ~]# ./func
now going to the function hello
Hello,today is Wed Mar 7 04:00:57 CST 2007
back from the function
[root@localhost ~]# cat func
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,today is `date`"
return 1
}
echo "now going to the function hello"
hello
echo "back from the function"
[root@localhost ~]# ./func2
now going to the function hello
Hello,chinaitlab today is Wed Mar 7 04:18:57 CST 2007
back from the function
[root@localhost ~]# cat func2
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,$1 today is `date`"
return 1
}
echo "now going to the function hello"
hello chinaitlab
echo "back from the function"
调用函数文件
[root@localhost ~]# ./func3
now going to the function hello
Hello,today is Wed Mar 7 04:28:05 CST 2007
back from the function
[root@localhost ~]# cat hellofun
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,today is `date`"
return 1
}
[root@localhost ~]# cat func3
. hellofun
echo "now going to the function hello"
hello
echo "back from the function"
[root@localhost ~]# more /etc/init.d/network
#! /bin/bash
#
# network        Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to /
#               start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
### END INIT INFO
 
# Source function library.
. /etc/init.d/functions
此文件的开头部分就调用了一个函数“ . /etc/init.d/functions
关于函数调用的情况的检查:
[root@localhost ~]# ./func3
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./func3")
BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='3.00.15(1)-release'
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=i386
IFS=$' /t/n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LOGNAME=root
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/root
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=2691
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
TERM=vt100
UID=0
USER=root
_=hellofun
hello ()
{
    echo "Hello,today is `date`";
    return 1
}
now going to the function hello
Hello,today is Wed Mar 7 04:35:44 CST 2007
back from the function
[root@localhost ~]# cat func3
. hellofun
set
echo "now going to the function hello"
hello
echo "back from the function"
上文中的黑色字体部分显示了定义的函数
删除函数
[root@localhost ~]# ./func3
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./func3")
BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='3.00.15(1)-release'
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=i386
IFS=$' /t/n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LOGNAME=root
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/root
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=2691
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
TERM=vt100
UID=0
USER=root
_=hellofun
hello ()
{
    echo "Hello,today is `date`";
    return 1
}
now going to the function hello
./func3: line 5: hello: command not found
back from the function
[root@localhost ~]# cat func3
. hellofun
set
unset hello
echo "now going to the function hello"
hello
echo "back from the function"
函数返回值
[root@localhost ~]# ./func3
now going to the function hello
Hello,today is Wed Mar 7 04:45:51 CST 2007
0
back from the function
[root@localhost ~]# cat hellofun
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,today is `date`"
return 0  # 定义函数的返回值,调用成功显示 0
}
[root@localhost ~]# cat func
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,today is `date`"
return 1
}
echo "now going to the function hello"
hello
echo "back from the function"
[root@localhost ~]# cat func3
. hellofun
echo "now going to the function hello"
hello
echo $? # 通过调用 $? 变量来调用函数的返回值
echo "back from the function"
shift 命令
   shift n
   此命令每执行一次将参数的位置向左移动 n
[root@localhost ~]# vi opt2
#!/bin/bash
usage ()
{
echo "usage:`basename $0` filenames"
}
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}"
~
~
~
~
~
~
"opt2" 20L, 265C written
[root@localhost ~]# ./opt2 linshi.sh func
linshi.sh : 4
func : 10
----------
total: 14
[root@localhost ~]# cat func
#!/bin/bash
#hellofun
function hello ()
{
echo "Hello,today is `date`"
return 1
}
echo "now going to the function hello"
hello
echo "back from the function"
gettops
   此命令获得多个命令行的参数
   [root@localhost ~]# cat optgets
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahvc OPTION     getopts命令把ahvc的四个字母的值依次赋值给OPTION变量
do
        case $OPTION in
        a)
                ALL=true
                echo "ALL IS $ALL"
                ;;
        h)
                HELP=true
                echo "HELP is $HELP"
                ;;
        v)
                VERBOSE=TRUE
                echo "VERBOSE IS $VERBOSE"
                ;;
        c)
                echo "c value is $c"
                ;;
        /?)
                echo "`basename $0` -[a h f v] -[c value] file "
        esac
done
 
[root@localhost ~]# ./optgets -f -a -c
./optgets: illegal option -- f
optgets -[a h f v] -[c value] file
ALL IS true
c value is
[root@localhost ~]# cat optgets2
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahvc: OPTION
do
        case $OPTION in
        a)
                ALL=true
                echo "ALL IS $ALL"
                ;;
        h)
                HELP=true
                echo "HELP is $HELP"
                ;;
        v)
                VERBOSE=TRUE
                echo "VERBOSE IS $VERBOSE"
                ;;
        c)
                c=$OPTARG
                echo "c value is $c"
                ;;
        /?)
                echo "`basename $0` -[a h f v] -[c value] file "
        esac
done
 
[root@localhost ~]# ./optgets2 -f -a -c ff
./optgets2: illegal option -- f
optgets2 -[a h f v] -[c value] file
ALL IS true
c value is ff
 

你可能感兴趣的:(shell学习笔记2)