2011-11-19 48 views
14

我有一個shell腳本有條件地調用一個函數。如何在shell腳本中調用函數?

例如: -

if [ "$choice" = "true" ] 
then 
    process_install 
elif [ "$choice" = "false" ] 
then 
    process_exit 
fi 

process_install() 
{ 
    commands... 
    commands... 
} 

process_exit() 
{ 
    commands... 
    commands... 
} 

請讓我知道如何做到這一點。

回答

14

不指定外殼(有many),所以我假設Bourne Shell,那是我想你的腳本開頭:

#!/bin/sh 

請記住標記與外殼未來的問題類型,因爲這將有助於社區回答你的問題。

您需要在之前定義您的功能,然後再調用它們。使用()

process_install() 
{ 
    echo "Performing process_install() commands, using arguments [${*}]..." 
} 

process_exit() 
{ 
    echo "Performing process_exit() commands, using arguments [${*}]..." 
} 

然後,你可以打電話給你的功能,就像如果你調用任何命令:

if [ "$choice" = "true" ] 
then 
    process_install foo bar 
elif [ "$choice" = "false" ] 
then 
    process_exit baz qux 

您也不妨在這個時刻檢查無效的選擇...

else 
    echo "Invalid choice [${choice}]..." 
fi 

See it run with three different values of ${choice}.

祝你好運!

+0

嗨@Johnsyweb,我沒有發現我的例子和你的任何區別...你可以請解釋簡短的....謝謝... – user626206

+0

我的例子定義之前的功能(這是在腳本的早期),他們是調用。 – Johnsyweb

0

在bash使用函數()的實施例:

#!/bin/bash 
# file.sh: a sample shell script to demonstrate the concept of Bash shell functions 
# define usage function 
usage(){ 
    echo "Usage: $0 filename" 
    exit 1 
} 

# define is_file_exists function 
# $f -> store argument passed to the script 
is_file_exists(){ 
    local f="$1" 
    [[ -f "$f" ]] && return 0 || return 1 
} 
# invoke usage 
# call usage() function if filename not supplied 
[[ $# -eq 0 ]] && usage 

# Invoke is_file_exits 
if (is_file_exists "$1") 
then 
echo "File found: $1" 
else 
echo "File not found: $1" 
fi 
+0

嗨@Authman Apatira,感謝您的reply.Please對我的方案提供的例子...... – user626206

+1

這將是更簡潔寫「'在文件存在函數中返回[[-f「$ f」]]'',會不會? –

+0

嗨@Jonathan Leffler,它不工作。請按照我的情景提供示例...謝謝。 – user626206

8
#!/bin/bash 

process_install() 
{ 
    commands... 
    commands... 
} 

process_exit() 
{ 
    commands... 
    commands... 
} 


if [ "$choice" = "true" ] then 
    process_install 
else 
    process_exit 
fi 
+3

+1並歡迎使用堆棧溢出。你的答案說明了該怎麼做 - 但也會從一些小解釋中受益。提問者可能不會發現您所寫的內容與他們所得到的內容之間的顯着差異。 –

+4

在'then'之前你錯過了一個分號。 – camh

1

總結:

  • 使用之前定義的功能。
  • 一旦定義,將它們視爲命令。

考慮這個劇本,叫funcdemo

#!/bin/bash 

[ $# = 0 ] && exhort "write nastygram" 

exhort(){ 
    echo "Please, please do not forget to $*" 
} 

[ $# != 0 ] && exhort "write begging letter" 

在使用中:

$ funcdemo 
./funcdemo: line 3: exhort: command not found 
$ funcdemo 1 
Please, please do not forget to write begging letter 
$ 

註記缺失功能的潛力躺在未被發現由客戶很長一段時間(想」在最嚴重的錯誤時刻')。只有當函數在執行時才存在,這與它在執行時是否存在任何其他命令一樣重要。的確,在執行命令之前,shell既不知道也不在乎它是外部命令還是函數。

1

這些功能在使用前需要定義。有沒有機制SH預先聲明的功能,但常用的方法是做這樣的事情:

 
main() { 
    case "$choice" in 
    true) process_install;; 
    false) process_exit;; 
    esac 
} 

process_install() 
{ 
    commands... 
    commands... 
} 

process_exit() 
{ 
    commands... 
    commands... 
} 

main() 
1

可以爲功能單獨創建另一個腳本文件並調用腳本文件時,你要撥打的功能。這將幫助您保持代碼清潔。

Function Definition : Create a new script file 
Function Call  : Invoke the script file 
-1
#!/bin/bash 
# functiontest.sh a sample to call the function in the shell script 

choice="true" 
function process_install 
{ 
    commands... 
} 

function process_exit 
{ 
    commands... 
} 

function main 
{ 
    if [[ "$choice" == "true" ]]; then 
     process_install 
    elif [[ "$choice" == "false" ]]; then 
     process_exit 
    fi 
} 

main "[email protected]" 

它會從main函數開始

+0

您可以通過突出顯示並按下Ctrl + K來格式化您的代碼 – WhatsThePoint