shell 函数

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 ]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 来源: Linux命令行与shell脚本编程大全 博客地址,推荐电脑点 内容 基本的脚本函数返回值在函数中使用变量...
    王诗翔阅读 7,120评论 0 3
  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy阅读 13,144评论 1 51
  • 寝室有个女生,来自农村,黑黑的,一口流利的家乡话,乍一看挺朴实的一个人。背着斜挎的布包,简便的行李。刚来寝室的时候...
    楚楚玖阅读 1,651评论 0 1
  • 无意间想起了曾经烧掉的日记和书稿。当时想着烧掉了就过去了,也就不留痕迹了。后来我老想起那些文字和那些时光。但在记忆...
    温霄冰阅读 1,651评论 0 0
  • 出去读书后,很多人都只能假期见一见,“下来抽根烟呗,等你。” 这聚少离多的时间里,好像每一回见面故友的改变都是如此...
    染墨I阅读 981评论 0 1