linux中脚本退出函数,Linux 命令 shell 脚本之09(函数)
1.使用函數(shù)
[oracle@XAG143 myshell]$ cat test_fun1.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 "Now this is the end of the script"
[oracle@XAG143 myshell]$ ./test_fun1.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
Now this is the end of the script
2.函數(shù)返回值(默認(rèn)退出狀態(tài)碼)
默認(rèn)情況下,函數(shù)的退出狀態(tài)碼是函數(shù)中最后一條命令返回的退出狀態(tài)碼。
在函數(shù)執(zhí)行結(jié)束后,可以用標(biāo)準(zhǔn)變量$?來(lái)確定函數(shù)的退出狀態(tài)碼
$ cat test4.sh
#!/bin/bash
# testing the exit status of a function
func1() {
echo "trying to display a non-existent file"
ls -l badfile
}
echo "testing the function: "
func1
echo "The exit status is: $?"
$ ./test4.sh
testing the function:
trying to display a non-existent file
ls: badfile: No such file or directory
The exit status is: 1
#函數(shù)的退出狀態(tài)碼是1,這是因?yàn)楹瘮?shù)中的最后一條命令沒有成功運(yùn)行。但你無(wú)法知道函數(shù)中其他命令中是否成功運(yùn)行
使用函數(shù)的默認(rèn)退出狀態(tài)碼是很危險(xiǎn)的
3.函數(shù)返回值(使用 return 命令)
$ cat test5.sh
#!/bin/bash
# using the return command in a function
function dbl {
read -p "Enter a value: " value
echo "doubling the value"
return $[ $value * 2 ]
}
dbl
echo "The new value is $?"
$ ./test5.sh
Enter a value: 2
doubling the value
The new value is 2
$ ./test5.sh
Enter a value: 200
doubling the value
The new value is 1
? 記住,函數(shù)一結(jié)束就取返回值;
? 記住,退出狀態(tài)碼必須是0~255。
要返回較大的整數(shù)值或者字符串值的話,你就不能用這種返回值的方法了
4.函數(shù)返回值(使用函數(shù)輸出)
會(huì)用echo語(yǔ)句來(lái)顯示計(jì)算的結(jié)果
$ cat test5b.sh
#!/bin/bash
# using the echo to return a value
function dbl {
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=$(dbl)
echo "The new value is $result"
$ ./test5b.sh
Enter a value: 200
The new value is 400
你會(huì)注意到dbl函數(shù)實(shí)際上輸出了兩條消息。read命令輸出了一條簡(jiǎn)短的消息來(lái)向用戶詢問輸入值。
bash shell腳本非常聰明,并不將其作為STDOUT輸出的一部分,并且忽略掉它。
如果你用echo語(yǔ)句生成這條消息來(lái)向用戶查詢,那么它會(huì)與輸出值一起被讀進(jìn)shell變量中。
5.向函數(shù)傳遞參數(shù)
函數(shù)名會(huì)在$0
變量中定義,函數(shù)命令行上的任何參數(shù)都會(huì)通過$1、$2等定義。也可以用特殊變量$#來(lái)判斷傳給函數(shù)的參數(shù)數(shù)目
[oracle@XAG143 myshell]$ cat test_fun6.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 10 and 15: "
value=$(addem 10 15)
echo $value
echo -n "Let's try adding just one number: "
value=$(addem 10)
echo $value
echo -n "Now trying adding no numbers: "
value=$(addem)
echo $value
echo -n "Finally, try adding three numbers: "
value=$(addem 10 15 20)
echo $value
[oracle@XAG143 myshell]$ ./test_fun6.sh
Adding 10 and 15: 25
Let's try adding just one number: 20
Now trying adding no numbers: -1
Finally, try adding three numbers: -1
6.獲取腳本在命令行中的參數(shù)值
[oracle@XAG143 myshell]$ cat test_fun7.sh
#!/bin/bash
# trying to access script parameters inside a function
function func7
{
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ]; then
value=$(func7 $1 $2)
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi
[oracle@XAG143 myshell]$ ./test_fun7.sh 4 5
The result is 20
8.函數(shù)中局部變量
只要在變量聲明的前面加上local關(guān)鍵字就可以了
[oracle@XAG143 myshell]$ cat test_fun8.sh
#!/bin/bash
# trying to access script parameters inside a function
function func7
{
local temp=$[ $1 * $2 ]
echo $[ $temp * 2 ]
}
if [ $# -eq 2 ]; then
value=$(func7 $1 $2)
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi
[oracle@XAG143 myshell]$ ./test_fun8.sh 4 5
The result is 40
9.向函數(shù)傳數(shù)組參數(shù)
[oracle@XAG143 myshell]$ cat test_fun10.sh
#!/bin/bash
# array variable to function test
function testit
{
local newarray
#newarray=$1
# or
newarray=($(echo "$@"))
echo "The new array value is: ${newarray[*]}"
for var in ${newarray[@]}; do
echo "打印的內(nèi)容@:" $var
done
for var2 in ${newarray[*]}; do
echo "打印的內(nèi)容*:" $var2
done
}
myarray=(1 2 3)
echo "The original array is ${myarray[*]}"
testit "${myarray[*]}"
[oracle@XAG143 myshell]$ ./test_fun10.sh
The original array is 1 2 3
The new array value is: 1 2 3
打印的內(nèi)容@: 1
打印的內(nèi)容@: 2
打印的內(nèi)容@: 3
打印的內(nèi)容*: 1
打印的內(nèi)容*: 2
打印的內(nèi)容*: 3
方法2
[oracle@XAG143 myshell]$ cat test_fun10.sh
#!/bin/bash
# array variable to function test
function testit
{
local newarray
newarray=($(echo "$@"))
echo "The new array value is: ${newarray[*]}"
for var in ${newarray[@]}; do
echo "打印的內(nèi)容@:" $var
done
for var2 in ${newarray[*]}; do
echo "打印的內(nèi)容*:" $var2
done
}
myarray=(1 2 3)
echo "The original array is ${myarray[*]}"
#testit "${myarray[*]}"
# or
arg1=$(echo ${myarray[*]})
testit $arg1
[oracle@XAG143 myshell]$ ./test_fun10.sh
The original array is 1 2 3
The new array value is: 1 2 3
打印的內(nèi)容@: 1
打印的內(nèi)容@: 2
打印的內(nèi)容@: 3
打印的內(nèi)容*: 1
打印的內(nèi)容*: 2
打印的內(nèi)容*: 3
10.從函數(shù)返回?cái)?shù)組
[oracle@XAG143 myshell]$ cat test_fun11.sh
#!/bin/bash
# returning an array value
function f_double
{
local origarray
local newarray
local elements
local i
origarray=($(echo "$@"))
newarray=($(echo "$@"))
elements=$[ $# - 1 ]
for (( i = 0; i <= $elements; i++ ))
{
newarray[$i]=$[ ${origarray[$i]} * 2 ]
}
echo ${newarray[*]}
}
myarray=(1 2 3)
echo "The original array is: ${myarray[*]}"
arg1=$(echo ${myarray[*]})
result=($(f_double $arg1))
echo "The new array is: ${result[*]}"
[oracle@XAG143 myshell]$ ./test_fun11.sh
The original array is: 1 2 3
The new array is: 2 4 6
11.創(chuàng)建函數(shù)庫(kù)
[oracle@DB02 myshell]$ cat myfuns.sh
# my script functions
function addem
{
echo $[ $1 + $2 ]
}
function multem
{
echo $[ $1 * $2 ]
}
[oracle@DB02 myshell]$ cat test_call_myfuns.sh
#!/bin/bash
# using functions defined in a library file
#. ./myfuns.sh
# or
source ./myfuns.sh
value1=10
value2=5
result1=$(addem $value1 $value2)
result2=$(multem $value1 $value2)
echo "The result of adding them is: $result1"
echo "The result of multiplying them is: $result2"
[oracle@DB02 myshell]$ chmod u+x test_call_myfuns.sh
[oracle@DB02 myshell]$ ./test_call_myfuns.sh
The result of adding them is: 15
The result of multiplying them is: 50
12.下載、安裝、使用GNU shtool shell腳本函數(shù)庫(kù)
[root@DB02 ~]# cd /usr/local/src
[root@DB02 src]# pwd
/usr/local/src
[root@DB02 src]# wget ftp://ftp.gnu.org/gnu/shtool/shtool-2.0.8.tar.gz
[root@DB02 src]# ls
shtool-2.0.8.tar.gz
使用tar命令提取文件。
[root@DB02 src]# tar -zxvf shtool-2.0.8.tar.gz
[root@DB02 src]# ls
shtool-2.0.8 shtool-2.0.8.tar.gz
構(gòu)建庫(kù)
[root@DB02 src]# cd shtool-2.0.8
[root@DB02 shtool-2.0.8]# ./configure
[root@DB02 shtool-2.0.8]# make
使用make命令測(cè)試這個(gè)庫(kù)文件
[root@DB02 shtool-2.0.8]# make test
Running test suite:
echo...........ok
mdate..........ok
table..........ok
prop...........ok
move...........ok
install........ok
mkdir..........ok
mkln...........ok
mkshadow.......ok
fixperm........ok
rotate.........ok
tarball........ok
subst..........ok
platform.......ok
arx............ok
slo............ok
scpp...........ok
version........ok
path...........ok
OK: passed: 19/19
安裝,需要使用make命令的install選項(xiàng)。不過你得以root用戶的身份運(yùn)行該命令
[root@DB02 shtool-2.0.8]# make install
./shtool mkdir -f -p -m 755 /usr/local
./shtool mkdir -f -p -m 755 /usr/local/bin
./shtool mkdir -f -p -m 755 /usr/local/share/man/man1
...
./shtool install -c -m 644 sh.version /usr/local/share/shtool/sh.version
./shtool install -c -m 644 sh.path /usr/local/share/shtool/sh.path
shtool 庫(kù)函數(shù)
shtool庫(kù)提供了大量方便的、可用于shell腳本的函數(shù)。如下列出了庫(kù)中可用的函數(shù)。
---------------------------------------------------
函 數(shù) 描 述
Arx 創(chuàng)建歸檔文件(包含一些擴(kuò)展功能)
Echo 顯示字符串,并提供了一些擴(kuò)展構(gòu)件
fixperm 改變目錄樹中的文件權(quán)限
install 安裝腳本或文件
mdate 顯示文件或目錄的修改時(shí)間
mkdir 創(chuàng)建一個(gè)或更多目錄
Mkln 使用相對(duì)路徑創(chuàng)建鏈接
mkshadow 創(chuàng)建一棵陰影樹
move 帶有替換功能的文件移動(dòng)
Path 處理程序路徑
platform 顯示平臺(tái)標(biāo)識(shí)
Prop 顯示一個(gè)帶有動(dòng)畫效果的進(jìn)度條
rotate 轉(zhuǎn)置日志文件
Scpp 共享的C預(yù)處理器
Slo 根據(jù)庫(kù)的類別,分離鏈接器選項(xiàng)
Subst 使用sed的替換操作
Table 以表格的形式顯示由字段分隔(field-separated)的數(shù)據(jù)
tarball 從文件和目錄中創(chuàng)建tar文件
version 創(chuàng)建版本信息文件
--------------------------------------------------------
每個(gè)shtool函數(shù)都包含大量的選項(xiàng)和參數(shù),你可以利用它們改變函數(shù)的工作方式。下面是shtool函數(shù)的使用格式:
shtool [options] [function [options] [args]]
13.使用庫(kù)shtool
[oracle@DB02 myshell]$ cat test_shtool.sh
#!/bin/bash
shtool platform
[oracle@DB02 myshell]$ ./test_shtool.sh
centos 6.5 (AMD64)
查看幫助
[oracle@DB02 myshell]$ shtool -h
總結(jié)
以上是生活随笔為你收集整理的linux中脚本退出函数,Linux 命令 shell 脚本之09(函数)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 早期牙周炎能治好吗
- 下一篇: linux计划任务执行bin文件,Lin