刘刚刚的个人博客

<linux就该这么学>第四章 Vim编辑器与Shell命令脚本

创建时间:2021-01-20 23:24:44
更新时间:2021-01-20 23:25:10

1. Linux中一切皆文件 2. 如果要修改一个服务,那么就是修改服务对应的配置文件 3. 想让配置文件生效需要重启服务

Vim编辑器的使用

vim包括三种模式:

  1. 命令模式
  2. 输入模式
  3. 末行模式

image-20210119231447976

# 如果文件不存在则保存时,会自动创建
vim 文件

命令模式中常用的命令:

命令作用
dd删除(剪切)光标所在整行
5dd删除(剪切)从光标处开始的5行
yy复制光标所在整行
5yy复制从光标处开始的5行
n显示搜索命令定位到的下一个字符串
N显示搜索命令定位到的上一个字符串
u撤销上一步的操作
p将之前删除(dd)或复制(yy)过的数据粘贴到光标后面
?字符串在文本中从下至上搜索该字符串
/字符串在文本中从上至下搜索该字符串
tip:

在RHEL7.5之后,如果搜索后直接保存,那么打开还是搜索结果,可以通过搜索一个不存在的结果,再保存.

末行模式中常用的命令:

命令作用
:w保存
:q退出
:q!强制退出(放弃对文档的修改内容)
:wq!强制保存退出
:set nu显示行号
:set nonu不显示行号
:命令执行该命令
:整数跳转到该行
:s/one/two将当前光标所在行的第一个one替换成two
:s/one/two/g将当前光标所在行的所有one替换成two
:%s/one/two/g将全文中的所有one替换成two

Shell脚本的编写

Shell命令有两种工作方式:

  1. 交互式:用户每输入一条就执行一条
  2. 批处理:用户编写一个完整的shell脚本,一次性执行

Shell脚本的变量定义与执行

变量定义时不需要些$,引用变量是需要写$,shell脚本一般用sh作为文件的后缀。

>>>
[root@ecs-3511-0001 ~]# vim test.sh
#!/bin/bash   #此处定义执行该脚本的解释器路径
#test Shell   #此处写该脚本的注释
pwd              # 命令
NAME='LG'        # 定义变量
echo $NAME        # 输出变量
[root@ecs-3511-0001 ~]# bash test.sh
/root
LG
[root@ecs-3511-0001 ~]# ./test.sh
-bash: ./test.sh: Permission denied
[root@ecs-3511-0001 ~]# chmod u+x test.sh
[root@ecs-3511-0001 ~]# ./test.sh
/root
LG

Shell脚本的参数接收

>>>
[root@ecs-3511-0001 ~]# cat test2.sh 
#!/bin/bash
#test2
echo "当前脚本的名称: $0"
echo "当前脚本的参数数量:$#"
echo "当前脚本的所有参数:$*"
echo "第一个参数为:$1"
[root@ecs-3511-0001 ~]# ./test2.sh one two three
当前脚本的名称: ./test2.sh
当前脚本的参数数量:3
当前脚本的所有参数:one two three
第一个参数为:one
命令意义
$0当前Shell脚本的名称
$#接收到的参数数量
$*所有的参数
$?上一次命令执行的返回值
$1,$2,$3....$n第n个位置的参数值

判断

文件测试

# 如果为真,则其结果为真,输出会显示0
[ 操作符  文件 ]

>>>
[root@ecs-3511-0001 ~]# ls
a.txt  c  test.sh  test2.sh
[root@ecs-3511-0001 ~]# [ -d /root/c ]
[root@ecs-3511-0001 ~]# echo $?
0
[root@ecs-3511-0001 ~]# [ -d /root/c1 ]
[root@ecs-3511-0001 ~]# echo $?
1
操作符作用
-d测试文件是否为目录类型
-e测试文件是否存在
-f判断是否为一般文件
-r测试当前用户是否有权限读取
-w测试当前用户是否有权限写入
-x测试当前用户是否有权限执行

逻辑运算

>>>
[root@ecs-3511-0001 ~]# ls
a.txt  c  test.sh  test2.sh
[root@ecs-3511-0001 ~]# [ -d /root/c ] && echo "真"
真
[root@ecs-3511-0001 ~]# [ -d /root/c1 ] && echo "真"
[root@ecs-3511-0001 ~]# [ -d /root/c1 ] || echo "假"
假
[root@ecs-3511-0001 ~]# [ -d /root/c ] && echo "真" || echo "假"
真
[root@ecs-3511-0001 ~]# [ -d /root/c1 ] && echo "真" || echo "假"
假
符号作用
&&当前执行结果为真,再执行后边的
\\ 当前执行结果为假,再执行后边的

整数比较运算符

[root@ecs-3511-0001 ~]# [ 10 -eq 10 ]
[root@ecs-3511-0001 ~]# echo $?
0
[root@ecs-3511-0001 ~]# [ 10 -eq 11 ]
[root@ecs-3511-0001 ~]# echo $?
1
[root@ecs-3511-0001 ~]# [ 10 -eq a ]
-bash: [: a: integer expression expected
操作符作用
-eq是否等于
-ne是否不等于
-gt是否大于
-lt是否小于
-le是否等于或小于
-ge是否大于或等于

字符串比较

[root@ecs-3511-0001 ~]# [ a = a ]
[root@ecs-3511-0001 ~]# echo $?
0
[root@ecs-3511-0001 ~]# [ a = b ]
[root@ecs-3511-0001 ~]# echo $?
1
[root@ecs-3511-0001 ~]# TEST=''
[root@ecs-3511-0001 ~]# [ -z TEST ]
[root@ecs-3511-0001 ~]# echo $?
1
操作符作用
=比较字符串内容是否相同
!=比较字符串内容是否不同
-z判断字符串内容是否为空

流程控制

if条件判断

if 条件1
then 条件1成立时的命令
elif 条件2
then 条件2成立时的命令
else 条件1、2都不成立的命令
fi

[root@ecs-3511-0001 ~]# cat test4.sh
#!/bin/bash
# 这个是用来判断分数的
NUM=$1
if [ $NUM -lt 60  ]
then echo "您没有及格"
elif [ $NUM -le 80 ] 
then echo "您的成绩良好"
elif [ $NUM -le 100 ]
then echo "您的成绩优秀"
else echo "分数不合法"
fi
[root@ecs-3511-0001 ~]# bash test4.sh 50
您没有及格
[root@ecs-3511-0001 ~]# bash test4.sh 80
您的成绩良好
[root@ecs-3511-0001 ~]# bash test4.sh 100
您的成绩优秀
[root@ecs-3511-0001 ~]# bash test4.sh 101
分数不合法

for 条件循环

for  变量 in  数据
do
    执行的命令
done

>>>
[root@ecs-3511-0001 ~]# cat  userlist.txt
root
user1
user2
user3
user4
[root@ecs-3511-0001 ~]# cat test5.sh 
#!/bin/bash
# 该脚本会读取用户列表创建用户并由用户指定密码

for UNAME in `cat userlist.txt`
do
        # 将输出内容定向到黑洞
        id $UNAME &> /dev/null
        if [ $? -eq 0 ]
        then
                echo "用户$UNAME已经存在"
        else
                read -p "请指定用户$UNAME的密码:" UPASSWD
                useradd $UNAME &> /dev/null && echo "$UPASSWD" | passwd --stdin $UNAME &> /dev/null
                if [ $? -eq 0 ]
                then echo "用户$UNAME创建成功"
                else echo "用户$UNAME创建失败"
                fi
        fi
done

[root@ecs-3511-0001 ~]# bash test5.sh
用户root已经存在
请指定用户user1的密码:123
用户user1创建成功
请指定用户user2的密码:123
用户user2创建成功
请指定用户user3的密码:123
用户user3创建成功
请指定用户user4的密码:123
用户user4创建成功
[root@ecs-3511-0001 ~]# bash test5.sh
用户root已经存在
用户user1已经存在
用户user2已经存在
用户user3已经存在
用户user4已经存在

tip:

  1. /dev/null 类似于win的回收站,但没有回收功能
  2. $(cat 文件) 与 `cat 文件 `效果一样

while循环

# do...done 中如果使用了exit可以让程序跳出循环
while true/false
do 
    命令或exit 0
done

>>>
[root@ecs-3511-0001 ~]# cat test8.sh
#!/bin/bash
#测试while
while true
do
        read -p "请输入数字,0代表结束:" UINPUT
       if [ !  $UINPUT -eq 0 ]
       then echo "您输入了$UINPUT"
       else 
               echo "结束"
               exit 0
       fi
done
[root@ecs-3511-0001 ~]# bash test8.sh
请输入数字,0代表结束:1
您输入了1
请输入数字,0代表结束:0
结束
[root@ecs-3511-0001 ~]# cat test9.sh
#!/bin/bash
#测试while
TAG=true
while $TAG
do
        read -p "请输入数字,0代表结束:" UINPUT
       if [ !  $UINPUT -eq 0 ]
       then echo "您输入了$UINPUT"
       else 
               echo "结束"
               TAG=false
       fi
done
[root@ecs-3511-0001 ~]# bash test9.sh
请输入数字,0代表结束:0
结束

case条件处理

case 变量 in
    范围1)
    命令1
    ;;
    范围2)
    命令2
    ;;
    *)
    不属于范围1和范围2时的命令
    ;;
esac



>>>
[root@ecs-3511-0001 ~]# cat test10.sh 
#!/bin/bash
# 测试case
read -p "请输入单个字符:" UINPUT
case $UINPUT in
        [0-9])
        echo "您输入的是数字"
        ;;
        [a-z]|[A-Z])
        echo "您输入的是字母"
        ;;
        *)
        echo "没有判断出来您的输入类型"
esac  
[root@ecs-3511-0001 ~]# bash test10.sh 
请输入单个字符:1
您输入的是数字
[root@ecs-3511-0001 ~]# bash test10.sh 
请输入单个字符:a
您输入的是字母
[root@ecs-3511-0001 ~]# bash test10.sh 
请输入单个字符:~
没有判断出来您的输入类型

计划任务

定时任务

# 启动任务
## 方式一:
at 时间  >>> 命令  ctrl+d 结束输入
## 方式二:
echo  ”命令“ | at 时间

# 查看任务列表
at -l

# 查看任务具体的内容
## 结果的最后一行为执行的命令
at -c 任务编号

# 删除任务
atrm 任务编号


[root@ecs-3511-0001 ~]# ll a.txt
-rw-r--r-- 1 root root 4 Jan 20 17:09 a.txt
[root@ecs-3511-0001 ~]# uptime
 17:10:58 up  7:39,  4 users,  load average: 0.03, 0.07, 0.07
[root@ecs-3511-0001 ~]# ll a.txt
-rw-r--r-- 1 root root 4 Jan 20 17:11 a.txt
[root@ecs-3511-0001 ~]# at -l
2       Thu Jan 21 12:08:00 2021 a root
[root@ecs-3511-0001 ~]# atrm 2
[root@ecs-3511-0001 ~]# at -l

周期任务

周期的任务由crontab执行,任务中的命令需要写全路径(因为crontab读取不到变量),可以使用 whereis 命令来查看命令的完整路径

# 查看或编辑周期任务
crontab -e

# 定时任务的写法,如果不需要改参数,那么需要用*代替
分 小时 天 月 星期 命令

# 编辑其它用户的周期任务
crontab -u 用户名

>>>
[root@localhost ~]# ll a.txt
-rw-r--r--. 1 root root 4 Jan 18 01:21 a.txt
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
## 文件内容如下,没分钟更新下文件的时间
*/1 * * * * /usr/bin/touch /root/a.txt

[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor prese>
   Active: active (running) since Mon 2021-01-18 00:52:14 CST; 2 days ago
 Main PID: 1419 (crond)
    Tasks: 1 (limit: 12390)
   Memory: 2.6M
   CGroup: /system.slice/crond.service
           └─1419 /usr/sbin/crond -n
[root@localhost ~]# ll a.txt
-rw-r--r--. 1 root root 4 Jan 20 23:18 a.txt

image-20210120232239876

需要启动周期任务服务,周期任务才能生效

# 查看周期任务的状态
systemctl status crond

# 重启服务
systemctl restart crond

# 加入自启动服务
systemctl enable crond
我的名片

昵称:shuta

职业:后台开发(python、php)

邮箱:648949076@qq.com

站点信息

建站时间: 2020/2/19
网站程序: ANTD PRO VUE + TP6.0
晋ICP备18007778号