第一部分 Shell基础编程——第六章 Shell函数

笔记

#Shell函数

#函数定义
[root@localhost 0416]# vi hellofun
[root@localhost 0416]# cat hellofun
#!/bin/bash
#hellofun
function hello()
{
        echo "Hello,today is `date`"
        return 1
}
#无意义,不能执行
[root@localhost 0416]# chmod 755 hellofun

#函数调用
#拷贝文件
[root@localhost 0416]# cp hellofun func
#编辑
[root@localhost 0416]# vi func
#查看
[root@localhost 0416]# cat func
#!/bin/bash
#func
function hello()
{
        echo "Hello,today is `date`"
}
echo "now going to the function hello"
hello
echo "back from the function"
#执行
[root@localhost 0416]# ./func  
now going to the function hello
Hello,today is Tue Apr 16 15:30:43 CST 2013
back from the function

#参数传递
#编辑
[root@localhost 0416]# vi func2
#查看
[root@localhost 0416]# cat func2
#!/bin/bash
#func2
function hello()
{
		#类似于位置参数
        echo "Hello,$1 today is `date`"
}
echo "now going to the function hello"
hello chinaitlab
echo "back from the function"
#执行
[root@localhost 0416]# ./func2
now going to the function hello
Hello,chinaitlab today is Tue Apr 16 15:34:21 CST 2013
back from the function


#函数文件
#编辑
[root@localhost 0416]# vi func3
#查看
[root@localhost 0416]# cat func3
#!/bin/bash
#func3
#Source function
#可以包含其他文件或者其他函数
. hellofun
echo "now going to the function hello"
hello
echo "back from the function"
#执行
[root@localhost 0416]# ./func3
now going to the function hello
Hello,today is Tue Apr 16 15:43:31 CST 2013
back from the function
#启动文件,学习Shell看启动文件,修改前注意备份
#查看network文件
[root@localhost root]# cat /etc/rc.d/init.d/network
#查看functions文件
[root@localhost root]# cat /etc/rc.d/init.d/ functions

#检查载入函数和删除函数
#拷贝文件
[root@localhost 0416]# cp func3 func4
#拷贝文件
[root@localhost 0416]# cp func func4
#查看文件
[root@localhost 0416]# cat func4
#!/bin/bash
#func4
#Source function
. hellofun
set
echo "now going to the function hello"
hello
echo "back from the function"
#执行
[root@localhost 0416]# ./func4
BASH=/bin/bash
BASH_ENV=/root/.bashrc
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
CLASSPATH=.:/usr/java/jdk1.6.0_27/lib/dt.jar:/usr/java/jdk1.6.0_27/lib/tools.jar:/usr/java/jdk1.6.0_27/jre/lib/ext/mysql-connector-java-3.1.10-bin.jar
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=i386
IFS=$' \t\n'
INPUTRC=/etc/inputrc
JAVA_HOME=/usr/java/jdk1.6.0_27
LAMHELPFILE=/etc/lam/lam-helpfile
LANG=en_US.en
LANGUAGE=zh_CN.GB18030:zh_CN.GB2312:zh_CN
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/java/jdk1.6.0_27/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=3541
PS4='+ '
PVM_ROOT=/usr/share/pvm3
PVM_RSH=/usr/bin/rsh
PWD=/home/wgb/shell/0416
QTDIR=/usr/lib/qt-3.1
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='192.168.223.1 2757 22'
SSH_CONNECTION='192.168.223.1 2757 192.168.223.100 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=0
USER=root
USERNAME=root
XPVM_ROOT=/usr/share/pvm3/xpvm
_=hellofun
hello () 
{ 
    echo "Hello,today is `date`";
    return 1
}
now going to the function hello
Hello,today is Tue Apr 16 17:54:50 CST 2013
back from the function

#查看文件
[root@localhost 0416]# cat func5
#!/bin/bash
#func5
#Source function
. hellofun
set
unset hello
echo "now going to the function hello"
hello
echo "back from the function"
#执行
[root@localhost 0416]# ./func5
BASH=/bin/bash
BASH_ENV=/root/.bashrc
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
CLASSPATH=.:/usr/java/jdk1.6.0_27/lib/dt.jar:/usr/java/jdk1.6.0_27/lib/tools.jar:/usr/java/jdk1.6.0_27/jre/lib/ext/mysql-connector-java-3.1.10-bin.jar
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=i386
IFS=$' \t\n'
INPUTRC=/etc/inputrc
JAVA_HOME=/usr/java/jdk1.6.0_27
LAMHELPFILE=/etc/lam/lam-helpfile
LANG=en_US.en
LANGUAGE=zh_CN.GB18030:zh_CN.GB2312:zh_CN
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/java/jdk1.6.0_27/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=3541
PS4='+ '
PVM_ROOT=/usr/share/pvm3
PVM_RSH=/usr/bin/rsh
PWD=/home/wgb/shell/0416
QTDIR=/usr/lib/qt-3.1
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='192.168.223.1 2757 22'
SSH_CONNECTION='192.168.223.1 2757 192.168.223.100 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=0
USER=root
USERNAME=root
XPVM_ROOT=/usr/share/pvm3/xpvm
_=hellofun
hello () 
{ 
    echo "Hello,today is `date`";
    return 1
}
now going to the function hello
./func5: line 8: hello: command not found
back from the function
#查看文件
[root@localhost 0416]# cat func5
#!/bin/bash
#func5
#Source function
. hellofun
#set
unset hello
echo "now going to the function hello"
hello
echo "back from the function"
#执行
[root@localhost 0416]# ./func5
now going to the function hello
./func5: line 8: hello: command not found
back from the function


#函数返回状态值
#拷贝文件
[root@localhost 0416]# cp hellofun hellofunctions
#查看文件
[root@localhost 0416]# cat hellofunctions 
#!/bin/bash
#hellofunctions
function hello()
{
        echo "Hello,today is `date`"
        return 1
}
#拷贝文件
[root@localhost 0416]# cp func func6
#查看文件
[root@localhost 0416]# cat func6
#!/bin/bash
#func6
. hellofunctions
echo "now going to the function hello"
hello
echo $?
echo "back from the function"
#返回0,正确执行,返回1,一般发生错误——约定
#执行
[root@localhost 0416]# ./func6
now going to the function hello
Hello,today is Tue Apr 16 18:02:56 CST 2013
1
back from the function
#查看文件
[root@localhost 0416]# cat hellofunctions 
#!/bin/bash
#hellofunctions
function hello()
{
        echo "Hello,today is `date`"
        return 0
}
#执行
[root@localhost 0416]# ./func6
now going to the function hello
Hello,today is Tue Apr 16 18:02:56 CST 2013
0
back from the function
#查看文件
[root@localhost 0416]# cat func6
#!/bin/bash
#func6
. hellofunctions
echo "now going to the function hello"
returnvalue=hello
echo $?
#这样的赋值是不允许的
echo $returnvalue
echo "back from the function"
[root@localhost 0416]# ./func6
now going to the function hello
0
hello
back from the function
#编辑文件
[root@localhost 0416]# vi hellofunctions
#查看文件
[root@localhost 0416]# cat hellofunctions 
#!/bin/bash
#hellofunctions
function hello()
{
        echo "Hello,today is `date`"
        return 127
}
#执行文件,根据文件返回值就可以做一些操作
[root@localhost 0416]# ./func6
now going to the function hello
127
back from the function


 

附图

第一部分 Shell基础编程——第六章 Shell函数_第1张图片

 

第一部分 Shell基础编程——第六章 Shell函数_第2张图片

 

 

第一部分 Shell基础编程——第六章 Shell函数_第3张图片 第一部分 Shell基础编程——第六章 Shell函数_第4张图片  第一部分 Shell基础编程——第六章 Shell函数_第5张图片
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]

你可能感兴趣的:(shell函数,第六章,第一部分,Shell基础编程)