In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use if statements in bash scripts". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use if statements in bash scripts.
In addition to the "if,else" form, there are other forms of "if" statements:
The code is as follows:
If [condition]
Then
Action
Fi
This statement executes the action only if condition is true, otherwise no action is performed and any lines after "fi" continue.
The code is as follows:
If [condition]
Then
Action
Elif [condition2]
Then
Action2
.
.
.
Elif [condition3]
Then
Else
Actionx
Fi
The above "elif" form tests each condition continuously and performs the operation that meets the first true condition. If none of the conditions are true, the "else" operation is executed, and if one condition is true, the line after the entire "if,elif,else" statement continues.
Received independent variable
In the sample program in the introductory article, we use the environment variable "$1" to reference the first command line argument. Similarly, you can use "$2", "$3", and so on to refer to the second and third arguments passed to the script. Here's an example:
The code is as follows:
#! / usr/bin/env bash
Echo name of script is $0
Echo first argument is $1
Echo second argument is $2
Echo seventeenth argument is $17
Echo number of arguments is $#
This example does not need to be explained except for the following two details. First, "$0" will be expanded to the name of the script invoked from the command line, and "$#" will be extended to the number of arguments passed to the script. Experiment with the above script to understand how it works by passing different types of command-line arguments.
Sometimes you need to reference all command line arguments at once. For this purpose, bash implements the variable "$@", which extends to all command-line arguments separated by spaces. You will see an example of using this variable in the "for" loop later in this article.
At this point, I believe you have a deeper understanding of "how to use if statements in bash scripts". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.