1.read 命令(交互式)
read ##用于接收用户输入的信息
-s #表示对输入内容的加密(即不显示输入的内容)
-p #显示提示语
-t #表示等待时间,超过多久后便自动退出
实验:
## -p表示显示提示语,i表示接收用户输入信息的变量
[root@localhost ~]# read -p "Please input an integer:" i
Please input an integer:5
[root@localhost ~]# echo $i
5
## -s表示加密;即无法看到用户输入的内容
[root@localhost ~]# read -sp "Please input a character:" j
Please input a character:[root@localhost ~]# echo $j
westos
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
方法1:
[root@localhost ~]# ifconfig eth0 | grep 'netmask'
inet 172.25.254.166 netmask 255.255.255.0 broadcast 172.25.254.255
## -d表示指定分隔符;-f表示指定截取的列
[root@localhost ~]# ifconfig eth0 | grep 'netmask' | cut -d " " -f 10
172.25.254.166
方法2:
## awk默认分隔符即为空格,且不区分空格的个数
[root@localhost ~]# ifconfig eth0 | grep 'netmask' | awk '{print $2}'
172.25.254.166
练习2:检测ip是否可以通信
&& ##代表 ture(正确)
|| ##代表 false(错误)
[root@localhost ~]# ping -c1 -w1 172.25.254.33 > /dev/null && echo YES || echo NO
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
注释:
-c 表示次数(count),-w 表示等待时间(wait)
$1 表示脚本后跟的第一串字符
> /dev/null 表示将冗余的输出信息导入到垃圾箱内
&& 代表ture,即命令执行成功则执行该符号后的命令
|| 代表false,即命令执行失败则执行该符号后的命令
[root@localhost mnt]# sh ip_checks.sh 33
172.25.254.33 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]# vim westos
[root@localhost mnt]# cat westos
hello westos
[root@localhost mnt]# cat file
## -o表示输出到指定文件,即将westos文件内容导入到file中
[root@localhost mnt]# sort -o file westos
[root@localhost mnt]# cat file
hello westos
[root@localhost mnt]# vim sort_test
[root@localhost mnt]# cat sort_test
##默认只对第一列数排序
[root@localhost mnt]# sort -n sort_test
##指定只对第二列排序;-t表示指定分隔符,-k表示排序的列
[root@localhost mnt]# sort -nt : -k 2 sort_test
练习3:获取系统cpu前5的进程pid和cpu
## -o表示输出指定信息,--sort表示排序,-表示倒序,
[root@localhost ~]# ps ax -o %cpu,pid --sort=-%cpu | head -n 5
[root@localhost ~]# ps ax -o %cpu,pid --sort=-%cpu | head -n 6
方法1:
## head -n 6表示前6行,taill -n 5表示后5行
[root@localhost ~]# ps ax -o %cpu,pid --sort=-%cpu | head -n 6 | tail -n 5
方法2:
## -v表示取反
[root@localhost ~]# ps ax -o %cpu,pid --sort=-%cpu | head -n 6 | grep -v PID
练习4:找出/tmp目录中最大的文件的文件
[root@localhost ~]# ll /tmp/
方法1:
## sort表示排序;-t表示指定分隔符,-n表示纯数字排序,-r表示倒序
[root@localhost ~]# ll /tmp/ | sort -nrt " " -k 5 | cut -d " " -f 9 | head -n 2
ssh-7njSGonlEXAj
ssh-EYXgPDqOGMZK
方法2:
## -S表示倒序
[root@localhost ~]# ll -S /tmp/ | head -n 3
## -v表示取反
[root@localhost ~]# ll -S /tmp/ | head -n 3 | grep -v total
## ll命令后直接添加-S即可进行排序;-d表示指定分隔符,-f表示指定输出的列
[root@localhost ~]# ll -S /tmp/ | head -n 3 | grep -v total | cut -d " " -f 9
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:找出/mnt目录里最大的文件
方法1:
[root@localhost mnt]# ll /mnt
[root@localhost mnt]# ll -S /mnt
[root@localhost mnt]# ll -S /mnt | head -n 2
[root@localhost mnt]# ll -S /mnt | head -n 2 | grep -v total
[root@localhost mnt]# ll -S /mnt | head -n 2 | grep -v total | cut -d " " -f 9
方法2:
[root@localhost mnt]# ls -l /mnt | grep total -v | awk -F " " '//{print $5,$9}' | sort -nr | head -n 1 | cut -d " " -f 2
5.test命令
test "$a" = $b" 等同于 [ "$a" = "$b" ]
比较大小:
[ "$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)
选择条件:
-a ##且;条件必须都满足(and)
-o ##或;条件至少满足一个(or)
例如:
[ "$a" -ne "$b" -a "$a" -gt "$b" ] ##$a不等于并且大于$b;-a表示所有条件都必须满足
[ "$a" -ne "$b" -o"$a" -gt "$b" ] ## $a不等于或者大于$b;-0表示条件至少满足一个
文件类型:
-f ##普通文件(file)
-d ##目录(directory)
-L ##软连接(link)
-S ##套接字(socket)
-b ##快设备(block)
例如:
[ -f "file" ] ##判断文件是否为普通文件
[ -b "file" ] ##判断文件是否为块设备
[ -S "file" ] ##判断文件是否为套接字
[ -c "file" ] ##判断文件是否为字符设备
[ -L "file" ] ##判断文件是否为软链接
练习6:判断输入的数字是否在10以内
[root@localhost mnt]# vim number.sh
#####################
#!/bin/bash
read -p "请输入一个整数:" i
[ -z $i ] && {
echo "EROOR:Please input a interger"
exit 1
}
[ $i -gt 0 -a $i -lt 10 ] && echo "YES" || echo "NO"
注释:
read ##接收用户输入信息;-p表示显示提示语
exit ##表示退出;1表示异常退出,0表示正常退出
-z ##表示判断是否为空(zero)
-a ##表示且,即为所有条件必须同时满足
-gt ##判断是否大于
-lt ##判断是否小于
&& ##表示成功;若命令执行成功则执行&&后边的内容
|| ##表示失败;若命令执行失败执行||后边的内容
[root@localhost mnt]# sh number.sh
请输入一个整数:
EROOR:Please input a interger
[root@localhost mnt]# sh number.sh
请输入一个整数:3
YES
[root@localhost mnt]# sh number.sh
请输入一个整数:-9
NO
练习7:判断文件类型
#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
[root@localhost mnt]# ll /dev/mapper/
total 0
crw-------. 1 root root 10, 236 Dec 28 08:23 control
lrwxrwxrwx. 1 root root 7 Dec 28 08:23 rhel-root -> ../dm-0
lrwxrwxrwx. 1 root root 7 Dec 28 08:23 rhel-swap -> ../dm-1
[root@localhost mnt]# sh file_type.sh control
this file is not exist!
[root@localhost mnt]# sh file_type.sh /dev/mapper/control
this is a charector