Introduction and examples of Shell syntax: file redirection, importing external scripts

Introduction and examples of Shell syntax: file redirection, importing external scripts

1 Introduction

Shell is a powerful command line interpreter that provides rich syntax and functions. This article will combine more cases to explain in detail the file redirection in Shell syntax and the knowledge points of introducing external scripts. We'll discuss what these knowledge points are for, how to apply them correctly, and give more detailed examples.

2. File redirection

File redirection is one of the commonly used functions in Shell, which allows us to change the input source and output target when executing commands. The following are three special symbols involved in file redirection:

  • >: Redirect standard output to a file (overwrite original content).
  • >>: Append standard output to the end of the file.
  • <: Read content from a file as standard input.

2.1 Output Redirection

2.1.1 Use >the standard output of the command to import the file

Use >the symbol to redirect the standard output of the command to a file. If the file exists, it will be overwritten; if the file does not exist, it will be created.

Example :

echo "Hello, World!" > output.txt

The above example writes the string "Hello, World!" to output.txtthe file named .

2.1.2 >>Append the standard output of the command to the end of the file using

>>Append the command's standard output to the end of the file using the symbol. If the file does not exist, it will be created and written to.

Example :

echo "Goodbye, World!" >> output.txt

The above example appends the string "Goodbye, World!" to output.txtthe end of the file named.

2.1.3 Use 2>the standard error output of the command to import the file

Use 2>a symbol to redirect a command's standard error output into a file.

Example :

command_not_found 2> error.log

The above example redirects the standard error output of "command_not_found" to error.loga file named .

2.2 Input redirection

2.2.1 Using <input read from a file as the standard input of a command

Use <the symbol to read content from a file as standard input to a command.

Example :

wc -l < input.txt

The above example uses input redirection to use input.txtthe content of as wc -lthe standard input of the command to count the number of lines in the file.

2.2.2 Using <<inline input redirection in scripts

Use <<the symbol to use inline input redirection in scripts.

Example :

cat << EOF > output.txt
Hello
World
EOF

The above example feeds multiple lines of text to catthe command and redirects the results to output.txta file named . In this example, EOFthe start and end markers.

3. Import external scripts

Introducing external scripts is a commonly used technique in Shell programming, which allows us to call other scripts or functions in scripts to achieve code reuse and modularization.

3.1 Using .commands (point commands)

.External scripts can be imported using commands.

Example :

. ./external_script.sh

The above example will external_script.shimport an external script named into the current script. Note that .there is a space immediately after the command, and the ./current directory must be added before the path.

3.2 Using sourcecommands

sourceCommands can also be used to import external scripts.

Example :

source ./external_script.sh

The above example imports the script files in the current directory external_script.shinto the current script. Unlike .commands, sourcecommands can omit the preceding path ./.

4. More case presentations

4.1 File redirection case

4.1.1 Example 1: Import the standard output of a command into a file

Example :

# 将ls命令的标准输出导入到filelist.txt文件中
ls > filelist.txt

The above example outputs and redirects the list of files in the current directory to filelist.txta file named .

4.1.2 Example 2: Append the standard output of the command to the end of the file

Example :

# 使用date命令将当前日期追加到log.txt文件末尾
date >> log.txt

Each time the above example is executed, the current date will be appended to log.txtthe end of the file named.

4.1.3 Example 3: Import both standard error output and standard output into the same file

Example :

# 将stderr和stdout输出都导入到error.log文件中
command_not_found >> error.log 2>&1

The above example appends both standard error and standard output of the "command_not_found" command to error.logthe file named .

4.2 Introducing an external script case

4.2.1 Example 1: Introduce the helper.sh script in the main script and call the functions in it

main_script.sh:

#!/bin/bash
. ./helper.sh

greet "Alice"

helper.sh:

#!/bin/bash
greet() {
    
    
    echo "Hello, $1!"
}

main_script.shThe above example introduces an external script into the main script helper.sh, calls the function in it greet, and passes parameters to print the greeting.

4.2.2 Example 2: Import the configuration file in the script and read the configuration items in it

config.sh:

#!/bin/bash
username="alice"
password="123456"

main_script.sh:

#!/bin/bash
source ./config.sh

echo "Username: $username"
echo "Password: $password"

The above example demonstrates how to import a configuration file into a script config.sh, and obtain the user name and password configuration items defined in it for output.

The above is a detailed explanation and case of file redirection and introduction of external scripts in Shell syntax. File redirection provides us with the ability to flexibly control input and output, allowing us to easily handle text data and error messages. The introduction of external scripts allows us to reuse code and modularize scripts, improving the readability and maintainability of Shell scripts.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132093776