tr命令
- tr,translate的简写,意思是转化,转变,转换。用来从标准输入中通过替换或删除操作进行字符转换。
tr的具体用法可以通过tr --help
查看: - 写一个脚本,将输入的小写字母都转换为大写:
vim upper.sh
#!/bin/bash
echo $1 | tr 'a-z' 'A-Z'
执行结果:
- 将输入的大写字母转换为小写,同理:
vim small.sh
#!/bin/bash
echo $1 | tr 'A-Z' 'a-z'
执行结果:
test命令
- 功能:检查文件类型和比较值。
- lt 小于,le小于等于,ge 大于等于,gt 大于
- 在shell中,test命令 = [],=和==效果一样。
- 通过
man test
可以看test具体的用法说明
test命令比较值的大小
举例:
a=1
b=1 ##先赋值
test "$a" = "$b" && echo yes || echo no ##判断a=b
[ "$a" = "$b" ] && echo yes || echo no ##判断a=b
[ ! "$a" = "$b" ] && echo yes || echo no ##判断a=b不成立是真还是假
[ "$a" != "$b" ] && echo yes || echo no ##判断a不等于b
[ "$a" -eq "$b" ] && echo yes || echo no ##相等
[ "$a" -ne "$b" ] && echo yes || echo no ##不相等
[ "$a" -lt "$b" ] && echo yes || echo no ##小于
[ "$a" -le "$b" ] && echo yes || echo no ##小于等于
[ "$a" -gt "$b" ] && echo yes || echo no ##大于
[ "$a" -ge "$b" ] && echo yes || echo no ##小于等于
[ "$a" -lt "1" -a "$b" -gt "5" ] && echo yes || echo no ##a小于1,且b大于5,两个都成立才输出yes
[ "$a" -lt "1" -o "$b" -gt "5" ] && echo yes || echo no ##a小于1或b大于5,有一个成立则输出yes
执行结果:
测试题
要求:当根分区的使用量 > 80%,要求写一个报警的脚本,且每分钟报警一次,并将结果输出到日志文件/var/log/messages
中。
1
我们通过命令:df -h / | tail -n 1 | cut -d " " -f 15 | cut -d % -f 1
可以只取内存使用量的数值。
vim checkstorage.sh
#!/bin/bash
[ "`df -h / | tail -n 1 | cut -d " " -f 15 | cut -d "%" -f 1 `" -ge "30" ] && { ##为了实验效果明显写的30
echo "Warning: /is full!" >> /var/log/messages
}
at now+1min <<-EOF 设置定时任务
checkstorage.sh
EOF
chmod +x checkstorage.sh 给脚本添加执行权限
ls -l checkstorage.sh
at -l 查看定时任务列表
at -c +num 查看任务内容
2
类似于C语言中的数组:
a=(`df -h / | tail -n 1`)
echo ${a[0]}
echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
echo ${a[4]}
echo ${a[5]}
echo ${a[4]} | cut -d % -f 1
3 awk
df -h / | tail -n 1 | awk '{print $5}' | cut -d % -f 1
test命令判断文件的类型
-e | 文件是否存在 |
---|---|
-f | 是不是普通文件 |
-L | 是不是软链接 |
-S | 是否是一个套接字 socket |
-b | 块设备 block |
-d | 目录 |
-c | 字符设备 |
-z | zero,值为空 |
-n | 值不为空 |
ef | 看两个文件是不是互为硬链接,是不是节点一致 |
nt | 是不是前面的文件新一些 |
ot | 前面的文件是不是老一些 |
举例:
a=""
echo $a 输出a,未空
[ -z "$a" ] && echo yes || echo no 值为空,输出yes
a=1
[ -z "$a" ] && echo yes || echo no 这输出no
[ -n "$a" ] && echo yes || echo no 这输出yes,不为空
小测试
完善之前的ip判断,如果输入为空,给出提示。
#!/bin/bash
[ -z "$1" ] && {
echo "Error:Please input ipaddress following script!!"
exit ##输入为空,直接退出。
}
ping -c 1 -w 1 $1 &> /dev/null && {
echo -e "\033[32m$1 is up\033[0m"
}||{
echo -e "\033[31m$1 is down\033[0m"
}
举例:
/mnt/haha 和 /mnt/ping.sh 互为硬链接
[ "/mnt/haha" -ef "/mnt/ping.sh" ] && echo yes || echo no ##输出为yes
[ "/mnt/ping.sh" -ot "/mnt/checkstorage.sh" ] && echo yes || echo no
[ "/mnt/ping.sh" -nt "/mnt/checkstorage.sh" ] && echo yes || echo no
举例:
[ -e "/mnt" ] && echo yes || echo no
[ -e "/mnt/lala" ] && echo yes || echo no 文件存不存在
[ -f "/mnt/test" ] && echo yes || echo no 普通文件
[ -L "/etc/system-release" ] && echo yes || echo no
ls -l /etc/system-release 软链接
yum install mariadb-server -y
systemctl start mariadb
ls -l /var/lib/mysql/mysql.sock
[ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no 套接字
[ -b "/dev/vda" ] && echo yes || echo no 块设备
[ -d "/mnt/" ] && echo yes || echo no
[ -d "/dev/" ] && echo yes || echo no 目录
[ -c "/dev/pts/0" ] && echo yes || echo no 字符设备
小测试
给你一个文件名称,判断文件的类型
vim filetype.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error:Please input a file following scripts!!"
exit
}
[ -e "$1" ] || {
echo "$1 is not exist!"
exit ##不存在的不继续执行,提高脚本效率
}
[ -f "$1" ] && {
echo "$1 is a common file!" ##不退出,因为软链接文件也是普通文件,继续判断
}
[ -L "$1" ] && {
echo "$1 is a soft link file!"
exit
}
[ -S "$1" ] && {
echo "$1 is socket file!"
exit
}
[ -b "$1" ] && {
echo "$1 is a block file!"
exit
}
[ -d "$1" ] && {
echo "$1 is a directory!"
exit
}
[ -c "$1" ] && {
echo "$1 is a letter file!"
exit
}
版权声明:本文为even160941原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。