当前位置:网站首页>Shell script case ---2
Shell script case ---2
2022-07-24 12:23:00 【Stick stick eat not fat】
1) please ping Pass Baidu domain name 3 Time , Different information is output for success or failure .
Complete in three ways .
Mode one :
The point to note here is , If 3 Tidu ping through ,&& It will take effect , That is, execute the following commands ,ping www.baidu.com ok
3 Once only once no ping through ,|| take effect , Will execute the following commands ,ping www.baidu.com error
#!/bin/bash
IP=www.baidu.com
ping -c3 $IP &>/dev/null && echo "ping $IP ok" || echo "ping $IP error"
Mode two :
use if Statement completion non interactive ping website
Non interactive as follows :
#!/bin/bash
# Defining variables
ip=www.baidu.com
# only ping Three times without outputting anything
ping -c3 $ip &>/dev/null
# Judge the execution result of the previous statement , If the execution is successful, it will be 0; It is 1
if [ $? -eq 0 ];then
echo "$ip ok"
else
echo "$ip error"
fi
Mode three :
use if Statement completes interactive ping website
Interactive as follows :
#!/bin/bash
# After executing the script , User input variables IP, And there are prompt statements
read -p "please input network ip:" IP
# only ping Three times without outputting anything
ping -c3 $IP &>/dev/null
# Judge the execution result of the previous statement , If the execution is successful, it will be 0; It is 1
if [ $? -eq 0 ];then
echo "$IP ok"
else
echo "$IP error"
fi
2) The meaning of the relevant parameters .
primary shell The script is as follows :
[[email protected] scripts]# cat test.sh
#!/bin/bash
echo " The first 3 A place is $3"
echo " The first 2 A place is $2"
echo " The first 1 A place is $1"
echo " All the parameters are :$*"
echo " All the parameters are :[email protected]"
echo " The number of parameters is :$#"
echo " The current process PID yes :$$"
echo " The current file name is :$0"
echo '$4=' $4
echo '$5=' $5
echo '$*=' $*
echo '$$=' $#
echo '[email protected]=' [email protected]
echo '$$=' $$
echo '$0=' $0
The execution result of the script is as follows :
[[email protected] scripts]# sh test.sh 1 2 3 4 5
The first 3 A place is 3
The first 2 A place is 2
The first 1 A place is 1
All the parameters are :1 2 3 4 5
All the parameters are :1 2 3 4 5
The number of parameters is :5
The current process PID yes :76280
The current file name is :test.sh
$4= 4
$5= 5
$*= 1 2 3 4 5
$$= 5
[email protected]= 1 2 3 4 5
$$= 76280
$0= test.sh
About single quotation marks and double quotation marks
Single quotation marks --- What you see is what you get --- Strong citation --- For Strings
Double quotes --- Analysis of variables --- Weak reference --- For Strings
3) Check disk usage , If exceeded 10%, Then input the results into the file /tmp/Disk_Free.txt in ; Otherwise output content “ The disk is currently in good condition ”
#!/bin/bash
# First use df -h Check disk usage
# Reuse grep Match to the row of the root partition
# Then use awk Find the disk usage and filter out the percent sign , To facilitate comparison
Disk_Free=$(df -h|grep /$|awk '{print $(NF-1)}'|awk -F '%' '{print $1}')
# Compare the results with 10 Compare , The result here is as echo Output content , Variables should be enclosed in braces
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) Output Linux System related information .
[[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 " operating system :$System" >>/tmp/info.txt
echo " system kernel :$Kernel" >>/tmp/info.txt
echo " Virtual platform :$virtua" >>/tmp/info.txt
echo " Static host :$st_hos" >>/tmp/info.txt
#CPU Load condition
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 The minute load is :$cpu_load1" >> /tmp/info.txt
echo "CPU5 The minute load is :$cpu_load5" >> /tmp/info.txt
echo "CPU15 The minute load is :$cpu_load15" >> /tmp/info.txt
echo "==================$(date +%F)=================" >>/tmp/info.txt
5-1) use for loop , Input user prefix and number of users interactively , The user password is uniformly set to 123, Mass create users .
[[email protected] scripts]# vim test.sh
#!/bin/bash
read -p " Please enter the number of users :" num
read -p " Please enter a prefix :" 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
To delete the above users in batch , a line Linux Command is enough .
( Suppose you create 10 Users ,boy1 boy2 boy3 … boy10)
for i in boy{
1..10};do userdel $i;done
5-2) On the basis of the first question , First, judge whether the number of created users entered is a number
[[email protected] scripts]# cat test.sh
#!/bin/bash
read -p " Please enter the number of users :" num
if [[ ! $num =~ ^[0-9]+$ ]];then
echo " error , Please enter a number "
exit 1
fi
read -p " Please enter a prefix :" 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) On the basis of the second question , Determine whether the prefix of the user name is a letter , And you can have countless opportunities to try and make mistakes .
[[email protected] scripts]# cat test.sh
#!/bin/bash
read -p " Please enter the number of users :" num
while true
do
if [[ $num =~ ^[0-9]+$ ]];then
break
else
echo " error , Please enter a number "
read -p " Please enter the number of users :" num
fi
done
read -p " Please enter a prefix :" pre
while true
do
if [[ $pre =~ ^[a-Z]+$ ]];then
break
else
echo " error , Please enter Pinyin "
read -p " Please enter a prefix :" 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
边栏推荐
- [Commons beanautils topic] 004 beanautils topic
- Snowflake algorithm (PHP)
- Online XML to CSV tool
- How to use a third party without obtaining root permission topic: MIUI chapter
- 6-16漏洞利用-rlogin最高权限登陆
- [mathematical basis of Cyberspace Security Chapter 9] finite field
- What is prescaler in STM32
- C Advanced - data storage
- 02 linear structure 2 multiplication and addition of univariate polynomials (linked list solution)
- 如何将Typora中图片上传到csdn
猜你喜欢

QT notes - custom data types

Leetcode:51. queen n

QT notes - EventFilter event filter

NFT digital collection system construction - app development

In kuborad graphical interface, operate kubernetes cluster to realize master-slave replication in MySQL

Wechat applet - drawing dashboard

What is prescaler in STM32

TypeNameExtractor could not be found
![Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]](/img/ee/e0770298d0534014485145c434491a.png)
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]

Installation and deployment of ansible
随机推荐
Buckle practice - 31 effective tic tac toe games
【C和指针第11章】动态内存分配
Snowflake algorithm (PHP)
Why is the datetime type field 8 hours earlier when I use flinkcdc to read MySQL's binlog?
Buckle practice - sum of 34 combinations
Understand the storage and retrieval of data
Day4: circular structure
【网络空间安全数学基础第3章】同余
基于ARM和FPGA的数字示波器设计——QMJ
6-16漏洞利用-rlogin最高权限登陆
计算两个坐标经纬度之间的距离(5种方式)
CCF 1-2 question answering record (1)
4*4图片权重的收敛规则
【我也想刷穿 LeetCode啊】468. 验证IP地址
[rust] what software should I use to develop rust? Recommended editors commonly used to support rust
GCC的基本用法
Wechat applet learning five page Jump methods
L1-059 ring stupid bell
NFT digital collection system construction - app development
Basic usage of GCC