Table of contents
2. Shell variables and assignments
1. Single quotes, double quotes, backticks
3. Greater than > and less than < (redirection)
(2) Application (when the descriptor is on the right, the & symbol should be added)
(3) The difference between > and >>
1. Calculate the number of characters
2. Delete the characters on the left side of the string
3. Delete the characters on the right side of the string
1. Basic knowledge
- The shell is an analytical language and does not require a compiler to compile
- #! /bin/bash: Indicates compiling with the /bin/bash tool
2. Shell variables and assignments
1. Pay attention
(1) Shell has no spaces on both sides when assigning values
(2) When the right side of the shell is a word group, there must be double quotation marks
(3) The format of the variable is the same as that of the C language
2. Types of variables
3. Examples
hello.sh中
echo "参数个数:"$# //3
echo "所有参数@" $@ //123 hi hhh
echo "所有参数*" $* //123 hi hhh
echo "上一条shell命令的返回值*" $? // 0
echo "第三个参数*" $3 //hhh
output on the command line
./hello.sh 123 hi hhh
3. Special symbols
1. Single quotes, double quotes, backticks
2. Pipeline
ls -l | wc, ls -l collects the information of the files in the current directory, and then sends it to the pipeline, wc reads it from the pipeline, calculates the number of lines, the number of words and the total characters
3. Greater than > and less than < (redirection)
(1) common sense
When the process is running, the system will open three files, which are standard input (0), standard output (1) (the display is read from descriptor 1), standard error (2)
(2) Application (when the descriptor is on the right, the & symbol should be added)
ls -l 1>a.txt |
Originally, the command was to be output to descriptor 1, and then output to a.txt after redirection |
echo 0<b.doc |
Originally read files from the keyboard, but now read data from b.doc |
echo “hello world” 1>&2: |
Hello world was originally supposed to be input to descriptor 1, but it has now been output to descriptor 2. That is, output a sentence to the standard error device |
cat > name |
Get information from standard input and write directly to the file name |
Cat >name <a.txt |
Take the content of a.txt as input, then > output to name |
(3) The difference between > and >>
>, the file exists, delete and recreate
>>, append if the file exists
Four, string operation
1. Calculate the number of characters
time="today is `date`"
echo " Number of characters in time: ${#time }"
2. Delete the characters on the left side of the string
path="/etc/rc0.d/Z20openbsd-inetd"
level=${ path#/etc/rc[0-9].d/[AZJ]}
echo $level (output: 20openbsd-inetd )
3. Delete the characters on the right side of the string
path="/etc/rc0.d/Z20openbsd-inetd"
vel = $ { level %% [ a - zA - Z ] * }
echo $vel (output: /etc/rc0.d/Z20 ) (special symbols are not considered, such as "-")
5. Test statement
Six, logic statement
1、if-elif
Are the strings equal |
if [ "$var1" = "$var2" ] |
whether the file exists and is readable |
if test -e name && test -r name |
whether the file exists and is readable |
if [ -e name ] && [ -r name ] |
if [ -e file ] && [ -r file ] then cat file # If the file exists and is readable, display the contents of the file elif [ - e file ] #judging whether the file exists then chmod u + r file #Let the user's permission add readable cat file # If the file exists but cannot be read, then display its content after adding read permission else touch file # If the file does not exist, create the empty file fi |
2. Multiple branches
read VAR #Receivea user input from the keyboard case $ VAR in #judgingthe value entered by the user$VAR 1 ) echo "one" #Ifthe value of$VARis1, then display one ;; #Eachbranch must end with a double semicolon (except the last branch) similar tobreak 2) echo "two" ;; * ) echo "unknown" #Theasterisk*isa wildcard in the Shell esac |
Precautions: 1. The value of the variable VAR is actually a string, so 1) in the above code can also be written as "1") 2. The entire case structure must end with esac. |
Problem: "\r": command not found There are two types of editor end symbols
|
3、for循环
files=`ls` # 在当前目录下执行 ls,将所有的文件名保存在变量 files 中 for a in $files # 循环地将 files 里面的每个单词赋给 a,赋完则退出循环 do if [ -f $a ] # 如果文件$a 是一个普通文件,那么就计算他的行数 then wc -l $a fi done |
4、ifeq
ifeq ($(OS), Linux)
$(info Running on Linux) //如果成立,则输出Running on Linux)
else
$(warning This Makefile is intended for Linux)//如果不成立,则输出警告:This Makefile is intended for Linux
endif.PHONY: all
all:
@echo "Building target..."
# 其他构建命令和规则