mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
0f72c57346
commit
4fae43106e
@ -1,106 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to write functions in Bash)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-functions)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
How to write functions in Bash
|
||||
======
|
||||
Reduce redundancy and maintenance in your code by writing functions.
|
||||
![woman on laptop sitting at the window][1]
|
||||
|
||||
When you're programming, you're literally defining a procedure, or a _routine_, you want the computer to perform. A simple analogy compares computer programming to baking bread: you list ingredients once to set up the work environment, then you list the steps you must take to end up with a loaf of bread. In both programming and baking, some steps must be repeated at different intervals. In baking bread, for instance, this could be the process of feeding a sourdough culture:
|
||||
|
||||
|
||||
```
|
||||
STIR=100
|
||||
SNOOZE=86400
|
||||
|
||||
function feed_culture {
|
||||
remove_from(pantry)
|
||||
add(flour, water)
|
||||
stir($STIR)
|
||||
sleep($SNOOZE)
|
||||
}
|
||||
```
|
||||
|
||||
And later, kneading and proofing the dough:
|
||||
|
||||
|
||||
```
|
||||
KNEAD=600
|
||||
SNOOZE=7200
|
||||
|
||||
function process_dough {
|
||||
remove_from(proofing_drawer)
|
||||
knead($KNEAD)
|
||||
return_to_drawer($SNOOZE)
|
||||
}
|
||||
```
|
||||
|
||||
In programming, these subroutines can be expressed as _functions_. Functions are important for programmers because they help reduce redundancy in code, which in turn reduces the amount of maintenance required. For example, in the imaginary scenario of baking bread programmatically, if you need to change the amount of time the dough proofs, as long as you've used a function before, you merely have to change the value of the seconds once, either by using a variable (called **SNOOZE** in the sample code) or directly in the subroutine that processes dough. That can save you a lot of time, because you don't have to hunt through your codebase for every possible mention of rising dough, much less worry about missing one. Many a bug's been caused by a missed value that didn't get changed or by a poorly executed **`sed`** command in hopes of catching every last match without having to hunt for them manually.
|
||||
|
||||
In [Bash][2], defining a function is as easy as setting it either in the script file you're writing or in a separate file. If you save functions to a dedicated file, you can `source` it into your script as you would `include` a library in C or C++ or `import` a module into Python. To create a Bash function, use the keyword `function`:
|
||||
|
||||
|
||||
```
|
||||
function foo {
|
||||
# code here
|
||||
}
|
||||
```
|
||||
|
||||
Here's a simple (and somewhat contrived, as this could be made simpler) example of how a function works with arguments:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env bash
|
||||
ARG=$1
|
||||
|
||||
function mimic {
|
||||
if [[ -z $ARG ]]; then
|
||||
ARG='world'
|
||||
fi
|
||||
echo "hello $ARG"
|
||||
}
|
||||
|
||||
mimic $ARG
|
||||
```
|
||||
|
||||
Here are the results:
|
||||
|
||||
|
||||
```
|
||||
$ ./mimic
|
||||
hello world
|
||||
$ ./mimic everybody
|
||||
hello everybody
|
||||
```
|
||||
|
||||
Note the final line of the script, which executes the function. This is a common point of confusion for beginning scripters and programmers: functions don't get executed automatically. They exist as _potential_ routines until they are called.
|
||||
|
||||
Without a line calling the function, the function would only be defined and would never run.
|
||||
|
||||
If you're new to Bash, try executing the sample script once with the last line included and again with the last line commented out.
|
||||
|
||||
### Using functions
|
||||
|
||||
Functions are vital programming concepts, even for simple scripts. The more comfortable you become with functions, the better off you'll be when you're faced with a complex problem that needs something more dynamic than just declarative lines of commands. Keeping general-purpose functions in separate files can also save you some work, as it'll help you build up routines you commonly use so that you can reuse them across projects. Look at your scripting habits and see where functions might fit.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-functions
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-window-focus.png?itok=g0xPm2kD (young woman working on a laptop)
|
||||
[2]: https://opensource.com/resources/what-bash
|
106
translated/tech/20200610 How to write functions in Bash.md
Normal file
106
translated/tech/20200610 How to write functions in Bash.md
Normal file
@ -0,0 +1,106 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to write functions in Bash)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-functions)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
如何在 Bash 中编写函数
|
||||
======
|
||||
通过编写函数来减少代码的冗余和维护。
|
||||
![woman on laptop sitting at the window][1]
|
||||
|
||||
在编程时,实际上是在定义要由计算机执行的过程或_程序_。一个简单的类比是将计算机编程与烤面包进行比较:你一次列出了要设置工作环境的配料,然后列出了烤面包所必须采取的步骤。在编程和烘烤中,必须以不同的间隔重复执行某些步骤。例如,在烤面包中,这可能是酵母培养的过程:
|
||||
|
||||
|
||||
```
|
||||
STIR=100
|
||||
SNOOZE=86400
|
||||
|
||||
function feed_culture {
|
||||
remove_from(pantry)
|
||||
add(flour, water)
|
||||
stir($STIR)
|
||||
sleep($SNOOZE)
|
||||
}
|
||||
```
|
||||
|
||||
然后,揉面和醒发面团:
|
||||
|
||||
|
||||
```
|
||||
KNEAD=600
|
||||
SNOOZE=7200
|
||||
|
||||
function process_dough {
|
||||
remove_from(proofing_drawer)
|
||||
knead($KNEAD)
|
||||
return_to_drawer($SNOOZE)
|
||||
}
|
||||
```
|
||||
|
||||
在编程中,这些子例程可以表示为_函数_。函数对程序员很重要,因为它们有助于减少代码中的冗余,从而减少了所需的维护量。例如,在以编程方式烤制面包的假想场景中,如果你需要更改面团醒发的时间,只要你之前使用函数,那么你只需更改一次时间,或使用变量(在示例代码中为 **SNOOZE**)或直接在处理面团的子程序中更改时间。这样可以节省你很多时间,因为你不必通过你的代码库遍历每个可能正在醒发的面团,更不用说担心错过一个。许多 bug 是由未更改的缺失值或执行不正确的 **`sed`** 命令引起的,它们希望捕获所有可能而不必手动寻找它们。
|
||||
|
||||
在 [Bash][2] 中,在编写的脚本或在独立的文件中定义函数都一样简单。如果将函数保存到独立的文件中。那么可以将它 `source` 到脚本中,就像 `include` C 语言或 C++ 中的库或将模块 `import` 到 Python 中一样。要创建一个 Bash 函数,请使用关键字 `function`:
|
||||
|
||||
|
||||
```
|
||||
function foo {
|
||||
# code here
|
||||
}
|
||||
```
|
||||
|
||||
这是一个如何在函数中使用参数的例子(有些人为设计,因此可能会更简单):
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env bash
|
||||
ARG=$1
|
||||
|
||||
function mimic {
|
||||
if [[ -z $ARG ]]; then
|
||||
ARG='world'
|
||||
fi
|
||||
echo "hello $ARG"
|
||||
}
|
||||
|
||||
mimic $ARG
|
||||
```
|
||||
|
||||
结果如下:
|
||||
|
||||
|
||||
```
|
||||
$ ./mimic
|
||||
hello world
|
||||
$ ./mimic everybody
|
||||
hello everybody
|
||||
```
|
||||
|
||||
请注意脚本的最后一行,它执行该函数。对于新手脚本编写人员和程序员来说,这是一个普遍的困惑点:函数不会自动执行。它们作为_潜在的_子程序存在,直到被调用。
|
||||
|
||||
如果没有调用该函数,那么函数将仅被定义并且永远不会运行。
|
||||
|
||||
如果你刚接触 Bash,请尝试在包含最后一行的情况下执行示例脚本一次,然后在注释掉最后一行的情况下再次执行示例脚本。
|
||||
|
||||
### 使用函数
|
||||
|
||||
即使对于简单的脚本,函数也是很重要的编程概念。你越适应函数,在面对一个不仅需要声明性的命令行,还需要更多动态的复杂问题时,你就会越容易。将通用函数保存在单独的文件中还可以节省一些工作,因为它将帮助你建立常用的程序,以便你可以在项目间重用它们。看看你的脚本习惯,看是否有函数适合。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-functions
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-window-focus.png?itok=g0xPm2kD (young woman working on a laptop)
|
||||
[2]: https://opensource.com/resources/what-bash
|
Loading…
Reference in New Issue
Block a user