Basic knowledge of Shell (the script header is automatically added to the shell, and the three ways of running the shell)

1. What is a shell:

The shell of the command interpreter in the script
refers to the software (command parser) that "provides the user with an operation interface". It is similar to command.com under DOS and cmd.exe later. It receives user commands and then calls the corresponding application. The default shell in Linux is: bash

2. The meaning of shell script:

2-1: Record the execution process and execution logic of the command for repeated execution in the future
2-2: Script can process the host in batches
2-3: Script can process the host regularly

3. The first command executed in the script:

#!/bin/bash  ##可以通过 ps f 来查看进程树(查看运行脚本等解释器)

The "#!" character at the beginning is also called a magic number. When executing a bash script, the kernel will confirm which program interprets the contents of the script according to the interpreter behind the "#!";
common shell interpreters:
Linux Commonly used interpreters are: bash, sh, csh, tch, etc.
Commonly used interpreters in UNIX are: sh, kcsh, etc.

4. The script header is automatically added in the shell:

Among them, autocmd BufNewFile means to call the WESTOSFILE() module in a new script (ending with .sh .script). The
Insert picture description here
second line of autocmd means that the Tab key is set to 4 spaces by default in sh:
Insert picture description here

Execution effect:
Insert picture description here

5. Three operating modes of shell:
Insert picture description here

The first method is as follows: The specified interpreter in w.sh is bash, and after executing the script with sh w.sh, the system opens a subshell in the current shell, the type is sh:
Insert picture description here

The second way is as follows: source w.sh, use the current shell to directly execute the script without opening a new interpreter:
Insert picture description here

The third method is as follows: After the execution permission is given, execute with the path, a new interpreter will be opened in the system, and the type is the type specified in the script magic number:
Insert picture description here
5. Debug the script:

sh -x /mnt/westos.sh

+	##运行指令
不带+	##命令运行的输出

Insert picture description here
6. Script exercises:

6-1: ip_show.sh The network card displays the current ip address:
Insert picture description here
6-2: host_messages.sh displays the name and ip of the current host,
hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root
Insert picture description here

6-3: clear_log.sh can clear the log after executing this script
Insert picture description hereawk'/dsfs/ {print }'/etc/passwd means to filter the keyword dsfs in /etc/passwd/

Guess you like

Origin blog.csdn.net/lb1331/article/details/111569191
Recommended