Shell教程(六):函数、联机帮助
函數(shù)允許您對分解成更小的,邏輯子部分,然后可以被要求執(zhí)行各項(xiàng)任務(wù)時(shí),它需要一個(gè)腳本的整體功能。
使用函數(shù)來執(zhí)行重復(fù)性的任務(wù),是一個(gè)很好的方式來創(chuàng)建代碼的重用。代碼重用是現(xiàn)代面向?qū)ο缶幊痰脑瓌t的重要組成部分。
Shell函數(shù)是類似于其他編程語言中的子程序,過程和函數(shù)。
創(chuàng)建函數(shù):
聲明一個(gè)函數(shù),只需使用以下語法:
function_name () { ???list of commands }函數(shù)名 function_name,這就是你將使用它從其他地方在你的腳本調(diào)用。函數(shù)名必須遵循括號內(nèi),后括號內(nèi)的命令的列表。
例如:
以下是使用函數(shù)簡單的例子:
#!/bin/sh ? # Define your function here Hello () { ?? echo "Hello World" } ? # Invoke your function Hello當(dāng)你想執(zhí)行上面的腳本,它會產(chǎn)生以下結(jié)果:
$./test.sh Hello World $參數(shù)傳遞給函數(shù):
你可以定義一個(gè)函數(shù),它接受參數(shù),而調(diào)用這些函數(shù)。將這些參數(shù)代表$1,$2,依此類推。
以下是一個(gè)例子,我們傳遞兩個(gè)參數(shù)Zara和Ali?,然后我們捕獲和打印這些參數(shù)函數(shù)。
#!/bin/sh ? # Define your function here Hello () { ?? echo "Hello World $1 $2" } ? # Invoke your function Hello Zara Ali這將產(chǎn)生以下結(jié)果:
$./test.sh Hello World Zara Ali $從函數(shù)的返回值:
如果你執(zhí)行一個(gè)exit命令從一個(gè)函數(shù)內(nèi)部,其效果不僅是終止執(zhí)行的功能,而且Shell 程序中調(diào)用該函數(shù)。
如果你不是想,只是終止執(zhí)行該函數(shù),再有就是退出來的一個(gè)定義的函數(shù)。
根據(jù)實(shí)際情況,你可以從你的函數(shù)返回任何值,使用返回的命令,其語法如下:
return code這里的代碼可以是任何你選擇這里,但很明顯,你應(yīng)該選擇你的腳本作為一個(gè)整體的背景下是有意義的或有用的東西。
例子:
下面的函數(shù)返回一個(gè)值1:
#!/bin/sh ? # Define your function here Hello () { ?? echo "Hello World $1 $2" ?? return 10 } ? # Invoke your function Hello Zara Ali ? # Capture value returnd by last command ret=$? ? echo "Return value is $ret"這將產(chǎn)生以下結(jié)果:
$./test.sh Hello World Zara Ali Return value is 10 $嵌套函數(shù):
函數(shù)更有趣的功能之一是,他們可以調(diào)用本身以及調(diào)用其他函數(shù)。被稱為遞歸函數(shù)調(diào)用自身的函數(shù)。
經(jīng)過簡單的例子演示了一個(gè)嵌套的兩個(gè)函數(shù):
#!/bin/sh ? # Calling one function from another number_one () { ?? echo "This is the first function speaking..." ?? number_two } ? number_two () { ?? echo "This is now the second function speaking..." } ? # Calling function one. number_one這將產(chǎn)生以下結(jié)果:
This is the first function speaking... This is now the second function speaking...從提示的函數(shù)調(diào)用:
你可以把常用功能?.profile?的定義,這樣他們就會每當(dāng)?shù)卿?#xff0c;在命令提示符下,您可以使用它們。
或者,你可以在一個(gè)文件中的定義分組為 test.sh,然后通過鍵入當(dāng)前shell中執(zhí)行該文件:
$. test.sh這樣做的效果造成任何test.sh內(nèi)定義的函數(shù),可以閱讀在如下定義為當(dāng)前shell:
$ number_one This is the first function speaking... This is now the second function speaking... $要?jiǎng)h除從 shell 函數(shù)的定義,可以使用unset命令 .f 選項(xiàng)。這是相同的命令來刪除一個(gè)變量的定義Shell。
$unset .f function_name?
?
所有的Unix命令來與一些可選的和強(qiáng)制性的選擇。忘記這些命令的完整語法,這是很常見。
因?yàn)闆]有人能記得每一個(gè)UNIX命令和選項(xiàng),一直提供在線幫助,因?yàn)樵赨nix早期的時(shí)候。
Unix的版本的幫助文件,被稱為手冊頁。如果你知道任何命令的名字,但你不知道如何使用它,那么手冊頁來幫助你。
語法
下面是一個(gè)簡單的命令來獲得系統(tǒng)工作,而任何Unix命令的細(xì)節(jié):
$man command例子:
現(xiàn)在,你能想象的任何命令,你想要得到的幫助。假設(shè)你想知道關(guān)于pwd?,那么你只需要使用下面的命令:
$man pwd上面的命令將打開一個(gè)幫助你會給你pwd命令的完整信息。親自試一試在你的命令提示符下,以獲得更多的細(xì)節(jié)
你可以得到完整的細(xì)節(jié)上man命令本身使用下面的命令:
$man man手冊頁部分:
手冊頁一般分為部分,一般的man page作者偏好變化。下面是一些較常見的部分:
| 部分 | 描述 |
| NAME | Name of the command |
| SYNOPSIS | General usage parameters of the command. |
| DESCRIPTION | Generally describes of the command and what it does |
| OPTIONS | Describes all the arguments or options to the command |
| SEE ALSO | Lists other commands that are directly related to the command in the man page or closely resembling its functionality. |
| BUGS | Explains any known issues or bugs that exist with the command or its output |
| EXAMPLES | Common usage examples that give the reader an idea of how the command can be used. |
| AUTHORS | The author of the man page/command. |
最后,我要說的是手冊頁是一個(gè)重要的研究資源和第一途徑,當(dāng)你需要在Unix系統(tǒng)信息的命令或文件。
有用的Shell命令:
現(xiàn)在你知道如何著手,鏈接后會給你一個(gè)最重要和最頻繁使用的Unix Shell命令列表。
如果你不知道如何使用的任何命令,然后使用手冊頁獲取有關(guān)命令的完整細(xì)節(jié)。
這里是清單?shell - 有用的命令
from: http://www.yiibai.com/shell/what_is_shell.html#
總結(jié)
以上是生活随笔為你收集整理的Shell教程(六):函数、联机帮助的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell教程(五):替代、引用机制、输
- 下一篇: Sed教程(一):简介、环境设置、工作流