如何在Shell脚本中调用函数?

27

我有一个Shell脚本,根据条件调用一个函数。

例如:

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

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

process_exit()
{
  commands...
  commands...
}
请告诉我如何完成这个任务。
7个回答

33

你没有指定使用哪个shell(有许多种),因此我假设您使用的是Bourne Shell,也就是我认为你的脚本应该以以下方式开始:

#!/bin/sh

请记得给未来的问题打上所使用的shell类型标签,这将有助于社区回答您的问题。
在调用函数之前,您需要定义它们。使用()
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

看看它在三个不同的${choice}值下运行。

祝你好运!


嗨@Johnsyweb,我没有发现我的示例和你的有任何区别...你能简要解释一下吗?谢谢... - user626206
3
我的例子在调用函数之前(即在脚本的早期)定义这些函数。 - johnsyweb
澄清一下,你应该在调用函数的位置之上明确定义函数。所以如果你在第10行调用一个函数,那么这个函数必须写在第2-9行,而不是11行及以下。 - d-b

14
#!/bin/bash

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

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


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

3
+1,欢迎来到Stack Overflow。您的答案说明了该怎么做,但是稍微解释一下会更有益。提问者可能无法发现您所写的与他们所拥有的之间的显著差异。 - Jonathan Leffler
5
then 之前缺少一个分号。 - camh

4

在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

嗨,@Authman Apatira,感谢您的回复。请为我的情况提供示例... - user626206
1
在您的文件存在函数中,写成"return [[ -f "$f" ]]"会更加简洁,不是吗? - Jonathan Leffler
嗨,@Jonathan Leffler,它不起作用。您能否根据我的情况提供示例...谢谢。 - user626206

2

在使用之前需要定义函数。sh中没有预先定义函数的机制,但一种常见的技术是执行以下操作:

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

process_install()
{
  命令...
  命令...
}

process_exit()
{
  命令...
  命令...
}
main()

1
你可以为函数单独创建另一个脚本文件,并在需要调用函数时调用该脚本文件。这将有助于保持你的代码整洁。
Function Definition : Create a new script file
Function Call       : Invoke the script file

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
#!/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 "$@"  

它将从主函数开始。

请您将代码格式化,方法是将其高亮显示,然后按Ctrl+K。 - WhatsThePoint

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接