table of Contents
shell as a lightweight scripting language, can be user-defined function, and then call in a shell script, and similar classes and methods in Java. Today to talk about those things the shell function.
Definition Syntax shell functions as follows:
[function] funname [()]
{
Action;
[return int;]
}
Description:
1, can function with fun () is defined, may be directly fun () is defined, without any parameters.
2, return parameter can be displayed add: return returned, if not, will last a result of that command, as a return value. followed by the return value of n (0-255), can only return value, if not an error value will
Passing parameters
cat funcTest.sh
#!/bin/bash
parameters1=$1 #全局变量
parameters2=$2 #全局变量
function testFun() { #定义函数testFun
local cc="this is cc" #定义局部变量$cc
local dd="this is dd" #定义局部变量$dd
echo $parameters1, $parameters2 #访问参数1和参数2
echo $cc #打印局部变量
return 0 #shell函数返回值是整形,并且在0~257之间。
}
echo $dd #这里将会打印不生效,因为dd是局部变量。
testFun #使用函数testFun
sh funcTest.sh canshu1 canshu2
canshu1, canshu2
this is cc
parameters1 and parameters2 defined global variables.
cc and dd are local variables defined only in the function name.
$ parameters1 is the first parameter $ 1, $ parameters2 $ 2 is the first parameter, and so is the n $ n $ n parameter.
return 0 return parameter can be displayed add: return returned, if not, the result will be the last command run as a return value.
recursive function
bash also supports recursive functions (functions able to call itself) such as:
cat digui.sh
#!/bin/bash
function name() {
echo $1
name hello
sleep 1
}
name
After running the script continues to print out hello, ending press Ctrl + C.
A script, another script function
Create a file and output the contents to a file touchAndechoFiles .sh
cat touchAndechoFiles.sh
#!/bin/bash
dir_path=$2
start_day=$3
end_day=$4
#yesterday=`date -d -1day +%Y%m%d`
touchFiles() {
#dir_path=$1
#start_day=$2
#end_day=$3
while :
do
local start_day=$(date -d "$start_day 1day" +%Y%m%d)
local statis_day=`date -d "${start_day} -1 day" +%Y%m%d`
touch ${dir_path}/${statis_day}.csv
if [ $? -eq 0 ]; then #此处$?是接收上条命令是否成功,成为为0,失败为非0
return 0
else
return 10
fi
echo start_day=$start_day
echo statis_day=${statis_day}
echo end_day=$end_day
if [ "${statis_day}" = "${end_day}" ]; then
break;
fi
done
}
echoFiles() {
#dir_path=$1
#start_day=$2
#end_day=$3
while :
do
local start_day=$(date -d "$start_day 1day" +%Y%m%d)
local statis_day=`date -d "$start_day -1 day" +%Y%m%d`
echo ${statis_day} >> ${dir_path}/${statis_day}.csv
if [ $? -eq 0 ]; then
return 0
else
return 10
fi
echo start_day=$start_day
echo statis_day=${statis_day}
echo end_day=$end_day
if [ "${statis_day}" = "${end_day}" ]; then
break;
fi
done
}
$1 $2 $3 $4
cat checkFiles1.sh
#!/bin/bash
dir_path=$1
start_day=$2
end_day=$3
files=$4
#yesterday=`date -d -1day +%Y%m%d`
checkFiles() {
file_name=`ls $dir_path | grep "${files}"`
if [ -z file_name ]; then
echo $dir_path目录下没有您输入日期相对应的文件
else
echo $dir_path目录下包含您输入日期的文件有:$file_name
fi
}
echo dir_path=$dir_path
echo start_day=$start_day
echo end_day=$end_day
echo files=$files
sh /home/oru/liuzhai/shell/test/touchAndechoFiles.sh touchFiles $dir_path $start_day $end_day
if [ $? -eq 0 ]; then #此处$?是接收上条命令调用函数的返回值
sh /home/oru/liuzhai/shell/test/touchAndechoFiles.sh echoFiles $dir_path $start_day $end_day
if [ $? -eq 0 ]; then
checkFiles
else
echo 向文件输入内容失败!!!
fi
else
echo 创建文件失败!!!
fi
Note: Because the call checkFiles1.sh script in a function touchAndechoFiles.sh, and was mass participation, so the last touchAndechoFiles.sh script parameters need to be passed in writing, $ 1 is reserved for the function name, if touchAndechoFiles .sh script is not written in the last row of the need to pass parameters, the script can not pass parameters if no parameter of the function name, the passed parameter will offset (to find this bug for a long time only to find).