shell之活学活用

1. 查看系统运行级别

[root@localhost ~]# runlevel 
N 5
[root@localhost ~]# runlevel |cut -c3
5
[root@localhost ~]# runlevel |cut -d' ' -f2
5
[root@localhost ~]# 

在这里插入图片描述

2. 查看系统ip等信息

[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:80a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:08:0a  txqueuelen 1000  (Ethernet)
        RX packets 13769  bytes 2221738 (2.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8056  bytes 1276337 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig eth0 |grep 'netmask'
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
[root@localhost ~]# ifconfig eth0 |grep 'netmask' |tr -s ' '
 inet 172.25.254.10 netmask 255.255.255.0 broadcast 172.25.254.255
[root@localhost ~]# ifconfig eth0 |grep 'netmask' |tr -s ' '|cut -d' ' -f3
172.25.254.10
[root@localhost ~]# 

在这里插入图片描述

[root@localhost ~]# ifconfig eth0 |grep 'netmask' 
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
[root@localhost ~]# ifconfig eth0 |grep 'netmask' |tr -d 'a-z'
         172.25.254.10   255.255.255.0   172.25.254.255
[root@localhost ~]# ifconfig eth0 |grep 'netmask' |tr -d 'a-z'|tr ' ' '\n'









172.25.254.10


255.255.255.0


172.25.254.255
[root@localhost ~]# ifconfig eth0 |grep 'netmask' |tr -d 'a-z'|tr ' ' '\n'|grep -v '^$'
172.25.254.10
255.255.255.0
172.25.254.255
[root@localhost ~]# 

在这里插入图片描述

[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:80a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:08:0a  txqueuelen 1000  (Ethernet)
        RX packets 14028  bytes 2244596 (2.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8203  bytes 1295651 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ifconfig eth0 |grep 'ether'
        ether 52:54:00:00:08:0a  txqueuelen 1000  (Ethernet)
[root@localhost ~]# ifconfig eth0 |grep 'ether' |tr -s ' '
 ether 52:54:00:00:08:0a txqueuelen 1000 (Ethernet)
[root@localhost ~]# ifconfig eth0 |grep 'ether' |tr -s ' ' |cut -d' ' -f3
52:54:00:00:08:0a
[root@localhost ~]# 

在这里插入图片描述

3.保存普通用户用户名 密码到指定文件

[root@localhost ~]# grep 'bash$' passwd 
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
westos01:x:1001:1001::/home/westos01:/bin/bash
westos02:x:1002:1002::/home/westos02:/bin/bash
westos03:x:1003:1003::/home/westos03:/bin/bash
[root@localhost ~]# grep 'bash$' passwd | grep -v root
student:x:1000:1000:Student User:/home/student:/bin/bash
westos01:x:1001:1001::/home/westos01:/bin/bash
westos02:x:1002:1002::/home/westos02:/bin/bash
westos03:x:1003:1003::/home/westos03:/bin/bash
[root@localhost ~]# grep 'bash$' passwd | grep -v 'root' |cut -d: -f1,2,7
student:x:/bin/bash
westos01:x:/bin/bash
westos02:x:/bin/bash
westos03:x:/bin/bash
[root@localhost ~]# grep 'bash$' passwd | grep -v 'root' |cut -d: -f1,2,7 |tr ':' '\t'
student	x	/bin/bash
westos01	x	/bin/bash
westos02	x	/bin/bash
westos03	x	/bin/bash
[root@localhost ~]# grep 'bash$' passwd | grep -v 'root' |cut -d: -f1,2,7 |tr ':' '\t' |tee abc.txt
student	x	/bin/bash
westos01	x	/bin/bash
westos02	x	/bin/bash
westos03	x	/bin/bash
[root@localhost ~]# cat abc.txt 
student	x	/bin/bash
westos01	x	/bin/bash
westos02	x	/bin/bash
westos03	x	/bin/bash
[root@localhost ~]# 

在这里插入图片描述

4. 判断两台主机是否能ping通

[root@localhost ~]# sh ping.sh 
请输入你要ping通的主机ip: 172.25.254.8
当前主机和远程主机172.25.254.8是通的
[root@localhost ~]# cat ping.sh 
#!/bin/bash
read -p "请输入你要ping通的主机ip: " ip

ping -c1 $ip &>/dev/null   #-c1表示ping 1次就结束

if [ $? -eq 0 ];then
	echo "当前主机和远程主机$ip是通的"
else
	echo "当前主机和远程主机$ip不通"
fi

[root@localhost ~]# 

在这里插入图片描述

5. 判断httpd进程是否存在

#!/bin/bash
pgrep httpd &>/dev/null

if [ $? -eq 0 ];then
        echo "当前httpd进程存在"
else
        echo "当前httpd进程不存在"
fi

在这里插入图片描述

6.判断门户网站是否能够正常提供服务

#!/bin/bash
web_sever=www.baidu.com

wget -p /shell/ $web_server &>/dev/null  # -p 指定网站页面的下载位置,若通,将下载的页面文件删除

[ $? -eq 0 ] && echo "当前网站服务ok" && rm -rf /shell/* || echo "当前网站服务不ok"

在这里插入图片描述

7.判断用户是否存在

[root@localhost ~]# sh user.sh 
请输入一个用户: we
该用户不存在
[root@localhost ~]# vim user.sh
[root@localhost ~]# sh user.sh 
请输入一个用户: student
用户存在
[root@localhost ~]# sh user.sh 
请输入一个用户: root
用户存在
[root@localhost ~]# cat user.sh 
#!/bin/bash
read -p "请输入一个用户: " user_name

id $user_name &>/dev/null
if [ $? -eq 0 ];then
	echo "用户存在"
else
	echo "该用户不存在"
fi
[root@localhost ~]# 

在这里插入图片描述

8.计算1–100的奇数和

[root@localhost ~]# sh odd.sh 
1--100的奇数和为:2500
[root@localhost ~]# cat odd.sh 
#!/bin/bash
sum=0
for i in {1..100..2}
do
	let sum=$sum+$i
done

echo "1--100的奇数和为:$sum"

9. 3s监控一次系统的负载

#!/bin/bash
while true
do
        clear
        uptime
        sleep 3
done

在这里插入图片描述

发布了168 篇原创文章 · 获赞 1 · 访问量 3004

猜你喜欢

转载自blog.csdn.net/yrx420909/article/details/104342207