Shell script 8-19

Common logical statements

1, for statement:

for variable in list; do
   Loop
done

for((初始语句; 判断语句; 值变化语句)); do
   Loop
done

case statement:

2, case variable in
PAT1)
   Execution Statement
;;
PAT2)
   execute statement
;;
< span style="font-family: Song Ti; font-size: 14px;"> *)
The default execution statement< /span>
;;
esac

3, while statement:

Define the initial value
while condition Judgment; do
   Loop
   value change statement
done

4, until statement: the same as the while statement usage, but only the judgment condition is When it is False, it will enter the loop statement

5. If statement: p>

if condition judgment; then
   execute语句
fi

if conditional judgment; then
   execute statement 1
else
  Execute sentence 2
fi

if conditional judgment; then
  Execute statement 1
elif conditional judgment; then
   execute sentence 2
elif condition judgment; then
   execute statement 3
else
  Execute statement 4
fi

Function

1. Function introduction: In order to avoid code reuse, we generally write code blocks through functions, and this code block Used to implement a certain function, and this function will be called repeatedly in the code behind.

2, the syntax format of the function:

1, function fun_name(){

   Function body

}

fun_name

[Calling a function, the command in the function body will not be executed if you don’t call the function, the command in the function body will be executed wherever it is called]< /p>

3. Function call——return return value

(1) When the judgment statement is specified in the function, we default to return an incorrect result, but the most recent command is indeed correct. Time, we need to return an error status code we need through return;
(2) At the end of the function, we You need to specify a correct return code for return;
If used in a loop statement, return will jump out of the loop;

4. Function receiving parameters: letter Number + variable, same as ordinary command. Just note that when the function receives parameters, it will not use external parameters, but the internal parameters of the script shall prevail.

5. Variables in the function: global variables

       Local variables: variables inside the script

Local variable: local variable name=xxx Local variables only take effect within this function

Environment variables: Take effect in bash in the entire Linux system

6. Questions about script execution:

(1) bash method to execute – #!/bin/bash

The variables defined in the script executed by bash are in the subshell of the current shell;

(2) Full path or relative path to execute; – Authorized execution permission is required;

(source script file or. Script file)

source this way to execute the script , The value defined by the internal variable is in the current she ll;

7. Function recursion: the factorial of n, calling itself, and looping all the time.

Exercise: Find the factorial

#!/bin/bash
# Author: cqy
# Blog: https://www.cnblogs.com/cqyyyyy/
# Time: 2019-07-28 22:28:15
# Name: Factorial.sh
# Version: v1.0
# Description: This is a Script.

fac(){
        if [$1 -lt 1 ]; then
                echo "1"
        else
                echo $[$1*$(fac $[$1-1])]
        fi
}
fac 5

#!/bin/bash
# Author: cqy
# Blog: https://www.cnblogs.com/cqyyyyy/
# Time: 2019-07-28 22:28:15
# Name: Factorial.sh
# Version: v1.0
# Description: This is a Script.

fac(){
        if [$1 -lt 1 ]; then
                echo "1"
        else
                echo $[$1*$(fac $[$1-1])]
        fi
}
fac 5

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5525 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.