1.退出状态码
#!/bin/bash
#using the return command in a function
function db1 {
read -p "Enter a value: " value
echo "doubleing the value"
return $[ $value * 2 ]
}
db1
echo "the new value is $?"
//
hadoop@master:~/shell_test/func$ ./return
Enter a value: 200
doubleing the value
the new value is 144
#注意
退出状态码只能是0-255
如果不用return 正确会返回0,
错误返回错误状态码
2,函数输出
#!/bin/bash
#using echo to return a value
function db1 {
read -p "Enter a value " value
echo $[ $value * 2 ]
}
result=`db1`
echo "the return value is $result"
hadoop@master:~/shell_test/func$ ./test1
Enter a value 3
the return value is 6
3.向函数传递参数
#!/bin/bash
#passing parameters to a function
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 to 15: "
value=`addem 10 15`
echo $value
echo -n "Let,s trying adding no numbers:"
value=`addem 10`
echo $value
hadoop@master:~/shell_test/func$ ./test2
Adding 10 to 15: 25
Let,s trying adding no numbers:20
注意:
不能直接用命令行传递进来的值。
4.在函数中处理变量
#!/bin/bash
#using a global varible to pass value
function db1 {
value=$[ $value * 2 ]
}
read -p "Enter a value :" value
db1
echo "the new value is : $value"
//
hadoop@master:~/shell_test/func$ ./test3
Enter a value :4
the new value is : 8
5.局部变量 local
#!/bin/bash
#using a local paramer
function func1 {
local temp=$[ $value * 2 ]
result=$[ $temp *2 ]
}
temp=4
value=6
func1
echo "the result is $result"
hadoop@master:~/shell_test/func$ ./test4
the result is 24
6.函数中传递数组
如果试图将改数组当成一个函数的参数,函数只会取得数组变量的第一个值。
因此需要先将数组分解成单个的值,然后再做函数的参数使用,在函数内部,我们需要重组数组
#!/bin/bash
#array variable to function test
function test {
local newarray
newarray=(`echo "$@"`)
echo "the new array value is : ${newarray[*]}"
}
myarray=(1,2,3,4)
echo "the original array value is ${myarray[*]}"
test ${myarray[*]}
hadoop@master:~/shell_test/func$ ./test5
the original array value is 1,2,3,4
the new array value is : 1,2,3,4
7.数组的使用
#!/bin/bash
#adding value in an array
function addarry {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "the original array is ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=`addarry $arg1`
echo "the result is $result"
hadoop@master:~/shell_test/func$ ./arraytest
the original array is 1 2 3 4 5
the result is 15
8.函数递归
#!/bin/bash
#adding value in an array
function addarry {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "the original array is ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=`addarry $arg1`
echo "the result is $result"
hadoop@master:~/shell_test/func$ ./factorial
Enter a value 1
the result of 1 is: 1
hadoop@master:~/shell_test/func$ ./factorial
Enter a value 3
the result of 3 is: 6
9.创建库
. 路径
#!/bin/bash
#my script functions
function addem {
echo $[ $1 + $2 ]
}
function multem {
echo $[ $1 * $2 ]
}
function divem {
if [ $2 -ne 0 ]
then
echo $[ $1 / 2 ]
else
echo -1
fi
}
value1=10
value2=20
result1=`addem $value1 $value2`
result2=`multem $value1 $value2`
result3=`divem $value1 $value2`
echo "the result 1 is $result1"
echo "the reslut 2 is $result2"
echo "the reslut 3 is $result3"
hadoop@master:~/shell_test/func$ ./useFun
the result 1 is 30
the reslut 2 is 200
the reslut 3 is 5
10. .bashrc 文件中定义函数
这样我们就可以在任何地方使用
或者
直接 . /hadoop/myfuncs 使shell生效
文件比较符
文件比较运算符
-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]
-d filename 如果 filename为目录,则为真 [ -d /tmp/mydir ]
-f filename 如果 filename为常规文件,则为真 [ -f /usr/bin/grep ]
-L filename 如果 filename为符号链接,则为真 [ -L /usr/bin/grep ]
-r filename 如果 filename可读,则为真 [ -r /var/log/syslog ]
-w filename 如果 filename可写,则为真 [ -w /var/mytmp.txt ]
-x filename 如果 filename可执行,则为真
[ -L /usr/bin/grep ]
filename1-nt filename2 如果 filename1比 filename2新,则为真
[ /tmp/install/etc/services -nt /etc/services ]
filename1-ot filename2 如果 filename1比 filename2旧,则为真
[ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string 如果 string长度为零,则为真 [ -z "$myvar" ]
-n string 如果 string长度非零,则为真 [ -n "$myvar" ]
string1= string2 如果 string1与 string2相同,则为真
[ "$myvar" = "one two three" ]
string1!= string2 如果 string1与 string2不同,则为真
[ "$myvar" != "one two three" ]
算术比较运算符
num1-eq num2 等于 [ 3 -eq $mynum ]
num1-ne num2 不等于 [ 3 -ne $mynum ]
num1-lt num2 小于 [ 3 -lt $mynum ]
num1-le num2 小于或等于 [ 3 -le $mynum ]
num1-gt num2 大于 [ 3 -gt $mynum ]
num1-ge num2 大于或等于 [ 3 -ge $mynum ]