2011-11-19 55 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