centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课 return用在函数中exit用在shell当中 直接退出整个脚本,整个子shell或当前shellbreak退出循环 上半节课 if 判断case判断shell脚本中的循环 下半节课 for whileshell中的函数breakcontinue 课程大纲(继续上节课的) 7. if
需要编写一个较庞大的脚本时,可能会涉及许多函数.变量.这是通常建议将众多的函数.变量放入一个单独的脚本内.这样做的好处很明显,不用担心某个函数.变量是否已经被定义和使用,也不用频繁地定义.清除函数和变量. 一.函数文件的编写 函数文件的格式和脚本文件一样. [root@localhost shell]# cat function.example #函数文件中也可以不写下面这行Shell调用语句 #!/bin/bash #function example. #This is a function
函数定义.调用 $ cat te.sh #!/bin/bash # define a function test() { echo "This is a function." } # test function call test $ sh te.sh 函数库文件 编写函数库文件 #!/bin/bash # define func hello() { echo -n "hello $1. " credit } credit() { echo "Do you
脚本 first (测试示例1) first#!/bin/bashecho 'your are in first file' 方法一:使用source #!/bin/bashecho 'your are in second file'source first 方法二:使用. #!/bin/bashecho 'your are in second file'. first 方法三:使用sh#!/bin/bashecho 'your are in second file'sh first
在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变量和函数的变量的作用域问题可能令你费解,在这里梳理一下这个问题. (1)Shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止. 例1:脚本变量的作用域#!/bin/bash#define the function ltx_funcltx_func(){ echo $v1 #modify the variable v1 v1=200 }#define the