shell函数学习
时间:2010-07-03 来源:liyongfeng410799624
函数学习过程
nihao.sh
#!/bin/bash
#nihao
function hello()
{
echo "input name"
read name
echo "my name is $name $1" return 100
}
hello chipi
调用函数文件 fun.sh #!/bin/bash
#fun
. nihao.sh 调用目录中nihao.sh脚本(固定格式: .+空格+文件名)
echo first
hello 调用目录中nihao.sh脚本中定义的hello函数
echo end
查看载入函数:set 删除函数:unset fun.sh #!/bin/bash
#fun
. nihao.sh
set
unset hello
echo first
hello
echo end
执行后提示: [root@vcom6002 root.adminssh]# ./fun.sh
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./fun.sh")
BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='3.00.15(1)-release'
CLASSPATH=/usr/local/jdk/lib:/usr/local/jdk/jre/lib
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=vcom6002
HOSTTYPE=i386
IFS=$' \t\n'
INPUTRC=/etc/inputrc
JAVA_HOME=/usr/local/jdk
KDEDIR=/usr
LANG=zh_CN.GBK
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/jdk/bin:/usr/local/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=5917
PS4='+ '
PWD=/home/root.adminssh
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
TERM=vt100
UID=0
USER=root
_=nihao.sh
hello ()
{
echo "input name";
read name;
echo "my name is $name $1";
return 100
} set的作用
first
./fun.sh: line 7: hello: command not found unset起作用了,找不到命令也找不到函数hello
end
[root@vcom6002 root.adminssh]# 函数返回状态值 使用符合 $? 表示函数返回值 fun.sh #!/bin/bash
#fun
. nihao.sh
echo first
hello
echo $?
echo end 执行结果: [root@vcom6002 root.adminssh]# ./fun.sh
first
input name
liyongfeng
my name is liyongfeng
100
end
[root@vcom6002 root.adminssh]#
#nihao
function hello()
{
echo "input name"
read name
echo "my name is $name $1" return 100
}
hello chipi
调用函数文件 fun.sh #!/bin/bash
#fun
. nihao.sh 调用目录中nihao.sh脚本(固定格式: .+空格+文件名)
echo first
hello 调用目录中nihao.sh脚本中定义的hello函数
echo end
查看载入函数:set 删除函数:unset fun.sh #!/bin/bash
#fun
. nihao.sh
set
unset hello
echo first
hello
echo end
执行后提示: [root@vcom6002 root.adminssh]# ./fun.sh
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./fun.sh")
BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='3.00.15(1)-release'
CLASSPATH=/usr/local/jdk/lib:/usr/local/jdk/jre/lib
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTSIZE=1000
HOME=/root
HOSTNAME=vcom6002
HOSTTYPE=i386
IFS=$' \t\n'
INPUTRC=/etc/inputrc
JAVA_HOME=/usr/local/jdk
KDEDIR=/usr
LANG=zh_CN.GBK
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/jdk/bin:/usr/local/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=5917
PS4='+ '
PWD=/home/root.adminssh
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
TERM=vt100
UID=0
USER=root
_=nihao.sh
hello ()
{
echo "input name";
read name;
echo "my name is $name $1";
return 100
} set的作用
first
./fun.sh: line 7: hello: command not found unset起作用了,找不到命令也找不到函数hello
end
[root@vcom6002 root.adminssh]# 函数返回状态值 使用符合 $? 表示函数返回值 fun.sh #!/bin/bash
#fun
. nihao.sh
echo first
hello
echo $?
echo end 执行结果: [root@vcom6002 root.adminssh]# ./fun.sh
first
input name
liyongfeng
my name is liyongfeng
100
end
[root@vcom6002 root.adminssh]#
相关阅读 更多 +