Linux learning--review shell script development

The script is to compose a script file to execute the complex execution process through logic code.

A simple script analysis description: Use a shell script to view the current time and who is logged into the system.

#!/bin/bash

# 查看系统时间和登录的用户
echo "The time and date are :"

date

echo ""

echo "who's logged into the system :"

who

Script analysis:

1. #!/bin/bash is used to specify the interpreter of the script file.

2. # xxxxx is used to write script comments, and the interpreter does not execute it.

3. echo "xxx" is used to output a piece of text, similar to printf, the output function

4. date/who is an external variable of Linux, which is also a Linux command, used to obtain system information, such as date to obtain the current time, and who is used to obtain the current user

Execute the script file: bash file name.sh, the result is as shown below 

echo, escape character, single quote, double quote, backtick .

1. echo is used to output characters and can identify special variables

2. The escape character \ is used to make the $ symbol output as it is, which means to restore the original appearance of the symbol and will not be interpreted as a special meaning.

3. Double quotes will identify special variables

4. Single quotes will not recognize special variables

5. Backticks will keep the result.

The variables in the shell will be given their values ​​when they are defined, and the variables in the script will disappear or be saved after the shell is executed, depending on the way the script is executed.

When using bash and sh to execute, the subshell process is started to run. Variables are also loaded in the subshell, and when the subshell exits, the variables disappear.

When using source and ./ to execute scripts, variables are loaded in the current shell environment and scripts are executed.

Note that the acquisition of variable values ​​​​to add a dollar sign.

Linux, substitution quotes for shell variables

A major feature of the shell is that the results can be extracted again from the execution results of the commands , so it is suitable for scripting.

1、$()  

2. `` backtick

Arrangement of special symbols in linux

1. ${var} is used to fetch variable results, similar to $variable

2. $() executes the command in parentheses, and takes out the execution result of the command

3. `` has the same meaning as $().

4. () Open the subshell to execute the command result

5. $vars is used to retrieve variable results

 

 

 

Guess you like

Origin blog.csdn.net/qq_50929489/article/details/127382293