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

Tips on bash function

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "bash function tips", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "bash function tips" this article.

Here are some things you don't know about the bash function. Usually when you write a function, you do this:

Function name () {...}

Isn't it? I know you'll do that, because that's the way everyone writes functions. That's what I'm talking about. In bash

{... }

It doesn't mean "the body of the function" or "the scope of the function" as it does in JavaScript or c, it's actually a compound command. You can do all kinds of strange things, such as:

Function fileExists () [[- f $1]]

I don't need those curly braces! You can do this:

Function isEven () ($1% 2 = = 0)

This function is an arithmetic formula. Or you can do this:

Function name () (...

This will generate the function in the child shell instead of executing it in the current environment.

Or you can use while, if, case, select and for. Here's an example:

Function sleep1 () while:; do "$@"; sleep1; done

This function will create a sleep1 function that will always run one command per second. You can perform operations such as sleep1 df-h to monitor disk changes.

These tips not only make your code more beautiful, but also allow you to quickly write single-line bash programs, but they are also very useful. It is especially useful if you need to create a temporary environment for your function and temporarily change the variables or shell options.

This is an example I found in the code:

Function caseInsensitiveMatch () (shopt-s nocasematch....)

In this case, the function caseInsensitiveMatch is executed in a child shell, which sets the nocasematch option, and its scope is this function. For IFS and other variables, you usually need to change them temporarily. You don't have to save the previous values and then restore them.

These are all the contents of this article entitled "Tips for bash functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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