Linux Shell Commands

What is Shell


        A shell is a program used to manage and execute computer commands and can be considered either a command language or an interpreted programming language. The Shell is the bridge between the user and the operating system, allowing the user to operate the computer using simple commands. A Shell program usually consists of a command interpreter and a file manager. Users can interact with the Shell program through the command line interface.
        Shell programs can be written in different programming languages, such as c, c++, python, etc., but they are most commonly written in c language. Shell programs can run in a terminal window or in a graphical user interface.
        Shell programs provide many functions, such as creating files, modifying files, deleting files, renaming files, moving files, viewing file contents, modifying file permissions, connecting to other computers, etc. Shell programs also support redirection and pipe combination, allowing users to redirect the input and output of commands to different files, or to combine the output of multiple commands together.
        The Shell program also provides functions such as environment variables and alias commands, allowing users to easily manage and execute computer commands. Through the Shell program, users can quickly operate the computer and improve work efficiency.

Why use Shell

        Because it’s convenient~~~

        cmd is the command line interpreter for Windows operating systems, which uses command-based syntax and a text interface to execute commands. It is mainly used to execute Windows system commands and batch files. It has certain scripting capabilities, but it is not powerful and flexible enough.

        The shell is the command line interpreter of the Unix/Linux operating system, which uses text-based syntax and programmable interface to execute commands. The shell has powerful scripting capabilities and can write complex programs and automated scripts through scripting languages, making it more flexible and powerful than cmd.

        To be fair, more people first come into contact with the Windows system. Windows provides cmd and powershell, but why do so many people use shell? The main reason is that servers are mostly deployed on servers with Unix/Linux operating systems, so the command line becomes particularly important.

study

First shell script

First of all, we first prepare a Linux environment, whether it is a Linux system, a virtual machine or a docker container. Anyway, we first create a script, test.sh

#!/bin/bash    # 这句话表示用linux指定去解析文本内容
#显示hello world!
echo hello world!

Then give the script executable permissions:

chmod +x test.sh

After execution, the words "hello world!" will be displayed.

        The first is the first line. When the system encounters a file starting with #!/bin/bash, it will think that the file is a script that needs to be executed using the bash interpreter, and then it will follow the path to find the bash interpreter. , and use that interpreter to execute the script. In Linux, in addition to the bash shell, there are many versions of shells, such as zsh and dash.

        Then the echo on the second line is what it means to display.

variable

        In shell programming, variables are identifiers used to store values, which can be strings, numbers, Boolean values, etc. Here are some common shell variable types:

  • Environment variables: These variables are inherited from the environment variables when the shell starts and can be exported to the child process through the export command. Environment variables can be used to pass parameters, configuration files, etc.
  • Local variables: These variables are defined in a script or command and are only valid within the current shell instance. Local variables can be used to save temporary data, function parameters, etc.
  • Shell variables: These variables are special variables set by the shell program, some of which are environment variables and some of which are local variables. Shell variables ensure the normal operation of the shell. For example, the $HOME variable represents the current user's home directory.

        When defining a variable, do not add the dollar sign to the variable name, such as variable. If you want to use a variable, you need to precede the variable name with a dollar sign, such as $variable. The curly braces outside the variable name are optional, you can add them or not. The curly braces are added to help the interpreter identify the boundaries of the variables. It is recommended to add curly braces to all variables. This is a good programming habit.

#!/bin/bash
echo $PATH
hello=" world"
echo $hello

echo ${hello}

For example, we echo $PATH to display the environment variable, and then we define the local variable hello. At this time, $ is not added, but if we want to use it, we need to use $hello. But if you want to get used to it, it's better to add a curly brace.

And if we want to define a variable that is the result of the previous instruction, it is also achieved through $ or `xxx`, for example

#!/bin/bash

path=$(pwd)

files=`ls -al`

echo current path: $path

echo files: $files

We use $(pwd) to represent the result of executing the pwd instruction, which is the current absolute path, and save it in the path variable.

We let files be assigned the result of running ls -al, which is the instruction to list all files.

The third line simply displays $path. There is no operation here. It just takes the path variable and displays it.

The fourth line, $files is displayed here, so the display result will list all files.

string

        We often use strings when writing shell scripts, because many things need to be named and require characters. In most high-level languages, single quotes and double quotes have different meanings. For example, in C#, single quotes are characters and double quotes are strings. In some high-level languages, they are the same, such as single quotes and double quotes in Python. The meaning is the same.

apostrophe

        At this time we need to use the shell, the difference between single quotes and double quotes will be relatively large. In shell scripts, single quotes represent strings. Whatever is written in single quotes will be output as is and will not be run again. The variables are invalid. And single quotes cannot be nested within single quotes. for example

hello="world"

greet='Hello ${hello}'

echo $greet

What do you think the output will be? The output is Hello ${hello}

Double quotes

        Double quotation marks are different. In double quotation marks, the variable name is prefixed with $, the backquote character and the escape character will be executed, but other special characters will be turned off.

hello="world"

greet2="Hello ${hello}"

greet3="'Hello ${hello}'"

echo $greet2


第一次输出都是 Hello World

第二次输出是 'Hello World',这时候单引号的功能关闭了

Substrings, length and search

root@93da3b39d1f0:/# string="abcdefg"

#获取字符串的子串
root@93da3b39d1f0:/# echo ${string:1:4}
bcde

#获取字符串的长度
root@93da3b39d1f0:/# echo ${#string}
7

#查找字符串中d的位置
root@93da3b39d1f0:/# echo `expr index "$string" d`
4

#查找字符串那种e的位置
root@93da3b39d1f0:/# echo `expr index "$string" e`
5

#我们如果查找一个不存在的,这时候只匹配第一个
root@93da3b39d1f0:/#echo `expr index "$string" eag`
5


parameter

        Generally speaking, parameters are required by a program. For example, I have a file called test.sh. If I directly pass in the parameters and operate according to my parameters, then I don’t need to understand the requirements of the script at all. I only need the parameters. .

参数长度为:$#
第一个参数为:$1  
第二个参数为:$2  
第三个参数为:$3  

In addition to the parameter processing in the above example, there are several special characters used to handle parameters:

  • #Number of parameters passed to the script
  • $* displays all parameters passed to the script as a single string. If "" is enclosed in """, all parameters will be output in the form of "12n".
  • $? Current process ID number where the script is running
  • $! ID number of the last process running in the background
  •  Same as $?, but used in quotes and returns each parameter in quotes. If "@" is enclosed in """, all parameters will be output in the form of "1""2""n".
  • - Displays the current options used by the Shell, which has the same function as the set command.
  • ? Display the exit status of the last command. 0 indicates no errors, any other value indicates an error.

        To sum up, Shell parameters can provide input to the script, so that the script can perform corresponding operations based on these parameters. By mastering the usage of Shell parameters, we can write and run Shell scripts more flexibly.

process control

The if else statement uses if and fi as the beginning and end, and then and else as the conditional transition in the middle.

if [ -f "file.txt" ]; then  
 echo "文件存在"  
else  
 echo "文件不存在"  
fi  

loop statement

You can use the for, while and until keywords to implement loops in Shell scripts. Among them, the for loop is used to traverse the elements in the list, the while loop is used to repeatedly execute the code when the condition is true, and the until loop is used to repeatedly execute the code when the condition is not true.

for i in {1..10}; do  
 echo $i  
done  

count=1  
while [ $count -le 10 ]; do  
 echo "计数:$count"  
 count=$((count+1))  
done  

Jump statement

You can use the break, continue and shift keywords in Shell scripts to implement jumps. Among them, the break keyword is used to jump out of the current loop, the continue keyword is used to skip the remaining part of the current loop, and the shift keyword is used to decrement the loop counter by 1.

for i in {1..10}; do  
 if [ $i -eq 10 ]; then  
   break  
 fi  
 echo $i  
done  

arithmetic operators

        Shell supports a variety of arithmetic operators, mainly used for calculating and manipulating numbers. The following are some commonly used Shell arithmetic operators:

  1. +: addition operator. For example: a=3; b=5; echo $a+$b, the output result is 8.
  2. -: Subtraction operator. For example: a=3; b=5; echo $a-$b, the output result is -2.
  3. *: Multiplication operator. For example: a=3; b=5; echo $a*$b, the output result is 15.
  4. /: Division operator. For example: a=6; b=3; echo $a/$b, the output result is 2. Note that the result of a division operation is always an integer, even if the decimal part of the input is truncated.
  5. %: Modulo (remainder) operator. For example: a=6; b=3; echo $a%$b, the output result is 0.
  6. <<: left shift operator. For example: a=5; echo $a<<2, the output result is 13. The left shift operation moves the binary number to the left by the specified number of digits, and fills the vacated bits on the right with 0s.
  7. >>: Right shift operator. For example: a=5; echo $a>>2, the output result is 1. The right shift operation moves the binary number to the right by the specified number of digits, and fills the vacated bits on the left with 0s.
  8. ^: Exclusive OR (XOR) operator. For example: a=5; b=3; echo $a^$b, the output result is 6. The result of the XOR operation is the sum of the different bits of the corresponding bits of the two binary numbers. If they are the same, it is 0, and if they are different, it is 1.
  9. &&: Logical AND operator. For example: a=0; b=1; echo $a&&$b, the output result is 0. If the result of the logical AND operation is true, 1 is returned, otherwise 0 is returned.
  10. ||: logical OR operator. For example: a=0; b=1; echo $a||$b, the output result is 1. If the result of the logical OR operation is true, 1 is returned, otherwise 0 is returned.

        These arithmetic operators are often used in shell scripts to implement simple numerical calculations and logical operations. It should be noted that Shell has weak arithmetic capabilities, and it is recommended to use professional mathematics software or programming languages ​​to complete complex mathematical operations.

Relational operators

        Shell supports a variety of relational operators, which are mainly used to compare the size of two numbers. The following are some commonly used Shell relational operators:

  1. -eq: equals operator. For example: a=3; b=5; if [ $a -eq $b ]; then echo "a is equal to b"; else echo "a is not equal to b"; fi, the output result is "a is not equal to b".
  2. -ne: not equal to operator. For example: a=3; b=5; if [ $a -ne $b ]; then echo "a is not equal to b"; else echo "a is equal to b"; fi, the output result is "a is not equal to b".
  3. -lt: less than operator. For example: a=3; b=5; if [ $a -lt $b ]; then echo "a is less than b"; else echo "a is greater than or equal to b"; fi, the output result is "a is less than b".
  4. -le: less than or equal to operator. For example: a=3; b=5; if [ $a -le $b ]; then echo "a is less than or equal to b"; else echo "a is greater than b"; fi, the output result is "a is less than or equal to b".
  5. -gt: greater than operator. For example: a=3; b=5; if [ $a -gt $b ]; then echo "a is greater than b"; else echo "a is less than or equal to b"; fi, the output result is "a is greater than b".
  6. -ge: Greater than or equal to operator. For example: a=3; b=5; if [ $a -ge $b ]; then echo "a is greater than or equal to b"; else echo "a is less than b"; fi, the output result is "a is greater than or equal to b".

        These relational operators are often used in shell scripts to perform simple size comparison operations. However, it should be noted that Shell's relational operators can only compare numbers, not strings. If you need to compare strings, you can use string operators such as ==, !=, >, <, etc.

Logical Operators


        Shell supports a variety of logical operators, which are mainly used to combine and control the execution of conditional statements. The following are some commonly used Shell logical operators:

  1. &&: Logical AND operator. Indicates that the condition is true only when both commands before and after are executed successfully. For example: command1 && command2 means that command2 will be executed only after command1 is executed successfully.
  2. ||: Logical OR operator. Indicates that the condition is true when one of the two commands before and after is executed successfully. For example: command1 || command2, which means that as long as either command1 or command2 is executed successfully, the condition is true.
  3. !: Logical NOT operator. Expresses the negation of a certain condition. For example: !command_success, indicating that the command_success condition is false.
  4. &&&: Logical AND & operator. Indicates that the condition is true only when both the preceding and following commands are executed successfully, but unlike &&, &&& will prevent the subsequent command from executing if the previous command fails. For example: command1 &&& command2 means that command2 will be executed only after command1 is executed successfully, but if command1 fails, command2 will not be executed.
  5. ||&: Logical OR & operator. It means that when one of the two commands before and after is successfully executed, the condition is true, but unlike ||, ||& will prevent the subsequent command from executing if the previous command is successful. For example: command1 ||& command2 means that as long as command1 is executed successfully, command2 will not be executed.

        These logical operators can help you build more complex conditional statements to meet different needs. For example, you can use && and || to combine multiple commands to achieve sequential execution, branch execution, etc. It should be noted that the precedence of logical operators is from left to right, calculated in the order of commands.

おすすめ

転載: blog.csdn.net/u013379032/article/details/131949066