函数
•shell中,只有环境变量是全局的
•shell脚本中,定义在方法外的就是全局变量
•shell 中在方法中定义的变量默认也是全局变量
•local一般用于局部变量声明,都在函数内部使用
(1)shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止。
(2)shell函数定义的变量默认是global的,其作用域从“函数被调用时执行变量定义的地方”开始,到shell结束或被显示删除处为止。
函数定义的变量可以被显示定义成local的,其作用域局限于函数内。但请注意,函数的参数是local的。
(3)如果同名,Shell函数定义的local变量会屏蔽脚本定义的global变量。
•用户也可以根据自己的需要定义自己的环境变量(全局变量),定义环境变量的方法:
export variable_name=value
#or
variable_name=value
export variable_name
【1】普通用户的环境变量只存在普通用户下,别的用户默认情况下是无法,调到另一个用户所定义的环境变量
★自定义环境变量
1,vim /home/user01/.bash_profile
profiles='/etc/profile'
str_user_user01_dir="/home/user01/"
str_sever_cmd="/home/user01/tomcat/bin/startup.sh"
2,使生效:source /home/user01/.bash_profile
3,root用户切换到user01用户默认是取不到user01用户定意的环境变量。
想要也能获取当变量值,可以再执行source /home/user01/.bash_profile就可以获取当变量存的字符串。
[user01@localhost root]$ source /home/user01/.bash_profile
【2】超级用户的环境变量,超全局,跨用户,不管是切换用户还是登到普通用户,定义的环境变量,永远都是能获取到的。
★自定义 环境变量
1,vim /etc/profile.d/install.jdk.sh
num=1111
2,使生效:. /etc/profile.d/install.jdk.sh
或:source /etc/profile
[root@localhost ~]# echo $num
1111
【3】自定义全局变量其实就是环境变量,也可以管它叫超全局变量,跨用户,跨脚本。
1,自定义全局变量与跨脚本后全局变量
vim /etc/profile.d/install.test.sh
profiles='/etc/profile'
str_user_user01_dir="/home/user01/"
[root@centos]# . /etc/profile.d/install.test.sh
[root@centos]# echo $str_user_user01_dir
/home/user01/
2,在脚本中加入如下语句,使之生效。后续脚本即可使用其定义的变量。
放在函数中也是可以的。
. /etc/profile.d/install.test.sh
source /etc/profile
例:测试跨脚本后全局变量
目录
. .代表前目录
. bash 代表当前目录下,bash文件夹
vim a.sh
. /etc/profile.d/install.test.sh
source /etc/profile
echo ${str_user_user01_dir}
echo 'a.sh'
vim b.sh
echo ${str_user_user01_dir}
echo 'b.sh'
vim menu.sh
#!/bin/bash
#或(也可放入函数中)
#. /etc/profile.d/install.jdk.sh
#source /etc/profile
. ./a.sh
. ./b.sh
执行:sh menu.sh
显示:
[root@localhost shell]# sh menu.sh
/home/user01/
a.sh
/home/user01/
b.sh
●传递参数
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";
./test.sh 1 2 3
bash test.sh 1 2 3
执行的文件名:test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3
$# 传递到脚本的参数个数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误
$* 以"$1 $2 … $n"的形式输出所有参数。
$@ 以"$1" "$2" … "$n" 的形式输出所有参数
●Shell中创建自定义函数。格式如下:
function_name () { commands; commands; }
function function_name { commands; commands; }
function function_name () { commands; commands; }
函数的参数在函数内是以$[0-9]、${10}...,这种局部变量的方式来访问的。
●函数的左花括号和命令之间必须有至少一个空格。每个命令的后面都要有一个分号,即便是最后一个命令
/> function greet { echo "Hello $LOGNAME, today is $(date)"; }
#此时函数已经驻留在当前的bash shell中,因此使用函数效率更高。
/> greet
Hello root, today is Fri Nov 18 20:45:10 CST 2011
/> greet() { echo "Hello $LOGNAME, today is $(date)"; }
/> greet
Hello root, today is Fri Nov 18 20:46:40 CST 2011
#welcome函数内部使用了函数参数。
/> function welcome { echo "Hi $1 and $2"; }
/> welcome stephen jane
Hi stephen and jane
●declare -F选项将列出当前Shell中驻留的函数
/> declare -F
declare -f greet
declare -f welcome
●清空指定的函数,使其不在Shell中驻留。
/> unset -f welcome
●函数说明:
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。
return后跟数值n(0-255).
3、函数返回值在调用该函数后通过 $? 来获得
4、所有函数在使用前必须定义
5、$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

浙公网安备 33010602011771号