Linux shell programming (detailed process)

What is a shell?

Insert picture description here

Quick start

1. The script needs to start with #! Beginning with /bin/bash, to specify the parser that executes the script
2. The script requires executable permissions
Case
1. Create a simple shell script
Insert picture description here2. Write a shell script

Insert picture description here3. Add script permissions

Insert picture description here
4. Then execute
Insert picture description here

shell variables

System variable

$HOME,$PWD,$USER,$PATH....

Insert picture description here
You can use set | more to display all variables of the current Shell
Insert picture description here

Custom variable

Basic grammar

  1. Define variable: variable = value
    2) Unset variable: unset variable
  2. Declare static variables: readonly variables, note: can not unset
    cases
    Insert picture description here
    Insert picture description here
    Declare static variables B, can not unset
    Insert picture description here
    Insert picture description here
    variable definition rules
 1.变量和java有曲同工之妙,可以由字母下划线,数字,组成,但是不能以数字开头
 2.等号两侧不能有空格
 3.变量名字一般习惯大写
 

Variable assignment

Insert picture description herecarried out
Insert picture description here

Setting environment variables has been shell commented

Basic grammar

1) export 变量名=变量值 (功能描述:将shell变量输出为环境变量)
2) source 配置文件 (功能描述:让修改后的配置信息立即生效)
3) echo $变量名 (功能描述:查询环境变量的值)

Schematic diagram
Insert picture description here

Positional parameter variable

When we execute a shell script, if we want to get the parameter information of the command line, we can use positional parameter variables such as:
./myshell.sh 100 200, this is a command line to execute the shell, which can be obtained in the myshell script To parameter information
Basic syntax

$n (功能描述:n为数字,$0代表命令本身,$1-$9代表第一到第九个参数,十以上的参数,十以上的参 数需要用
大括号包含,如${
    
    10})
$* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体)
$@(功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待)
$#(功能描述:这个变量代表命令行中所有参数的个数)

Case study

Insert picture here## Title description
Insert picture description here

Predefined variables

It is the variables that have been defined by the shell designer in advance, which can be used directly in the shell script

$$ (功能描述:当前进程的进程号(PID))
$! (功能描述:后台运行的最后一个进程的进程号(PID))
$? (功能描述:最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个
变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)

Case study
Insert picture description here
Insert picture description here

Operator

Basic grammar

1) “$(( 运算式 ))”或“$[ 运算式 ]” (推荐)
2) expr m + n 注意expr运算符间要有空格
3) *, /, % 乘,除,取余

Insert picture description here
Execution
Insert picture description hererequest two command line parameters [integer] and
Insert picture description here
performing

Request the sum of the two parameters [integer] of the command line

Conditional judgment

[condition] (note that there must be a space before and after the condition) If it is not empty, return true, you can use $? to verify (0 is true, >1 is false)
application example
[atguigu] return true
[] return false
[condition] && echo OK || Echo notok condition is met, execute the following statement

常用判断条件
1) 两个整数的比较
= 字符串比较 -lt 小于 -le 小于等于 -eq 等于 -gt 大于 -ge 大于等于 -ne 不等于
2) 按照文件权限进行判断
-r 有读的权限 -w 有写的权限 -x 有执行的权限
3)按照文件类型进行判断
-f 文件存在并且是一个常规的文件 -e 文件存在 -d 文件存在并是一个目录

Insert picture description hereInsert picture description here

Process control (emphasis)

if 判断
if [ 条件判断式 ];then
程序
fi
或者
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
fi

Note: (1) [Conditional judgment formula], there must be a space between the brackets and the conditional judgment formula (2) The second method is recommended

Insert picture description here

Insert picture description here

case process

case $变量名 in
"值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac

Insert picture description here
test
Insert picture description here

for loop

Basic Grammar 1

for 变量 in 值123do
程序
done

Insert picture description here
Insert picture description here
In fact, $@ is used more, because each parameter in the parameter set can be processed.
Basic syntax 2

for (( 初始值;循环控制条件;变量变化))
do
程序
done

Insert picture description hereInsert picture description here

while loop

while [ 条件判断式 ]
do
程序
done

Insert picture description here
Insert picture description here
Read console input #
read(option) (parameter)
option:

-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了。。 参数
参数
变量:指定读取值的变量名

Case 1: Read the console and enter a num value
Insert picture description here

Insert picture description hereFunction#
contains system built-in functions and custom functions, here is a brief introduction to two commonly used built-in functions

basename
功能:返回完整路径最后/ 的部分,常用于获取文件名
basename pathname [suffix]
basename string [suffix] (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’) 字符,然后将字符串
显示出来
选项: suffix为后缀,如果suffix被指定了,basename会将pathname或string中的suffix去掉

Insert picture description here
dirname
function: return to the last part of the full path, usually used to return the path part
Insert picture description here

Custom function

[function] funname[()]
{ Action; [return int;] } Directly write the function name when calling: funname [value] Case 1: Calculate the sum of two input parameters, getSum



Insert picture description here
Insert picture description here

Database backup script

Insert picture description here

Insert picture description here

#!/bin/bash
# 完成数据库的定时备份
# 数据库备份的路径
BACKUP=/opt/module/data
# 数据库备份文件名
BACKUP_FILENAME=$(date "+%Y-%m-%d_%H-%M-%S")
# echo $BACKUP_FILENAME 测试文件名是否ok
echo "=============开始备份中============"

echo "=============备份的文件名为${BACKUP}/${BACKUP_FILENAME}.tar.gz=========="
# mysql主机名
HOST=localhost # 远程备份写对应的ip地址即可
# 用户名
DB_USER=root
# 密码
DB_PASSWORD=123456
# 备份数据库的名称
DATABASE=hivef.sql
# 创建备份的路径:如果存在就什么都不做;如果不存在就创建
[ ! -e "$BACKUP/$BACKUP_FILENAME" ] && mkdir -p "$BACKUP/$BACKUP_FILENAME"
# 执行mysql 的备份命令
mysqldump -u${
    
    DB_USER} -p${
    
    DB_PASSWORD} --host=${
    
    HOST} ${
    
    DATABASE} | gzip >
${
    
    BACKUP}/${
    
    BACKUP_FILENAME}/${
    
    BACKUP_FILENAME}.sql.gz

Guess you like

Origin blog.csdn.net/qq_43674360/article/details/111315783