linux中脚本退出函数,Linux 命令 shell 脚本之09(函数)
1.使用函數
[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.函數返回值(默認退出狀態碼)
默認情況下,函數的退出狀態碼是函數中最后一條命令返回的退出狀態碼。
在函數執行結束后,可以用標準變量$?來確定函數的退出狀態碼
$ 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
#函數的退出狀態碼是1,這是因為函數中的最后一條命令沒有成功運行。但你無法知道函數中其他命令中是否成功運行
使用函數的默認退出狀態碼是很危險的
3.函數返回值(使用 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
? 記住,函數一結束就取返回值;
? 記住,退出狀態碼必須是0~255。
要返回較大的整數值或者字符串值的話,你就不能用這種返回值的方法了
4.函數返回值(使用函數輸出)
會用echo語句來顯示計算的結果
$ 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
你會注意到dbl函數實際上輸出了兩條消息。read命令輸出了一條簡短的消息來向用戶詢問輸入值。
bash shell腳本非常聰明,并不將其作為STDOUT輸出的一部分,并且忽略掉它。
如果你用echo語句生成這條消息來向用戶查詢,那么它會與輸出值一起被讀進shell變量中。
5.向函數傳遞參數
函數名會在$0
變量中定義,函數命令行上的任何參數都會通過$1、$2等定義。也可以用特殊變量$#來判斷傳給函數的參數數目
[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.獲取腳本在命令行中的參數值
[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.函數中局部變量
只要在變量聲明的前面加上local關鍵字就可以了
[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.向函數傳數組參數
[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 "打印的內容@:" $var
done
for var2 in ${newarray[*]}; do
echo "打印的內容*:" $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
打印的內容@: 1
打印的內容@: 2
打印的內容@: 3
打印的內容*: 1
打印的內容*: 2
打印的內容*: 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 "打印的內容@:" $var
done
for var2 in ${newarray[*]}; do
echo "打印的內容*:" $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
打印的內容@: 1
打印的內容@: 2
打印的內容@: 3
打印的內容*: 1
打印的內容*: 2
打印的內容*: 3
10.從函數返回數組
[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.創建函數庫
[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腳本函數庫
[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
構建庫
[root@DB02 src]# cd shtool-2.0.8
[root@DB02 shtool-2.0.8]# ./configure
[root@DB02 shtool-2.0.8]# make
使用make命令測試這個庫文件
[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選項。不過你得以root用戶的身份運行該命令
[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 庫函數
shtool庫提供了大量方便的、可用于shell腳本的函數。如下列出了庫中可用的函數。
---------------------------------------------------
函 數 描 述
Arx 創建歸檔文件(包含一些擴展功能)
Echo 顯示字符串,并提供了一些擴展構件
fixperm 改變目錄樹中的文件權限
install 安裝腳本或文件
mdate 顯示文件或目錄的修改時間
mkdir 創建一個或更多目錄
Mkln 使用相對路徑創建鏈接
mkshadow 創建一棵陰影樹
move 帶有替換功能的文件移動
Path 處理程序路徑
platform 顯示平臺標識
Prop 顯示一個帶有動畫效果的進度條
rotate 轉置日志文件
Scpp 共享的C預處理器
Slo 根據庫的類別,分離鏈接器選項
Subst 使用sed的替換操作
Table 以表格的形式顯示由字段分隔(field-separated)的數據
tarball 從文件和目錄中創建tar文件
version 創建版本信息文件
--------------------------------------------------------
每個shtool函數都包含大量的選項和參數,你可以利用它們改變函數的工作方式。下面是shtool函數的使用格式:
shtool [options] [function [options] [args]]
13.使用庫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
總結
以上是生活随笔為你收集整理的linux中脚本退出函数,Linux 命令 shell 脚本之09(函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 早期牙周炎能治好吗
- 下一篇: 视网膜静脉阻塞的原因