1. shell
How does the Linux system operate computer hardware CPU, memory, disk, monitor, etc. [ Reference ]?
Answer: Use the Linux kernel to operate the computer hardware
- By writing shell commands and sending them to the Linux kernel for execution and operating computer hardware, shell commands are a bridge for users to operate computer hardware;
- Shell is a command, similar to the Windows system Dos command;
- The shell is a programming language. The shell contains variables, functions, logical control statements, etc.
1.1 shell running process
When the user issues an instruction to the operating system, the instruction is actually told to the shell, which is interpreted by the shell and processed by the kernel to take corresponding actions. The system's responses and output information are also processed by the shell and then displayed on the user's screen.
1.2 shell parser
View the shell parsers supported by centos on linux system
cat /etc/shells
The shell parser (bash) currently used by default in Linux systems :
echo $SHELL
Meaning: Print out the shell parser type used by the current system environment
- echo is used to print output data to the terminal
- $SHELL is a globally shared reading parser type environment variable. Global environment variables are variables that can be read by all Shell programs.
2. Shell script
A Shell text file written through a Shell command or programming language is a Shell script, also called a Shell program.
Function: Improve the management efficiency of the Linux system through Shell commands and programming languages.
2.1 Shell script writing specifications
- A shell script file is a text file, and it is recommended that the suffix end with .sh;
- The first line needs to set the type of shell parser, syntax:
#!/bin/bash
- Comment
single line comment
# hello
Multi-line comments
:<<!
hello world
hello
- implement
sh command execution method (essentially using the Shell parser to run the script file): sh script file
bash parser execution method (essentially using the Shell parser to run the script file): bash script file
Path execution method ( the script file needs to have executable permissions to execute itself, otherwise it cannot be executed ): ./script file
chmod a+m: Add execution permissions to the file to all users
a
: Represents all users, including the owner of the file, members of its group, and other users+m
: means adding (increasing) execution permissionschmod +m
andchmod +x
are equivalent, they are both used to set a file as executable
The way to execute a script file with sh or bash is to directly use the Shell parser to run the script file without executable permission;
The only path method is to execute the script file itself and requires executable permissions.
3. Case
3.1 shell script hello world
1 Create shell script file
touch helloworld.sh
2 Edit sh script
vim helloworld.sh
#!/bin/bash
echo "hello world"
3 execution
3.2 Shell script executes multiple commands
Known directory /root/itheima directory, execute the batch.sh script to create a one.txt in the /root/itheima/ directory, and add the content "Hello shell" to the one.txt file.
#!/bin/bash
touch /root/itheima/one.txt
echo "hello shell" >> /root/itheima/one.txt
View the contents of one.txt
cat one.txt