Linux学习笔记—shell常用指令(二)

1.test命令

知识点一:

test命令和[]等同
test "$A"=="$B"   等同 ["$A"=="$B"]
["$A"="$B"]      
["$A"!="$B"]     
["$A" -eq "$B"]
["$A" -ne "$B"]
["$A" -le "$B"]
["$A" -lt "$B"]
[ -z "$c" ]       字符串长度为0
[ -n "$c" ]       字符串长度不为0 
test "$a" = "$b" && echo yes || echo no
[ "$a" = "$b" ] && echo yes || echo no
[ "$a" != "$b" ] && echo yes || echo no   ##一个条件
[ ! "a" = "b" ] && echo yes || echo no    ##两个条件,"a" = "b"且该式不成立
[ "$a" -eq "$b" ] && echo yes || echo no  ##等于
[ "$a" -ne "$b" ] && echo yes || echo no  ##不等于
[ "$a" -le "$b" ] && echo yes || echo no  ##小于等于
[ "$a" -ge "$b" ] && echo yes || echo no  ##大于等于
[ "$a" -lt "$b" ] && echo yes || echo no  ##小于
[ "$a" -gt "$b" ] && echo yes || echo no  ##大于
[ "$a" -lt "$b" -o "$a" -eq "$b" ] && echo yes || echo no   ##-o 或者
[ "$a" -lt "$b" -a "$a" -eq "$b" ] && echo yes || echo no   ##-a 和
[ -z "$c" ] && echo yes || echo no  ##参数c为空,输出yes,否则输出no
[ -n "$c" ] && echo yes || echo no  ##参数c不为空,输出yes,否则为no

示例1:

[root@localhost mnt]# test "$a" = "$b" && echo yes || echo no
[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no

示图:
这里写图片描述
示例2:

[root@localhost mnt]# [ -z "$c" ] && echo yes || echo no
[root@localhost mnt]# [ -n "$c" ] && echo yes || echo no

这里写图片描述


实验一:如果root用户的uid=gid,输出为yes,否则为no

[root@localhost mnt]# a=`id -u root`
[root@localhost mnt]# b=`id -g root`
[root@localhost mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
[root@localhost mnt]# [ "$a" -eq "$b" -a "$a" -lt "100" ] && echo yes || echo no 

示图:
这里写图片描述
实验二:判断是否为0-10之间的数,若是为yes,否则no

[root@localhost mnt]# vim check_num.sh 
#!/bin/bash
[ -z "$1" ] && {
           echo input a number !!
           exit
}
[ "$1" -lt "10" -a "$1" -gt "0" ] && echo yes || echo no

或者

[root@localhost mnt]# vim check_num.sh 
#!/bin/bash
[ -z "$1" ] && {
           echo please give me a number !!
           exit
} || {
           [ "$1" -lt "10" -a "$1" -gt "0" ] && echo yes || echo no
}

这里写图片描述

测试:
这里写图片描述
实验三:用户只能以超级用户身份清空日志缓存

[root@localhost mnt]# vim clear_log.sh
#!/bin/bash
User_ID=`id -u`
[ "$User_ID" -ne "0" ] && {
        echo 您不是超级用户!
        exit 1
}
> /var/log/messages
[root@localhost mnt]# chmod +x clear_log.sh 

测试:
这里写图片描述
这里写图片描述


知识点二:

扫描二维码关注公众号,回复: 2267547 查看本文章
["file1" -ef "file2"]     ##file1和file2的节点号/索引号是否相同
["file1" -nt "file2"]     ##file1是否比file2新(创建的时间晚),nt (new time)
["file1" -ot "file2"]     ##file1是否比file2旧(创建的时间早),ot (old time)

示例:

[root@localhost mnt]# touch westos
[root@localhost mnt]# ln /mnt/westos /mnt/westos1    ##创建硬链接,节点号相同
[root@localhost mnt]# ls -li westos westos1
[root@localhost mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo no
[root@localhost mnt]# [ "/mnt/westos" -ef "/etc/passwd" ] && echo yes || echo no
[root@localhost mnt]# [ "/mnt/westos" -nt "/etc/passwd" ] && echo yes || echo no
[root@localhost mnt]# [ "/mnt/westos" -ot "/etc/passwd" ] && echo yes || echo no

注意:

 ls -i filename1 filename2  ##打印每个文件的索引号 
 ls -li filename            ##使用长列表格式列出目录中的内容并显示文件的索引号 

知识点三:

[ -e "file" ]  ##文件是否存在
[ -f "file" ]  ##文件是否是普通文件
[ -L "file" ]  ##软链接
[ -S "file" ]  ##套接字  ##进入程序内部的接口文件
[ -b "file" ]  ##块设备
[ -d "file" ]  ##目录
[ -c "file" ]  ##字符设备
[ -e "/mnt/file" ] && echo yes || echo no   ##文件是否存在
[ -f "/bin/ls" ] && echo yes || echo no     ##文件是否普通文件
[ -L "/mnt/hello" ] && echo yes || echo no  ##文件是否为软链接
[ -b "/dev/vdb" ] && echo yes || echo no    ##文件是否为块设备
[ -d "/mnt" ] && echo yes || echo no        ##文件是否为目录
[ -c "/dev/pts/0" ] && echo yes || echo no  ##文件是否为字符设备

示例1:普通文件
这里写图片描述


注意:属性的第一个字母为空,表示普通文件

[root@localhost mnt]# ls -l /mnt/file
-rw-r--r--. 1 root root 22 Jun 14 03:46 /mnt/file  

示例2:链接文件(属性第一个字母为l表示)
这里写图片描述


注意:
软链接等同于快捷方式,多个节点号对应一个文件;
硬连接等同于备份, 一个节点号对应多个文件 。

ln /mnt/file /mnt/hello      ##硬连接,不加-s
ln -s /mnt/file /mnt/hello   ##软链接,加-s

示例3:套接字

[root@server5 mnt]# yum install mariadb-server.x86_64
[root@server5 mnt]# systemctl start mariadb
[root@server5 mnt]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no  ##套接字
yes

这里写图片描述
示例4:文件类型同步

rsync -D /dev/pts/1 /mnt/file  

这里写图片描述


实验一:检测/mnt/file文件类型

[root@localhost mnt]# vim file.sh
[ "$1" "/mnt/file" ] && echo yes || echo no

测试:
这里写图片描述
实验二:检测文件类型

[root@localhost mnt]# vim check_file.sh
#!/bin/bash
[ -z "$1" ] && { echo Please Input Filename!!! 
                 exit 1
}
[ -e "$1" ] || { echo $1 does not exit!
                 exit 1
}
[ -L "$1" ] && { echo $1 is a link file 
                 exit 1
}
[ -S "$1" ] && { echo $1 is a socket file
                 exit 1
}
[ -d "$1" ] && { echo $1 is a directory
                 exit 1
}
[ -b "$1" ] && { echo $1 is a block device
                 exit 1
}
[ -c "$1" ] && { echo $1 is a character device 
                 exit 1
}
[ -f "$1" ] && { echo $1 is a common file 
                 exit 1
}
[root@server5 mnt]# chmod +x check_file.sh 

这里写图片描述
测试:

[root@localhost mnt]# sh check_file.sh /dev/pts/0                  ##字符设备
[root@localhost mnt]# sh check_file.sh /dev/vdb                    ##块设备
[root@localhost mnt]# sh check_file.sh file                        ##普通文件
[root@localhost mnt]# sh check_file.sh /var/lib/mysql/mysql.sock   ##套接字
[root@localhost mnt]# sh check_file.sh /mnt                        ##目录
[root@localhost mnt]# sh check_file.sh westos2                     ##文件不存在
[root@localhost mnt]# sh check_file.sh                             ##输入文件名称

这里写图片描述


注意:

exit 0    ##正常运行程序并退出程序
exit 1    ##非正常运行导致退出程序

2.tr命令

tr命令可以对来自标准输入的字符进行替换、压缩和删除。它可以将一组字符变成另一组字符,经常用来编写优美的单行命令,作用很强大。
示例1:输入字符大小写转换

[root@localhost mnt]# vim westos
westos
WESTOS
[root@localhost mnt]# tr 'a-z' 'A-Z' < /mnt/westos
[root@localhost mnt]# tr 'A-Z' 'a-z' < /mnt/westos

示图:
这里写图片描述
实验一:转换大小写

[root@localhost mnt]# vim test.sh
#!/bin/bash
WORD=$(echo $1 | tr 'A-Z' 'a-z')    ##先执行$1,将其转化为a-z,再将整体结果赋值给WORD
[ "$WORD" = "hello" ] && {
       echo yes
}||{
       echo no
}

测试:
这里写图片描述

实验二:验证用户是否存在,并更改密码

[root@localhost mnt]# vim user_create.sh
#!/bin/bash
[ "$#" -eq "2" ]||{
        echo "Please input username and password after script!"
        exit 1
}
Check_User=`getent passwd $1`
[ -n "$Check_User" ]&&{
        echo $1 is exits!
        exit 1
}

useradd $1
echo $2 | passwd --stdin $1

测试:
这里写图片描述

3.find命令

知识点一:

find /mnt/ -size 20k        ##查找文件大小等于20k
find /mnt/ -size -20k       ##查找文件大小小于20k
find /mnt/ -size +20k       ##查找文件大小大于20k

示例1:根据文件爱你大小查找文件

[root@localhost mnt]# dd if=/dev/zero of=/mnt/file1 bs=1024 count=10
[root@localhost mnt]# dd if=/dev/zero of=/mnt/file2 bs=1024 count=20
[root@localhost mnt]# dd if=/dev/zero of=/mnt/file3 bs=1024 count=30

这里写图片描述

知识点二:

find /etc -maxdepth 1 -name passwd              ##查找/etc下最大深度为1的passwd文件
find /etc -maxdepth 2 -name passwd              ##查找/etc下最大深度为2的passwd文件(深度小于等于2)
find /etc -mindepth 1 -maxdepth 2 -name passwd  ##查找/etc下深度大于1且小于等于2的passwd文件
find /etc -mindepth 2 -maxdepth 2 -name passwd  ##查找/etc下深度为2的passwd文件

示例:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/hannah_zh/article/details/80707091