Shell programming: in-depth analysis of process control and advanced applications

Table of contents

Shell process control

1. Conditional statement

2. Loop statement

Shell functions

Shell input/output redirection

Shell file contains

The file contains an example of


Shell process control

When using Shell programming, flow control is very important, it allows you to execute different commands or control the execution flow of the program according to conditions. Shell supports some basic flow control structures, including conditional statements and loop statements.

1. Conditional statement

if statement

if [ 条件 ]; then
    # 如果条件为真执行的命令
elif [ 其他条件 ]; then
    # 如果其他条件为真执行的命令
else
    # 如果所有条件都不为真执行的命令
fi

Example:

#!/bin/bash
read -p "请输入一个数字: " num

if [ $num -eq 0 ]; then
    echo "输入的数字是零"
elif [ $num -gt 0 ]; then
    echo "输入的数字是正数"
else
    echo "输入的数字是负数"
fi

2. Loop statement

for loop

for 变量 in 列表; do
    # 循环体内的命令
done

Example:

#!/bin/bash
for fruit in apple banana cherry; do
    echo "水果: $fruit"
done

while loop

while [ 条件 ]; do
    # 循环体内的命令
done

Example:

#!/bin/bash
count=1

while [ $count -le 5 ]; do
    echo "这是第 $count 次循环"
    ((count++))
done

until loop

until [ 条件 ]; do
    # 循环体内的命令
done

Example:

#!/bin/bash
count=1

until [ $count -gt 5 ]; do
    echo "这是第 $count 次循环"
    ((count++))
done

Shell functions

When you need to execute the same block of code multiple times in a shell script, you can use functions to encapsulate these codes so that they can be managed and reused more easily. In the shell, you can use the function keyword or () to define a function.

#!/bin/bash

# 定义一个简单的函数
my_function() {
  echo "这是一个自定义的Shell函数"
}

# 调用函数
my_function

1. #!/bin/bash indicates that this is a Bash script.

2. my_function() defines a function named my_function . Arguments can be included within parentheses after the function name, but in this example we are not using any.

3. In the function body, we use the echo command to print a message.

4. Finally, we call the my_function function in the body of the script. This executes the code inside the function body, which prints out the appropriate message.

#!/bin/bash

# 定义一个带参数的函数
greet() {
  local name="$1"
  echo "Hello, $name!"
}

# 调用函数,并传递参数
greet "Alice"
greet "Bob"

In this example, the greet function takes a parameter name and uses it in the message. We use the greet function twice, each time passing a different name as an argument.

Shell input/output redirection

These examples show how to use the different input/output redirection operators to process the input and output of commands. You can combine the operators and commands in these examples according to your specific needs.

1. Example of standard output redirection ( > ):

# 将ls命令的输出写入到file.txt文件中
ls > file.txt

2. Append output redirection ( >> ) Example:

# 将echo的输出追加到file.txt文件的末尾
echo "Hello, World!" >> file.txt

3. Example of standard input redirection ( < ):

# 从input.txt文件中读取内容,并使用sort命令排序
sort < input.txt

4. Pipeline ( | ) example:

# 使用ls命令列出当前目录的文件,并将结果传递给grep命令以搜索包含"example"的行
ls | grep "example"

5. Example of standard error redirection ( 2> ):

# 运行一个不存在的命令,将错误信息保存到error.log文件中
non_existent_command 2> error.log

6. Simultaneously redirect standard output and standard error ( &> or 2>&1 ) Example:

# 将命令的输出和错误信息都写入到output.log文件中
some_command &> output.log

Shell file contains

In shell scripts, you can use file includes to break up a script into multiple files to improve maintainability and code reuse. Usually, you can use the source command or the . (dot) operator to include other shell script files.

The file contains an example of

Script 1.sh:

#!/bin/bash

# 这是脚本1.sh的内容
echo "这是脚本1.sh"

Screenplay 2.sh:

#!/bin/bash

# 这是脚本2.sh的内容
echo "这是脚本2.sh"

Now, you can create a main script that includes these two files.

main script.sh:

#!/bin/bash

# 包含脚本1.sh
source 脚本1.sh

# 或者使用 . 操作符
# . 脚本1.sh

# 包含脚本2.sh
source 脚本2.sh

# 主脚本的内容
echo "这是主脚本"

# 运行脚本1.sh和脚本2.sh中的命令

In this way, you can put some common functions in a separate script file, and then include them where needed to improve the modularity and reusability of the code. Make sure the included script files have executable permissions so the shell can execute them.

 

Guess you like

Origin blog.csdn.net/m0_67906358/article/details/132647035