高级shell 脚本

1.函数

函数是一个脚本代码块,你可以为其命名并在代码中任何位置重用。要在脚本中使用该代码块时,只要使用所起的函数名就行了(这个过程称为调用函数)。本节将会介绍如何在shell脚本中创建和使用函数

 创建函数

function name { 
 commands 
}
name() { 
commands 
}

使用函数

[root@localhost advanced_shell_script]# cat test1.sh 
#!/bin/bash
#using a function in a script


function func1 {

    echo "This is an example of a function"

}


count=1

while [ $count -le 3 ] ;do
    func1
    count=$[ $count + 1 ]
done

echo "This is the end of the loop"
func1
echo "This is script end"

[root@localhost advanced_shell_script]# ./test1.sh
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
This is script end
[root@localhost advanced_shell_script]# 

使用return  返回 函数

[root@localhost advanced_shell_script]# cat test2.sh 
#!/bin/bash

#using the return command in a function

function db1 {
 read -p "Enter a value:" value
echo "doubling the value"
return $[ $value * 2  ]



}
db1
echo "The new value $?"

[root@localhost advanced_shell_script]# ./test2.sh 
Enter a value:2
doubling the value
The new value 4 
[root@localhost advanced_shell_script]# ./test2.sh       #注意$? 返回函数值必须小于256 ,因此需要另外一种方法
Enter a value:200
doubling the value
The new value 144
[root@localhost advanced_shell_script]# 

另外一种方法

[root@localhost advanced_shell_script]# cat test3.sh 
#!/bin/bash

#using the return command in a function

function db1 {
 read -p "Enter a value:" value
#echo "doubling the value"
echo $[ $value * 2  ]               #新函数会用echo语句来显示计算的结果。该脚本会获取dbl函数的输出,而不是查看退出状态码
}
value1=$(db1)
echo "The new value $value1"

[root@localhost advanced_shell_script]# ./test3.sh 
Enter a value:200
The new value 400
[root@localhost advanced_shell_script]# 

 传递参数:

[root@localhost advanced_shell_script]# cat test4.sh 
#!/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 20 and 30:"
value=$(addem 20 30)
echo $value

echo -n "30 *:"
value=$( addem 30)
echo $value

echo -n "10 20 30:"
value=$(addem 10 20 30)
echo $value

[root@localhost advanced_shell_script]# ./test4.sh 
Adding 20 and 30:50
30 *:900
10 20 30:-1
[root@localhost advanced_shell_script]# 

 命令行传递参数

[root@localhost advanced_shell_script]# cat test5.sh 
#/bin/bash
#trying to access script parameters inside a function

function func1 {
    echo $[ $1 * $2 ]


}

if [ $# -eq 2 ];then

    value=$(func1 $1 $2)
    echo "The result is $value"
else
    echo "Usage:badtest1 a b"
fi
[root@localhost advanced_shell_script]# ./test5.sh 
Usage:badtest1 a b
[root@localhost advanced_shell_script]# ./test5.sh 2 3
The result is 6
[root@localhost advanced_shell_script]# 

 在函数中处理变量

全局变量和局部变量

1. 全局变量
全局变量是在shell脚本中任何地方都有效的变量。如果你在脚本的主体部分定义了一个全局变量,那么可以在函数内读取它的值。类似地,如果你在函数内定义了一个全局变量,可以在脚本的主体部分读取它的值。
13 2. 局部变量
无需在函数中使用全局变量,函数内部使用的任何变量都可以被声明成局部变量。要实现这一点,只要在变量声明的前面加上local关键字就可以了。
[root@localhost advanced_shell_script]# cat test6.sh 
#!/bin/bash
#demonstrating the local keyword
function func1 {

    local test1=$[ $test2 * 2  ]

    test3=$[ test2 * 2 ]

}
test3=100
test1=10
test2=5
func1
echo "$test1 $test2 $test3 "

[root@localhost advanced_shell_script]# vim test6.sh 
[root@localhost advanced_shell_script]# ./test6.sh     #局部变量的值  test1 在函数执行后没有被修改,test3 的值在函数执行后,被修改为函数执行的结果
1223340 5 10 
[root@localhost advanced_shell_script]# 

 向函数传递数组变量的注意事项

[root@localhost advanced_shell_script]# cat test8.sh 
#!/bin/bash 
# array variable to function test 
function test1 {                  #在向函数传递数组变量的时候,需要将数据变量当做一个整体进行传递,通过$@ 方式 
                    #$@ 将给定的多个参数定义为同一字符串的多个单独的单词,类似  "A" "B"  "C" local newarray newarray
=($(echo $@)) echo "The new array value is: ${newarray[*]}" } function test2 { #在向函数传递数组变量时,如果使用 $1 参数,会识别为数组变量的第一个参数。无法完整的传递整个参数。默认不会将传递给函数的数组变量看成一个参数 local newarray newarray=$1 echo "The test2 function valueis: ${newarray[*]}" } myarray=(1 2 3 4 5) echo "The original array is ${myarray[*]}" test1 ${myarray[*]} test2 ${myarray[*]} [root@localhost advanced_shell_script]# ./test8.sh The original array is 1 2 3 4 5 The new array value is: 1 2 3 4 5 The test2 function valueis: 1 [root@localhost advanced_shell_script]#

函数递归的使用,使用bash -x 跟踪递归的处理步骤很有效

root@localhost advanced_shell_script]# cat test11.sh 
#!/bin/bash
#using recursion

function factorial {

    if [ $1  -eq 1 ];then
    
    echo 1
    else
    local temp=$[ $1 - 1 ]
    local result=`factorial $temp`
    #return $1
    echo $[ $result * $1 ]
    fi
}

value=5
result=$(factorial $value)
echo "$result"
[root@localhost advanced_shell_script]# bash -x test11.sh 
+ value=5
++ factorial 5
++ '[' 5 -eq 1 ']'
++ local temp=4
+++ factorial 4
+++ '[' 4 -eq 1 ']'
+++ local temp=3
++++ factorial 3
++++ '[' 3 -eq 1 ']'
++++ local temp=2
+++++ factorial 2
+++++ '[' 2 -eq 1 ']'
+++++ local temp=1
++++++ factorial 1
++++++ '[' 1 -eq 1 ']'
++++++ echo 1
+++++ local result=1
+++++ echo 2
++++ local result=2
++++ echo 6
+++ local result=6
+++ echo 24
++ local result=24
++ echo 120
+ result=120
+ echo 120
120
[root@localhost advanced_shell_script]# 

创建库的办法

[root@localhost advanced_shell_script]# cat myfuncs   #创建的库文件需要有执行权限
#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
}


[root@localhost advanced_shell_script]# cat test12.sh 
#!/bin/bash 
# using functions defined in a library file 
source ./myfuncs   #两种方式调用库文件 ,source 和 ..  ,这是让库文件在当前shell 调用。如果使用 ./mufuncs 相当于在子shell 调用了。会找不到命令
# ../myfuncs
value1
=10 value2=5 result1=$(addem $value1 $value2) result2=$(multem $value1 $value2) result3=$(divem $value1 $value2) echo "The result of adding them is: $result1" echo "The result of multiplying them is: $result2" echo "The result of dividing them is: $result3" # [root@localhost advanced_shell_script]# ./test12.sh The result of adding them is: 15 The result of multiplying them is: 50 The result of dividing them is: 2 [root@localhost advanced_shell_script]#

在命令行创建函数:

[root@localhost advanced_shell_script]# function funcs {  #当在命令行上定义函数时,你必须记得在每个命令后面加个分号,这样shell就能知道在哪里是命令的起止了
> echo $[ $1 + $2  ];}
[root@localhost advanced_shell_script]# funcs 1 2
3
[root@localhost advanced_shell_script]# 

在 .bashrc 中定义函数

[root@localhost ~]# cat /root/.bashrc     # .bashrc 是函数的最佳定义位置
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

function add {
    echo $[ $1 + $2 ]

}
[root@localhost ~]# 

在script 中使用在.bashrc 中定义的函数

root@localhost advanced_shell_script]# cat test13.sh 
#!/bin/bash

source /root/.bashrc    #通过source 命令将函数调用进来,否则命令找不到

result=$(add 2 3) 
echo "$result"

[root@localhost advanced_shell_script]# 
原文地址:https://www.cnblogs.com/zy09/p/10656083.html