力扣:
1.提取有效的电话号码
[root@server1 mnt]# grep -E "(^\([a-z]{3}\) |^[a-z]{3}-)[a-z]{3}-[a-z]{4}$" aaaf
abc-def-ghxy
(abc) def-ghxy
[root@server1 mnt]# grep -E "(^\([0-9]{3}\)\ |^[0-9]{3}-)[0-9]{3}-[0-9]{4}$" aaaf
123-456-7894
(123) 456-7890
- 打印文件第十行
sed -n 10p filename
head -n 10 filename| tail -n 1
- 转置文件内容
#!/bin/bash
awk '{
for(i = 1;i <= NF;i++){ /按照NF长度建立数组
if(NR==1){
arr[i]=$i; /如果是第一行,取对应的这个$i值
}
else {
arr[i]=arr[i]" "$i; /如果不是第1行,以空格为连接符连到后面
}
}
}
END {
for(i = 1;i <= NF;i++) {
print arr[i]; /打印出数组的元素,有NF个
}
}' $1
效果:
[root@server1 mnt]# cat test
i love you
1 2 3
[root@server1 mnt]# sh 1.sh test
i 1
love 2
you 3
就实现了转置。
- 统计词频
[root@server1 mnt]# cat test
he love you
he he love you so
he he love
you you you you you
[root@server1 mnt]# grep -E '[a-z]*' test -o /grep -e筛选单词,-o输出所匹配到的
he
love
you
he
he
love
you
so
he
he
love
you
you
you
you
you
[root@server1 mnt]# grep -E '[a-z]*' test -o| sort /sort 排序单词
he
he
he
he
he
love
love
love
so
you
you
you
you
you
you
you
[root@server1 mnt]# grep -E '[a-z]*' test -o| sort|uniq -c /uniq -c 去掉重复的单词,得到 "出现次数 单词"形式;-c 是count
5 he
3 love
1 so
7 you
[root@server1 mnt]# grep -E '[a-z]*' test -o| sort|uniq -c|sort -r /倒叙排列
7 you
5 he
3 love
1 so
[root@server1 mnt]# grep -E '[a-z]*' test -o| sort|uniq -c|sort -r| awk '{print $2,$1}' /打印出来
you 7
he 5
love 3
so 1
一、检测文件类型
编写脚本完成以下条件
file_check.sh
在执行时如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
脚本:
#!/bin/bash
[ -z "$1" ] && {
echo Error:Test file not specified,plaese specify.
exit
}
[ -e "$1" ] || {
echo Error:$1 not exist
}
[ -L "$1" ] && {
echo $1 is a link file
}
[ -b "$1" ] && {
echo $1 is a block file
}
[ -c "$1" ] && {
echo $1 is a char file
}
[ -d "$1" ] && {
echo $1 is a direcotry
}
[ -S "$1" ] && {
echo $1 is a socket file
}
[ -f "$1" ] && {
echo $1 is a common file
}
也可以这样,第二种更为简洁:
效果:
二、找出目录下最大文件
ls -l 目录 可以显示目录中所有文件案的属性
清按照文件大小进行排序并找出最大的2个文件并显示其名称
脚本:
!/bin/bash
ls -S * | head -n 2
三、显示网卡的ip信息
ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
脚本:
#!/bin/bash
[ -z "$1" ] && {
echo "Error:no interface name,please input interface following scripts."
exit
}
ifconfig $1 &> /dev/null || {
echo "$1 is not found."
exit
}
ifconfig $1 |head -n 2 |tail -n 1 |cut -d " " -f 10
四、更改http端口
编写 Apache_port.sh
此脚本后介入数字
http的端口就改为此数字
假设selinux为关闭状态
脚本:
#!/bin/bash
[ -z "$1" ] && {
echo Error:Please input port number
exit
}
[ -z "`netstat -antlupe | grep $1`" ] || {
echo "Error: $1 port is exist"
exit
}
[ -e "/etc/httpd/conf/httpd.conf" ] || {
yum install httpd -y &> /dev/null || {
echo apache is not install and yum repo not avaliable
exit
}
}
sed -i "/^Listen/c Listen $1" /etc/httpd/conf/httpd.conf
systemctl restart httpd > /dev/null && {
echo "Configure port successful"
} || {
echo "Can't start service"
}
效果:
五、显示能被su切换用户
练习脚本:
请显示系统中能被su命令切换的用户名称
#!/bin/bash
grep -E "bash$" /etc/passwd | cut -d : -f 1
效果:
六、统计在系统中能su切换的
并且用户加目录不在/home下的
用户数量
脚本;
效果:
七、显示主机信息
1
ip_show.sh 网卡 显示当前的ip地址
效果:
2
host_messages.sh 显示当前主机的名称,ip,登陆当前主机的用户
hostname: xxxxx
ipaddress: xxxx.xxxx.xxx.xxx
username: root
效果:
4.显示网卡
八、批量建立用户
课下脚本
for 语句
create_user.sh userfile passfile
userfile用的用户会被全部建立
passfile中的密码会被全部设定
分别取三次。
#!/bin/bash
names=($(cat /mnt/userfile))
passs=($(cat /mnt/passfile))
for name in `cat /mnt/userfile`
do
useradd $name
echo $name user is already make.
done
echo ${passs[0]} |passwd ${names[0]} --stdin
echo ${passs[1]} |passwd ${names[1]} --stdin
echo ${passs[2]} |passwd ${names[2]} --stdin
效果:
且密码正确。
九、检测同网段的主机
脚本练习:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表
效果:
十、查询文件类型
脚本练习:
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
脚本:
效果:
十一、查看系统资源使用情况
脚本练习
system_watch.sh disk | memory | upload (每秒显示)
disk 监控磁盘使用情况 (df)
memory 监控内存使用情况 (free)
upload 监控启动负载 (uptime)
十二
脚本练习
auto_ssh 192.168.0.1 westos
可以自动连接目标主机当目标主机网络不通时报错
脚本
#!/bin/bash
AUTO_CMD()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 $3
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$2\r" }
}
expect eof
EOF
}
ping -c1 -w1 $1 &>/dev/null && {
AUTO_CMD $1 $2 $3
}
效果:
十三、清空日志
clear_log.sh 执行此脚本后可以清空日志
内容:
效果: