• SHELL基础编程


    SHELL基础

    • shell是用户与linux内核之间的解释器
    • shell环境准备,本实验需要用到一台虚拟机A即可

    在这里插入图片描述

    查看有哪些解释器

     [root@som ~]# cat /etc/shells                  #查看shell解释器
    
    • 1

    使用usermod修改用户解释器

    [root@som ~]# usermod -s /bin/tcsh test		#使用usermod指令修改用户解释器
    [root@som ~]# grep test /etc/passwd			#从/etc/passwd过滤test用户信息
    
    • 1
    • 2

    BASH基本特性

    • 查看
      [root@som ~]# ls #数据有颜色区分
    • 重定向
      在这里插入图片描述
    • 覆盖重定向
    [root@som ~]# ls  >  a.txt
    [root@som ~]# cat  a.txt
    
    • 1
    • 2
    • 追加重定向
    [root@som ~]# ls  >>  a.txt
    [root@som ~]# cat a.txt 
    
    • 1
    • 2
    • 显示一个错误的文件
    [root@som ~]# ls xxyyzz.txt
    ls: 无法访问xxyyzz.txt: 没有那个文件或目录
    [root@som ~]# ls xxyyzz.txt > b.txt                     #失败,> 收集正确信息
    ls: 无法访问xxyyzz.txt: 没有那个文件或目录
    [root@som ~]# ls xxyyzz.txt  2>  b.txt                  #正确,2> 收集错误信息
    [root@som ~]# cat b.txt 
    ls: 无法访问xxyyzz.txt: 没有那个文件或目录
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 收集正确和错误的信息
    [root@som ~]# ls  a.txt  xxyzz.txt  >  b.txt
    [root@som ~]# ls  a.txt  xxyzz.txt  &>  b.txt           #收集所有信息
    [root@som ~]# cat b.txt 
    ls: 无法访问xxyzz.txt: 没有那个文件或目录
    a.      txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 管道
    [root@som ~]# ifconfig | head -2                      #查看ip信息前两行
    
    • 1

    在这里插入图片描述

    • 快捷键与Tab键补齐,常见快捷键如表所示
      在这里插入图片描述

    shell脚本的设计与运行

    什么是shell脚本

    • Shell脚本是一种自动化执行任务的脚本语言,可以帮助我们完成日常任务,比如文件管理、进程管理等。
    • 脚本可以理解为功能性文件

    编写问世脚本

    [root@som ~]# mkdir -p /root/shell/day0{1..2}
    [root@som ~]# vim /root/shell/day01/first.sh
    echo "Hello World"
    [root@som ~]# chmod  +x /root/shell/day01/first.sh          #添加执行权限
    [root@som ~]# /root/shell/day01/first.sh                    #绝对路径形式执行
    Hello World
    
    [root@som ~]# cd /root/shell/day01/		  	  #切换目录
    [root@som day01]# ./first.sh                  #相对路径执行
    Hello World
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    脚本格式规范

    • 脚本声明(需要的解释器、作者信息等)
    • 注释信息(步骤、思路、用途、变量含义等)
    • 可执行语句(操作代码)
    优化刚刚的first.sh脚本
    [root@som day01]# vim /root/shell/day01/first.sh
    #!/bin/bash                            #指定解释器
    #This a test program for shell.        #这是一个测试性的程序
    echo "Hello World"
    [root@som day01]# ./first.sh		   #执行脚本
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    执行shell脚本

    执行脚本的多种方式
    在这里插入图片描述

    方法一

    脚本在执行的时候要有执行(x)权限,否则会报错

    [root@som day01]# chmod -x first.sh 
    [root@som day01]# ./first.sh                     #报错
    -bash: ./first.sh: 权限不够
    [root@som day01]# /root/shell/day01/first.sh     #报错
    -bash: /root/shell/day01/first.sh: 权限不够
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方法二

    不需要文件有可执行权限,指定解释器执行脚本

    [root@som day01]# sh first.sh       		#指定sh来执行first.sh
    [root@som day01]# bash first.sh  			#指定bash解释器执行first.sh
    
    • 1
    • 2

    实验

    [root@som day01]# vim tmp.sh				#编写tmp.sh
    #!/bin/bash
    exit
    [root@som day01]# sh tmp.sh        			#指定运行脚本,没有关闭终端
    
    [root@som day01]# vim tmp.sh				#编写tmp.sh
    #!/bin/bash
    exit
    [root@som day01]# source  tmp.sh			#执行tmp.sh,会关闭终端
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    总结:

    • 指定解释器会新开子进程
    • 使用source不会新开子进程

    变量

    自定义变量

    • 环境变量(变量名通常大写,有操作系统维护)

    • 位置变量(bash内置变量,存储脚本执行时的参数)

    • 预定义变量(bash内置变量,可以调用但是不能赋值或修改)

    • 自定义变量(用户自主设置)
      定义变量

    • 可以是数字,字母,下划线

    • 变量名不能使用特殊符号,会报错

    • 不能以数字开头

    查看变量

    • echo ${变量名}
    • echo $变量名

    定义变量

    [root@som ~]# a=11
    [root@som ~]# echo $a           	#调用变量,查看变量的值
    [root@som ~]# a=33              	#变量名已经存在,再次赋值,里面的内容会被覆盖
    [root@som ~]# echo $a			 	#调用变量,查看变量的值
    
    [root@som ~]# a)=11					#变量包含特殊符号,所以定义失败
    -bash: 未预期的符号 `)' 附近有语法错误	
    [root@som ~]# 3a=33					#变量数字开头,所以定义失败
    bash: 3a=33: 未找到命令...
    
    [root@som ~]# a_0=11				#没有违规,所以成功
    [root@som ~]# _a=11					#没有违规,所以成功
    [root@som ~]# _0=11					#没有违规,所以成功
    
    [root@som ~]# x=CentOS
    [root@som ~]# echo $x           	#成功
    [root@som ~]# echo ${x}        		#成功
    若想要显示CentOS7.9
    [root@som ~]# echo $x7.9          	#失败,会显示.9,此时是把$x7看成一个变量名
    加上{}可以成功
    [root@som ~]# echo ${x}7.9			#输出CentOS7.9
    [root@som ~]# echo ${x}7.6			#输出CentOS7.6
    取消变量
    [root@som ~]# unset x					#取消变量
    [root@som ~]# echo $x
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    环境变量

    • 存储在/etc/profile或~/.bash_profile
    • 命令env可以列出所有环境变量
    • 环境变量通常是大写字母
    [root@som ~]# echo $PATH             	#命令搜索的路径变量
    [root@som ~]# echo $PWD             	#返回当前工作目录
    /root
    [root@som ~]# echo $USER            	#显示当前登录的用户
    root
    [root@som ~]# echo $UID               	#显示当前用户的uid
    0
    [root@som ~]# echo $HOME          		#显示当前用户的家目录
    /root
    [root@som ~]# echo $SHELL           	#显示当前的SHELL
    /bin/bash 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    位置变量

    • 存储脚本时执行的参数
    • $1 $2 $3 …$9 ${10} ${11} … #从10开始位置变量需要加{}
    [root@som ~]# vim /root/shell/day01/vars.sh
    #!/bin/bash
    echo $1
    echo $2
    echo $3
    
    [root@som ~]# chmod +x /root/shell/day01/vars.sh 
    [root@som ~]# /root/shell/day01/vars.sh aa bb cc       #执行脚本,传递参数
    aa
    bb
    cc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    预定义变量

    • 用来保存脚本程序的执行信息,可以直接使用这些变量,但是不能为这些变量赋值
      在这里插入图片描述
      $?:执行上一条命令的返回状态,0为正确,非0为错误
    [root@som ~]# ls /etc/hosts				#执行命令成功
    /etc/hosts
    [root@som ~]# echo $?           		#返回值为0,正确
    0
    
    [root@som ~]# ls /xxxxxyyyy     		#执行命令错误    	
    ls: 无法访问/xxxxxyyyy: 没有那个文件或目录
    [root@som ~]# echo $?					#返回值为非0,失败
    2
    
    其他几个预定义变量的测试
    [root@som7 ~]# chmod  +x /root/shell/day01/pre.sh
    [root@som7 ~]# /root/shell/day01/pre.sh  a b c d
    /root/shell/day01/pre.sh
    46608
    4
    a b c d
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    变量的扩展运用

    多种引号的区别

    双引号的应用

    • 使用双引号可以界定一个完整字符串。
    [root@som ~]# touch a b c            	#创建了三个文件
    [root@som ~]# touch "a b c"           	#创建1一个文件
    [root@som ~]# ls -l
    
    [root@som ~]# rm -rf  a b c           	#删除三个文件
    [root@som ~]# rm -rf  "a b c"       	#删除一个文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    单引号的应用

    • 界定一个完整的字符串,并且可以实现屏蔽特殊符号的功能。
    • 当双引号里面有变量时,会被扩展出来,也就是会取变量的值
    [root@som ~]# hi="world"
    [root@som ~]# echo "$hi"               #成功
    world
    [root@som ~]# echo '$hi'               #失败,当成一个字符串
    $hi
    当没有特殊符号时,单引号和双引号的含义是一样的
    
        [root@som ~]# touch "a b c"			
        [root@som ~]# touch 'c d e'
      
    练习单引号和双引号的区别
    [root@som ~]# echo "$USER id is $UID"	#调用变量
    root id is 0
    [root@som ~]# echo '$USER id is $UID'	#不调用变量
    $USER id is $UID
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    反撇号或$()的应用

    • 使用反撇号``或$()时,可以将命令执行的标准输出作为字符串存储,因此称为命令替换。
    [root@som ~]# grep root /etc/passwd
    [root@som ~]# test=`grep root /etc/passwd`		#定义变量,内容为命令输出结果
    [root@som ~]# echo $test
    
    [root@som ~]# test2=$(grep root /etc/passwd)	#定义变量,内容为命令输出结果
    [root@som ~]# echo $test2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    read命令定义变量

    • 使用read命令从键盘读取变量值
      • -p: 指定提示信息
      • -s: 屏蔽输入(键盘输入内容,在屏幕上不显示)
      • -t: 可指定超时秒数(指定秒数不输入,直接退出)

    read基本用法

    • 执行后从会等待并接受用户输入(无任何提示的情况),并赋值给变量:
    [root@som ~]# read iname					#定义变量iname
    123											#从键盘输入123作为iname的值
    [root@som ~]# echo $iname					#输出变量iname
    123
    
    虽然可以赋值。但是屏幕上没有任何提示信息,在未来写脚本的时候不太方便,可以加上-p选项,给出提示
    
    [root@som ~]# read -p "请输入用户名:" iname	#定义变量
    请输入用户名:tom
    [root@som ~]# echo $iname					 #输出变量
    tom
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    案例:

    创建一个脚本,通过read定义变量创建用户,更改密码
    
    [root@som ~]# vim /root/shell/day01/read.sh
    #!/bin/bash
    read -p "请输入用户名:" name
    read -p "请输入密码:"   pass
    useradd $name
    echo "$pass" | passwd --stdin $name
    
    [root@som ~]# chmod +x /root/shell/day01/read.sh
    [root@som ~]# /root/shell/day01/read.sh 
    请输入用户名:user2
    请输入密码:a
    更改用户 user2 的密码 。
    passwd:所有的身份验证令牌已经成功更新。
    
    但是此时密码是名为显示的,不安全,可以使用-s参数,不显示终端输入的信息
    [root@som ~]# vim /root/shell/day01/read.sh
    read -p "请输入用户名:" name
    read -s -p "请输入密码:"   pass
    useradd $name
    echo "$pass" | passwd --stdin $name
    
    [root@som ~]# /root/shell/day01/read.sh 
    请输入用户名:user3
    请输入密码:
    更改用户 user3 的密码 。
    passwd:所有的身份验证令牌已经成功更新。
    
    [root@som ~]# read -t 3 iname               #3秒不输入直接退出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    字符串条件测试

    • 是否为空 [ -z 字符串 ]
      是的话返回’0’,否则返回非0
      -等于 [ 字符串1 == 字符串2 ]
    • 变量和常量的判断
    [root@som ~]# [ $USER == root ]			#环境变量USER的值是root吗
    [root@som ~]# echo $?
    0
    [root@som ~]# [ $USER == tom ]			#环境变量USER的值是tom吗
    [root@som ~]# echo $?
    1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 不等于[ 字符串1 != 字符串2 ]
    [root@som ~]# [   $USER != tom   ]			#环境变量USER的值不是tom
    [root@som ~]# echo $?
    0
    
    • 1
    • 2
    • 3

    整数值比较

    格式:[ 整数值1 操作符 整数值2 ]

    • -eq:等于
    • -ne:不等于
    • -gt:大于
    • -ge:大于等于
    • -lt:小于
    • -le:小于等于

    参与比较的必须是整数(可以调用变量),比较非整数值时会出错:

    运算

    四则运算:+ - * /
    
    求模取余:%
    [root@som ~]# echo $[1+1]					#计算1+1
    [root@som ~]# echo $[10-2]					#计算10-2
    [root@som ~]# echo $[2*2]					#计算2*2
    [root@som ~]# echo $[6/3]					#计算6/3
    [root@som ~]# echo $[10%3]					#取10/3的余数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    变量计算

    [root@som ~]# a=10
    [root@som ~]# b=20
    [root@som ~]# echo $[a+b]					#计算变量a+变量b
    
    • 1
    • 2
    • 3

    常量与常量的比较

    小于
    [root@som ~]# [ 3 -lt 8 ]
    [root@som ~]# echo $?
    0
    
    大于
    [root@som ~]# [ 3 -gt 2 ]
    [root@som ~]# echo $?
    0
    
    等于
    [root@som ~]# [ 3 -eq 3 ]
    [root@som ~]# echo $?
    0
    ## 文件状态的测试
    ```shell
    1.  -e 判断对象是否存在(不管是目录还是文件),存在则结果为真
    [root@som ~]# [ -e /etc ]
    [root@som ~]# echo $      返回0表示存在
    
    2. -d 判断对象是否为目录(存在且是目录),是则为真
    [root@som ~]# [ -d /etc/hosts ]
    [root@som ~]# echo $        返回0表示存在
    
    3. -f 判断对象是否为文件(存在且是文件)是则为真
    [root@som ~]# [ -f /etc/hosts ]
    [root@som ~]# echo $?		返回0表示存在
    
    4. -r 判断对象是否可读,是则为真
    5. -w 判断对象是否可写,是则为真
    6. -x 判断对象是否具有可执行权限,是则为
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    一文带你走进“PyQt5”
    工作记录之策略模式
    运行手写数字识别例程出现cuDNN Error: cudnnConvolutionForward failed
    CSS基础
    k8s-----25、资源调度-ResourceQuota资源配额、资源限制limitrange、服务质量QoS
    网络爬虫-----初识爬虫
    双机热备与数据备份的关系说明一二
    时间轴_合成生物学
    Codeforces Round #821 (Div. 2) A~C(模拟、构造)
    Microsoft Dynamics 365 CE 扩展定制 - 8. DevOps
  • 原文地址:https://blog.csdn.net/m0_72568513/article/details/133951897