In addition to the "If,else" form, there are other forms of "if" statements:
Copy Code code as follows:
if [condition]
Then
Action
Fi
The statement performs an action only if condition is true, otherwise the operation is not performed and any rows after "fi" are resumed.
Copy Code code as follows:
if [condition]
Then
Action
elif [Condition2]
Then
Action2
.
.
.
elif [Condition3]
Then
Else
ActionX
Fi
The above "elif" form will continuously test each condition and perform an operation that conforms to the first true condition. If no condition is true, the "Else" action is executed, and if one condition is true, the line following the entire "If,elif,else" statement continues.
Receive self variable
In the sample program in the introductory article, we use the environment variable "$" to refer to the first command-line argument. Similarly, you can use "$", "$", and so on to refer to the second and third arguments passed to the script. Here's an example:
Copy Code code as follows:
#!/usr/bin/env Bash
echo name of script is $
echo-argument is $
Echo second argument is $
Echo Seventeenth argument is $17
echo number of arguments is $#
In addition to the following two details, this example does not need to be explained. First, "$" expands to the name of the script that is invoked from the command line, and "$#" expands to the number of arguments passed to the script. Test the above script to understand how it works by passing different types of command line arguments.
It is sometimes necessary to refer to all command-line arguments at once. For this purpose, bash implements the variable "$@", which expands to all command line arguments separated by spaces. In the "for" loop section later in this article, you'll see an example of using that variable.