Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write functions in Bash

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to write functions in Bash", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write functions in Bash.

Reduce code redundancy and maintenance by writing functions.

When programming, you are actually defining a procedure procedure or routine routine to be executed by the computer. A simple analogy is to compare computer programming with toast: you list the ingredients to build a work environment at once, and then list the steps that must be taken to bake bread. In programming and baking, some steps must be repeated at different intervals. For example, in toast, this may be the process of yeast culture:

STIR=100SNOOZE=86400 function feed_culture {remove_from (pantry) add (flour, water) stir ($STIR) sleep ($SNOOZE)}

Then, knead the dough and wake up the dough:

KNEAD=600SNOOZE=7200 function process_dough {remove_from (proofing_drawer) knead ($KNEAD) return_to_drawer ($SNOOZE)}

In programming, these subroutines subroutines can be expressed as functions function. Functions are important to programmers because they help reduce redundancy in the code, thereby reducing the amount of maintenance required. For example, in the hypothetical scenario of programmatically baking bread, if you need to change the dough wake-up time, as long as you used the function before, then you only need to change the use time once, either using the variable (SNOOZE in the sample code) or directly in the dough processing subroutine. This will save you a lot of time because you don't have to traverse every dough that may be waking up through your code base, let alone worry about missing one. Many bug are caused by unchanged missing values or incorrect sed commands that want to capture all possibilities without having to look for them manually.

In Bash, whether in a script written or in a separate file, defining functions is as simple as using them. If you save the function to a separate file. Then you can source it into a script, just like a library in the include C language or C++ or import a module into Python. To create a Bash function, use the keyword function:

Function foo {# code here}

This is an example of how to use parameters in a function (some are artificially designed, so it may be easier):

#! / usr/bin/env bashARG=$1 function mimic {if [[- z $ARG]]; then ARG='world' fi echo "hello $ARG"} mimic $ARG

The results are as follows:

$. / mimichello world$. / mimic everybodyhello everybody

Notice the last line of the script, which executes the function. This is a common puzzle for novice scripting: functions do not execute automatically. They exist as potential routines until they are called.

If the function is not called, the function is only defined and will never be run.

If you are new to Bash, try to execute the sample script once with the last line included, and then again with the last line commented out.

Use function

Even for simple scripts, functions are an important programming concept. The more you adapt to functions, the easier it will be when faced with a complex problem that requires not only a declarative command line, but also more dynamic. Saving general-purpose functions in a separate file can also save some work because it will help you build commonly used programs so that you can reuse them between projects. Take a look at your scripting habits to see if it is appropriate to use functions.

At this point, I believe you have a deeper understanding of "how to write functions in Bash". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report