1.read 命令
read ##用于接收用户输入的信息
-s #表示对输入内容的加密(即不显示输入的内容)
-p #显示提示语
-t #表示等待时间,超过多久后便自动退出
实验:
- -p表示显示提示语,i表示接收用户输入信息的变量
-
-s表示加密;即无法看到用户输入的内容
2.cut命令-d ##指定分隔符
-f ##指定截取的列
-c ##指定截取的字符位置
实验:[root@localhost ~]# vim cut_test [root@localhost ~]# cat cut_test
-
-d表示指定分隔符; -f表示指定截取的列
[root@localhost ~]# cut -d “:” -f 3 cut_test
-
-c表示指定截取的字符位置;无需指定分隔符
[root@localhost ~]# cut -c 8 cut_test
-
按列截取的第1-5个字符
[root@localhost ~]# cut -c 1-5 cut_test
练习1:获取当前主机的ip |
---|
[root@localhost ~]# ifconfig eth0 | grep 'netmask'
-
-d表示指定分隔符;-f表示指定截取的列
[root@localhost ~]# ifconfig eth0 | grep 'netmask' | cut -d " " -f 10
练习2:检测ip是否可以通信 |
---|
&& ##代表 ture(正确)
|| ##代表 false(错误)
[root@localhost ~]# ping -c1 -w1 172.25.254.33 > /dev/null && echo YES || echo NO
##写入文件中;即生成shell脚本
[root@localhost mnt]# vim ip_checks.sh
#####################
#!/bin/bash
ping -c1 -w1 172.25.254.$1 > /dev/null && echo 172.25.254.$1 is UP || echo 172.25.254.$1 is DOWN
3.sort命令 (排序)
-n ##纯数字排序
-r ##倒序
-t ##指定分隔符
-k ##指定排序的列
-o ##导入到指定文件
-u ##去掉重复的数字
[root@localhost mnt]# vim sort_test
[root@localhost mnt]# cat sort_test
##若是两位数则会当作两列数分别排序
[root@localhost mnt]# sort sort_test
-
-n表示纯数字排序
[root@localhost mnt]# sort -n sort_test
-
-u表示去重
[root@localhost mnt]# sort -nu sort_test
-
-r表示倒序
[root@localhost mnt]# sort -nr sort_test
-
默认只对第一列数排序
[root@localhost mnt]# sort -n sort_test
-
指定只对第二列排序;-t表示指定分隔符,-k表示排序的列
[root@localhost mnt]# sort -nt : -k 2 sort_test
4. uniq 命令 -
对充重复的字符做相应的处理(常常和 sort命令搭配着使用)
uniq -u #显示唯一的行
uniq -d #显示重复的行
uniq -c #显示并统计重复次数
[root@localhost mnt]# vim unique_test
[root@localhost mnt]# cat unique_test
##-n表示纯数字排序,-u表示去重
[root@localhost mnt]# sort -n unique_test
[root@localhost mnt]# sort -nu unique_test
## -c表示显示并统计重复次数
[root@localhost mnt]# sort -n unique_test | uniq -c
## -u 表示显示唯一的行
[root@localhost mnt]# sort -n unique_test | uniq -u
## -d 表示显示重复的行
[root@localhost mnt]# sort -n unique_test | uniq -d
5.test命令
比较大小:
[ "$1" = "$2" ] ##判断$1是否等于$2
[ "$1" -eq "$2" ] ##判断$1是否等于$2 (equal)
[ "$1" != "$2" ] ##判断$1是否不等于$2
[ "$1" -ne "$2" ] ##判断$1是否不等于$2 (not equal)
[ "$1" -le "$2" ] ##判断$1是否小于等于$2 (less than equal)
[ "$1" -lt "$2" ] ##判断$1是否小于$2 (less than)
[ "$1" -ge "$2" ] ##判断$1是否大于等于$2 (greater than equal)
[ "$1" -gt "$2" ] ##判断$1是大于$2 (greater than)
[ -z "$2" ] ##判断$2是否为空(zero)
[ -n "$2" ] ##判断$2是否不为空(not zero)
[ -e "$2" ] ##判断$2是否存在(exist)
文件类型:
-f ##普通文件(file)
-d ##目录(directory)
-L ##软连接(link)
-S ##套接字(socket)
-b ##快设备(block)
例如:
[ -f "file" ] ##判断文件是否为普通文件
[ -b "file" ] ##判断文件是否为块设备
[ -S "file" ] ##判断文件是否为套接字
[ -c "file" ] ##判断文件是否为字符设备
[ -L "file" ] ##判断文件是否为软链接
判断文件类型 |
---|
#1.编写脚本
[root@localhost mnt]# vim file_type.sh
#####################
#!/bin/bash
[ -z "$1" ] && {
echo "please input a filename!"
exit 1
}
[ -e "$1" ] || {
echo "this file is not exist!"
exit 0
}
[ -f "$1" ] && {
echo "this is a comman file"
exit 0
}
[ -d "$1" ] && {
echo "this is a directory"
exit 0
}
[ -S "$1" ] && {
echo "this is a solket"
exit 0
}
[ -c "$1" ] && {
echo "this is a charector"
exit 0
}
[ -L "$1" ] && {
echo "this is a link"
exit 0
}
[ -b "$1" ] && {
echo "this is a block"
exit 0
}
[ -b "$1" ] && {
echo "this is a block"
exit 0
}
注释:
-z ##判断是否为空(zero)
-n ##判断是否不为空(not zero)
-e ##判断是否存在(exist)
-f ##普通文件(file)
-d ##目录(directory)
-L ##软连接(link)
-S ##套接字(socket)
-b ##快设备(block)
#2.执行脚本(测试)
[root@localhost mnt]# sh file_type.sh were
this file is not exist!
[root@localhost mnt]# mkdir westos1
[root@localhost mnt]# touch file1
[root@localhost mnt]# sh file_type.sh file
this is a comman file
[root@localhost mnt]# sh file_type.sh westos
this is a comman file
[root@localhost mnt]# sh file_type.sh /dev/sda
this is a block