Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在shell脚本中调用函数?_Shell - Fatal编程技术网

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

如何在shell脚本中调用函数?,shell,Shell,我有一个shell脚本,它有条件地调用函数 例如:- if [ "$choice" = "true" ] then process_install elif [ "$choice" = "false" ] then process_exit fi process_install() { commands... commands... } process_exit() { commands... commands... } 请告诉我如何完成此操作。在bash中使用

我有一个shell脚本,它有条件地调用函数

例如:-

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

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

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

请告诉我如何完成此操作。

在bash中使用函数()的示例:

总结:

  • 在使用函数之前定义它们
  • 定义后,将其视为命令
考虑这个名为
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既不知道也不关心它是外部命令还是函数。

您没有指定哪个shell(有),因此我假设,我认为您的脚本以以下内容开头:

#!/bin/sh
请记住用shell类型标记将来的问题,因为这将帮助社区回答您的问题

您需要在调用函数之前定义它们。使用
()

然后可以调用函数,就像调用任何命令一样:

if [ "$choice" = "true" ]
then
    process_install foo bar
elif [ "$choice" = "false" ]
then
    process_exit baz qux
您可能还希望在此时检查无效的选择

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


祝你好运

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

main() { case "$choice" in true) process_install;; false) process_exit;; esac } process_install() { commands... commands... } process_exit() { commands... commands... } main() main(){ 中的案例“$choice” 正确)进程_安装;; 假)进程_退出;; 以撒 } 进程安装() { 命令。。。 命令。。。 } 进程_退出() { 命令。。。 命令。。。 } main()
可以分别为函数创建另一个脚本文件,并在需要调用函数时调用该脚本文件。这将帮助您保持代码的整洁

Function Definition : Create a new script file
Function Call       : Invoke the script file

它将从主函数开始

Hi@Authman Apatira,谢谢你的回复。请为我的场景提供一个例子……在你的文件存在函数中写“
return[[-f”$f]]]
”会更简洁,不是吗?Hi@Jonathan Leffler,它不起作用。请您根据我的场景提供示例…谢谢。+1,欢迎来到Stack Overflow。你的回答说明了该做什么,但也会从一点解释中受益。提问者可能没有发现你写的东西和他们得到的东西之间的显著差异。你在
然后
之前缺少了一个分号。嗨@Johnsyweb,我没有发现我的示例和你的示例之间有任何差异……请你简要解释一下……谢谢……我的示例之前定义了函数(这在脚本的前面)它们被调用。请通过突出显示代码并按Ctrl+K来格式化代码,好吗
else
    echo "Invalid choice [${choice}]..."
fi
main() { case "$choice" in true) process_install;; false) process_exit;; esac } process_install() { commands... commands... } process_exit() { commands... commands... } main()
Function Definition : Create a new script file
Function Call       : Invoke the script file
#!/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 "$@"