当前位置:网站首页>Shell脚本案例---2
Shell脚本案例---2
2022-07-22 06:55:00 【棒棒吃不胖】
1)请ping通百度域名3次,成功或失败分别输出不同的信息。
用三种方式完成。
方式一:
此处需要注意的点是,如果3次都ping通,&&
就会生效,即执行其后面的命令,ping www.baidu.com ok
3次只要有一次没有ping通,||
生效,会执行其后面的命令,ping www.baidu.com error
#!/bin/bash
IP=www.baidu.com
ping -c3 $IP &>/dev/null && echo "ping $IP ok" || echo "ping $IP error"
方式二:
用if
语句完成非交互式的ping网址
非交互式如下:
#!/bin/bash
#定义变量
ip=www.baidu.com
#只ping三次且不输出任何内容
ping -c3 $ip &>/dev/null
#判断上一个语句的执行结果,执行成功则为0;否则是1
if [ $? -eq 0 ];then
echo "$ip ok"
else
echo "$ip error"
fi
方式三:
用if
语句完成交互式的ping网址
交互式如下:
#!/bin/bash
#执行脚本后,用户输入变量IP,并且有提示语句
read -p "please input network ip:" IP
#只ping三次且不输出任何内容
ping -c3 $IP &>/dev/null
#判断上一个语句的执行结果,执行成功则为0;否则是1
if [ $? -eq 0 ];then
echo "$IP ok"
else
echo "$IP error"
fi
2)相关参数的含义。
原shell脚本如下:
[[email protected] scripts]# cat test.sh
#!/bin/bash
echo "第3个位置是$3"
echo "第2个位置是$2"
echo "第1个位置是$1"
echo "所有参数是:$*"
echo "所有参数是:[email protected]"
echo "参数个数是:$#"
echo "当前进程PID是:$$"
echo "当前文件名是:$0"
echo '$4=' $4
echo '$5=' $5
echo '$*=' $*
echo '$$=' $#
echo '[email protected]=' [email protected]
echo '$$=' $$
echo '$0=' $0
脚本执行结果如下:
[[email protected] scripts]# sh test.sh 1 2 3 4 5
第3个位置是3
第2个位置是2
第1个位置是1
所有参数是:1 2 3 4 5
所有参数是:1 2 3 4 5
参数个数是:5
当前进程PID是:76280
当前文件名是:test.sh
$4= 4
$5= 5
$*= 1 2 3 4 5
$$= 5
[email protected]= 1 2 3 4 5
$$= 76280
$0= test.sh
关于单引号和双引号的说明
单引号---所见即所得---强引用---针对字符串
双引号---变量的解析---弱引用---针对字符串
3)检查磁盘使用率,如果超过10%,则将结果输入到文件/tmp/Disk_Free.txt
中;否则输出内容“磁盘目前良好”
#!/bin/bash
#先用df -h查看磁盘使用情况
#再用grep匹配到根分区的那一行
#接着用awk找到磁盘使用率并过滤掉百分号,以方便比较
Disk_Free=$(df -h|grep /$|awk '{print $(NF-1)}'|awk -F '%' '{print $1}')
#将结果与10进行比较,此处结果作为echo输出的内容,变量要用大括号括起来
if [ $Disk_Free -gt 10 ];then
echo "disk use is:${Disk_Free}%" >> /tmp/Disk_Free.txt
else
echo "disk use is ok,now is ${Disk_Free}%" >>/tmp/Disk_Free.txt
fi
4)输出Linux系统相关信息。
[[email protected] scripts]# cat test.sh
#!/bin/bash
System=$(hostnamectl|grep System|awk -F ':' '{print $2}')
Kernel=$(hostnamectl|grep Kernel|awk -F ':' '{print $2}')
virtua=$(hostnamectl|grep Virtua|awk -F ':' '{print $2}')
st_hos=$(hostnamectl|grep host|awk -F ':' '{print $2}')
echo "操作系统:$System" >>/tmp/info.txt
echo "系统内核:$Kernel" >>/tmp/info.txt
echo "虚拟平台:$virtua" >>/tmp/info.txt
echo "静态主机:$st_hos" >>/tmp/info.txt
#CPU负载情况
cpu_load1=$(w|grep load|awk -F '[, ]+' '{print $(NF-2)}')
cpu_load5=$(w|grep load|awk -F '[, ]+' '{print $(NF-1)}')
cpu_load15=$(w|grep load|awk -F '[, ]+' '{print $NF}')
echo "CPU1分钟负载是:$cpu_load1" >> /tmp/info.txt
echo "CPU5分钟负载是:$cpu_load5" >> /tmp/info.txt
echo "CPU15分钟负载是:$cpu_load15" >> /tmp/info.txt
echo "==================$(date +%F)=================" >>/tmp/info.txt
5-1)用for循环,通过交互式输入用户前缀和用户数量,用户密码统一设置为123,批量创建用户。
[[email protected] scripts]# vim test.sh
#!/bin/bash
read -p "请输入用户数量:" num
read -p "请输入前缀:" pre
for i in $(seq $num);do
username=$pre$i
useradd $username &> /dev/null
echo 123|passwd --stdin $username &> /dev/null
if [ $? -eq 0 ];then
echo "user $username create ok"
fi
done
要批量删除上述的用户,一行Linux命令即可。
(假设创建了10个用户,boy1 boy2 boy3 … boy10)
for i in boy{
1..10};do userdel $i;done
5-2)在第一问的基础上,先判断输入的创建用户的数量是否为数字
[[email protected] scripts]# cat test.sh
#!/bin/bash
read -p "请输入用户数量:" num
if [[ ! $num =~ ^[0-9]+$ ]];then
echo "错误,请输入数字"
exit 1
fi
read -p "请输入前缀:" pre
for i in $(seq $num);do
username=$pre$i
useradd $username &> /dev/null
echo 123|passwd --stdin $username &> /dev/null
if [ $? -eq 0 ];then
echo "user $username create ok"
fi
done
5-3)在第二问的基础上,判断用户名的前缀是否是字母,并且都可以有无数次试错机会。
[[email protected] scripts]# cat test.sh
#!/bin/bash
read -p "请输入用户数量:" num
while true
do
if [[ $num =~ ^[0-9]+$ ]];then
break
else
echo "错误,请输入数字"
read -p "请输入用户数量:" num
fi
done
read -p "请输入前缀:" pre
while true
do
if [[ $pre =~ ^[a-Z]+$ ]];then
break
else
echo "错误,请输入拼音"
read -p "请输入前缀:" pre
fi
done
for i in $(seq $num);do
username=$pre$i
useradd $username &> /dev/null
echo 123|passwd --stdin $username &> /dev/null
if [ $? -eq 0 ];then
echo "user $username create ok"
fi
done
边栏推荐
- PHP array sort
- 为什么有些参数reload就可以生效,而有些参数必须重启数据库?
- Win11 Beta 22621.436和22622.436有什么区别?
- How can VR panoramic display attract users' attention in a new way of online promotion?
- MySQL系列三:函数&索引&视图&错误代码编号含义
- Pytorch deep learning practice-b station Liu erden-day1
- How does win11 close the touch pad? Three solutions for closing the touch panel in win11
- Buu misc advanced
- 数据湖(十八):Flink与Iceberg整合SQL API操作
- Mongodb query statement >, & gt;=、& lt;、& lt;=、=、!=、 In, not in usage introduction
猜你喜欢
LCD notes (1) LCD hardware operation principle of different interfaces
Graduation thesis on production line balance optimization [Flexsim simulation]
Niuke net brush question 1 [Fibonacci sequence of minimum steps]
MySQL join and index
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
Virtual machine performance test scheme
cpd配准存在的问题
Report design tool FastReport online designer v2022.1 full introduction to new changes
[FPGA tutorial case 35] communication case 5 - 16QAM modulation signal generation based on FPGA, and its constellation is tested by MATLAB
Implementation of MATLAB mixer
随机推荐
Can the values of multiple fields be judged at the same time in MySQL query
Pytorch deep learning practice-b station Liu erden-day1
中国企业管理软件走向全球化国际化的路径探讨
【计网】(三)超网、路由、NAT协议
Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
Android interview question: what is the difference between pendingintent and intent?
[HMS core] [push kit] set of message classification problems
Discussion on the path of Chinese enterprise management software towards globalization and internationalization
MySQL 增删改查(進階)
354. Russian Doll envelope problem
以CRM系统为案例讲解数据分析(重要性介绍及分析方法)
An annotation implementation method writes the returned data to the cache (facet, redis Implementation)
Guys, when Flink SQL job submits a job to yarn, it reports an SQL error that cannot be executed. If it is executed locally, it does not report an error. The server
PostgreSQL determines whether it is empty coalesce
STL map
Oracle怎么设置创建时不去检查编译错误?
NFS共享存儲服務
[learning notes, dog learning C] deeply understand arrays and pointers
家庭琐事问题
Talking about DOM objects in depth, use four versions of demoplus to break the history.state header (more)