当前位置: 首页 > news >正文

shell脚本案例

一、set命令实现脚本安全

[root@localhost ~]# set -o
allexport      	off
braceexpand    	off
emacs          	on
errexit        	off
errtrace       	off
functrace      	off
hashall        	on
histexpand     	on
history        	on
ignoreeof      	off
interactive-comments	on
keyword        	off
monitor        	on
noclobber      	off
noexec         	off
noglob         	off
nolog          	off
notify         	off
nounset        	off
onecmd         	off
physical       	off
pipefail       	off
posix          	off
privileged     	off
verbose        	off
vi             	off
xtrace         	off
[root@localhost ~]# vim shell/error.sh
#!/bin/bash
echo "Hello World"
sssss
echo "Hello Linux"
[root@localhost ~]# chmod +x shell/error.sh 
[root@localhost ~]# shell/error.sh 
Hello World
shell/error.sh:行3: sssss: 未找到命令
Hello Linux
[root@localhost ~]# vim shell/error.sh
#!/bin/bash
set -e
echo "Hello World"
sssss
echo "Hello Linux"
[root@localhost ~]# shell/error.sh 
Hello World
shell/error.sh:行4: sssss: 未找到命令

二、通过代码演示一下三种形式的区别

#!/bin/bash
n=74
str1=c.biancheng.net$n 
str2="shell \"Script\" $n"
str3='C语言中文网 $n'
echo $str1
echo $str2
echo $str3# 运行结果
c.biancheng.net74
shell "Script" 74
C语言中文网 $n

三、Shell字符串拼接

#!/bin/bash
name="shell"
url="http://c.biancheng.net/shell/"
str1=$name$url #中间不能有空格
str1=$name":"$url
str2="$name $url" #如果被双引号包围,那么中间可以有空格,也可以出现别的字符串
str3="$name:$url" 
str4="${name}Script:${url}Index.html" #在变量后加上字符串,需要给变量名加上大括号

四、常用转义字符

[root@localhost ~]# printf "%s\n" 1 2 3 4
1
2
3
4
[root@localhost ~]# printf "%f\n" 1 2 3 4
1.000000
2.000000
3.000000
4.000000
[root@localhost ~]# printf "%.2f\n" 1 2 3 4 #.2f表示保留两位小数
1.00
2.00
3.00
4.00 
[root@localhost ~]# printf "(%s)" 1 2 3 4;echo " "
(1)(2)(3)(4) 
[root@localhost ~]# printf " (%s) " 1 2 3 4;echo " "(1)  (2)  (3)  (4)  
[root@localhost ~]# printf " (%s) (%s)\n" 1 2 3 4;echo " "(1) (2)(3) (4)
[root@localhost ~]# printf " %s %s\n" 1 2 3 4;echo " "1 23 4
[root@localhost ~]# printf "%s %s %s\n" 1 2 3 4
1 2 3
4  
[root@localhost ~]# #%-10s表示宽度10个字符,左对齐
[root@localhost ~]# printf "%-10s %-10s %-4s %s \n" 姓名 性别 年龄 体重 小明 男性 20岁 70KG 小红 女性 18岁 50KG 
姓名     性别     年龄 体重 
小明     男性     20岁 70KG 
小红     女性     18岁 50KG 
[root@localhost ~]# #将十进制的1000转换为16进制数
[root@localhost ~]# printf "%X\n" 1000
3E8
[root@localhost ~]# printf "%x\n" 1000
3e8
[root@localhost ~]# #将十六进制的C转换为十进制
[root@localhost ~]# printf "%d\n" 0xc
12
[root@localhost ~]# [root@localhost ~]# var="welcome to study";printf "\033[31m%s\033[0m\n" $var
welcome
to
study
[root@localhost ~]# var="welcome to study";printf "\033[31m%s\033[0m\n" "$var"
welcome to study
[root@localhost ~]#

五、使用bc计算小数和declare -i计算

[root@localhost ~]# echo "scale=3;20/3"|bc
6.666
[root@localhost ~]# echo "scale=3;2/3"|bc
.666
[root@localhost ~]# i=20
[root@localhost ~]# j=20
[root@localhost ~]# declare -i sum=i*j
[root@localhost ~]# echo $sum
400
[root@localhost ~]# 

六、生成0-49之间的随机数

[root@localhost ~]# echo $[$[$RANDOM%50]+1]
40
[root@localhost ~]# echo $[$RANDOM%50]
44
[root@localhost ~]# echo $[$[$RANDOM%50]+1]  #生成1~50之间的随机数
[root@localhost ~]# echo $[RANDOM % 100 + 1]

七、生成随机颜色字符串

[root@localhost ~]# echo -e "\033[1;$[RANDOM%7+31]mStudy\033[0m"
Study
[root@localhost ~]# echo -e "\033[1;$[RANDOM%7+31]mStudy\033[0m"
Study
[root@localhost ~]#

八、鸡兔同笼问题

[root@localhost ~]# vim shell/chicken.sh
#!/bin/bash
HEAD=35
FOOT=94RABBIT=$(((FOOT-HEAD-HEAD)/2))
CHOOK=$[35-RABBIT]
echo "兔子的数量为:"$RABBIT
echo "鸡的数量为: "$CHOOK[root@localhost ~]# chmod +x shell/chicken.sh 
[root@localhost ~]# shell/chicken.sh 
兔子的数量为::12
鸡的数量为:23
[root@localhost ~]# # 在脚本中写入变量,让用户在命令行写入需要计算的数值
[root@localhost ~]# vim shell/chicken.sh
#!/bin/bash
HEAD=$1
FOOT=$2RABBIT=$(((FOOT-HEAD-HEAD)/2))
CHOOK=$[HEAD-RABBIT]
echo "兔子的数量为:"$RABBIT
echo "鸡的数量为: "$CHOOK[root@ansible-salve1 ~]# ./chicken.sh 30 80
兔子的数量为:10
鸡的数量为: 25
[root@ansible-salve1 ~]# 

九、计算出所有人的年龄总和

[root@ansible-salve1 shell]# vim nianling.txt
[root@ansible-salve1 shell]# cat nianling.txt
a=20
b=18
c=22
[root@ansible-salve1 shell]# cut -d"=" -f2 nianling.txt 
20
18
22
[root@ansible-salve1 shell]# cut -d"=" -f2 nianling.txt | tr '\n' + | grep -Eo ".*[0-9]" | bc
60
[root@ansible-salve1 shell]# grep -Eo "[0-9]+" nianling.txt 
20
18
22
[root@ansible-salve1 shell]# grep -Eo "[0-9]+" nianling.txt | tr '\n' +| grep -Eo ".*[0-9]" | bc
60
[root@ansible-salve1 shell]# 

十、文件测试表达式

[root@ansible-salve1 ~]# test -a test.txt
[root@ansible-salve1 ~]# echo $?
1
[root@ansible-salve1 ~]# test -d shell
[root@ansible-salve1 ~]# echo $?
0
[root@ansible-salve1 ~]# touch test.txt
[root@ansible-salve1 ~]# test -w test.txt &&echo  "true"
true
[root@ansible-salve1 ~]# test -r test.txt &&echo  "true"
true
[root@ansible-salve1 ~]# test -x test.txt &&echo  "true"
[root@ansible-salve1 ~]# 

十一、测试字符串

[root@ansible-salve1 ~]# var_test='Mike' 
[root@ansible-salve1 ~]# echo $var_test 
Mike
[root@ansible-salve1 ~]# [[ -n var_test ]] && echo "True"
True# 通配符
[root@ansible-salve1 ~]# FILE=test.log
[root@ansible-salve1 ~]# [[ "$FILE" == *.log ]] && echo "True"
True
[root@ansible-salve1 ~]# FILE=test.txt
[root@ansible-salve1 ~]# [[ "$FILE" == *.log ]] && echo "True"
[root@ansible-salve1 ~]# [[ "$FILE" != *.log ]] && echo "True"
True
[root@ansible-salve1 ~]# # 扩展的正则表达式
[root@ansible-salve1 ~]# FILE=test.log
[root@ansible-salve1 ~]# [[ "$FILE" =~  \.log$ ]] && echo "True"
True
[root@ansible-salve1 ~]# N=100
[root@ansible-salve1 ~]# [[ "$N" =~ ^[0-9]+$ ]] && echo "True"
True
[root@ansible-salve1 ~]# N=A10
[root@ansible-salve1 ~]# [[ "$N" =~ ^[0-9]+$ ]] && echo "True"
[root@ansible-salve1 ~]# IP=1.2.3.4
[root@ansible-salve1 ~]# [[ "$IP" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
[root@ansible-salve1 ~]# echo $?
0
[root@ansible-salve1 ~]# 
# 使用正则表达式判断IP是否合法
[root@ansible-salve1 ~]# IP=1.2.3.333
[root@ansible-salve1 ~]# [[ $IP =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]
[root@ansible-salve1 ~]# echo $?
1
[root@ansible-salve1 ~]# IP=255.255.255.255
[root@ansible-salve1 ~]# [[ $IP =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]
[root@ansible-salve1 ~]# echo $?
0
[root@ansible-salve1 ~]# 

十二、磁盘空间的判断

# 查看磁盘空间占用率,若超出80则提示空间不足。
[root@localhost ~]# df | head -2 | tail -1 | tr -s ' ' | cut -d' ' -f5 | cut -d'%' -f1
5
[root@localhost ~]# used_precent=$(df | head -2 | tail -1 | tr -s ' ' | cut -d' ' -f5 | cut -d'%' -f1)
[root@localhost ~]# echo $used_precent
5
[root@localhost ~]# [ $used_precent -gt 80 ] && echo "磁盘空间不足!" || echo "磁盘空间充足!"
磁盘空间充足!

十三、使用read给多个变量赋值并输出

[root@ansible-salve1 shell]# vim info.sh 
#!/bin/bash
read -p "Enter some information > " name url age
echo "网站名字:$name"
echo "网址:$url"
echo "年龄:$age"
[root@ansible-salve1 shell]# chmod +x info.sh 
[root@ansible-salve1 shell]# ./info.sh 
Enter some information > hehao www.baidu.com 18
网站名字:hehao
网址:www.baidu.com
年龄:18
[root@ansible-salve1 shell]# # 注意:必须在一行内输入所有的值,不能换行,否则只能给第一个变量赋值,后续变量都会赋值失败

十四、只读取一个字符

# -n 1表示只读取一个字符,运行脚本后,只要用户输入一个字符,立即就读取结束,不等待用户按下回车键
[root@ansible-salve1 shell]# vim info1.sh 
#!/bin/bash
read -n 1 -p "Enter a char > " char  && printf "\n"
echo "---------------------------------------------------------"
echo $char
[root@ansible-salve1 shell]# chmod +x info1.sh 
[root@ansible-salve1 shell]# ./info1.sh 
Enter a char > a
---------------------------------------------------------
a

十五、在指定时间内输入密码

#使用&&组合了多个命令,这些命令会依次执行,并且从整体上作为 if 语句的判断条件,只要其中一个命令执行失败(退出状态为非 0 值),整个判断条件就失败了,后续的命令也就没有必要执行
[root@ansible-salve1 shell]# vim info2.sh
#!/bin/bash
ifread -t 20 -sp "Enter password in 20 seconds(once) > " pass1 && printf "\n" &&  #第一次输入密码read -t 20 -sp "Enter password in 20 seconds(again)> " pass2 && printf "\n" &&  #第二次输入密码[ $pass1 == $pass2 ]  #判断两次输入的密码是否相等
thenecho "Valid password"
elseecho "Invalid password"
fi
[root@ansible-salve1 shell]# chmod +x info2.sh 
[root@ansible-salve1 shell]# ./info2.sh 
Enter password in 20 seconds(once) > 
Enter password in 20 seconds(again)> 
Valid password
[root@ansible-salve1 shell]# 

十六、利用正则表达式

[root@ansible-salve1 shell]# vim info3.sh
[root@ansible-salve1 shell]# cat info3.sh 
#!/bin/bash
read -p "Are you rich? yes or no: " ANSWER
[[ $ANSWER =~ ^[Yy]|[Yy][Ee][Ss]$ ]] && echo "You Are Rich" || echo "Good Good Study,Day Day up"
[root@ansible-salve1 shell]# chmod +x info3.sh 
[root@ansible-salve1 shell]# ./info3.sh 
Are you rich? yes or no: y
You Are Rich
[root@ansible-salve1 shell]# ./info3.sh 
Are you rich? yes or no: YeS
You Are Rich
[root@ansible-salve1 shell]# 

十七、工作选项

[root@ansible-salve1 shell]# vim backup.sh 
#!/bin/bash
SRC=/etc/
DEST=/data/backup_`date +%F_%H-%M-%S`
cp -a $SRC $DEST
[ $? -eq 0 ] && echo "备份成功" || echo "备份失败"
[root@ansible-salve1 shell]# chmod +x backup.sh
[root@ansible-salve1 shell]# vim info4.sh
#!/bin/bash
cat <<EOF
请选择:
1.备份文件
2.清理日志文件
3.软件升级
4.软件回滚
5.删库跑路
EOF
read -p "请输入上面的数字1-5:" MENU
[ $MENU -eq 1 ] && ./backup.sh
[ $MENU -eq 2 ] && echo "清理日志文件"
[ $MENU -eq 3 ] && echo "软件升级"
[ $MENU -eq 4 ] && echo "软件回滚"
[ $MENU -eq 5 ] && echo "删除跑路"[root@ansible-salve1 shell]# chmod +x info4.sh 
[root@ansible-salve1 shell]# ./info4.sh 
请选择:
1.备份数据库
2.清理日志文件
3.软件升级
4.软件回滚
5.删库跑路
请输入上面的数字1-5:1
备份成功
[root@ansible-salve1 shell]# 

http://www.dtcms.com/a/364052.html

相关文章:

  • 【Unity Shader学习笔记】(二)图形显示系统
  • nmap扫描端口,netstat
  • 二叉树经典题目详解(下)
  • CH01-1.1 Exercise-Ordinary Differential Equation-by LiuChao
  • 猫猫狐狐的“你今天有点怪怪的”侦察日记
  • 标贝科技参编《数据标注产业发展研究报告(2025 年)》
  • ARM裸机开发(GPIO标准库开发)
  • Java搭建高效后端,Vue打造友好前端,联合构建电子采购管理系统,实现采购流程电子化、自动化,涵盖采购全周期管理,功能完备,附详细可运行源码
  • 提高卷积神经网络模型的一些应用
  • 复刻 Python 实现的小智语音客户端项目py-xiaozhi日记
  • AI助力开发:JetBrains官方DeepSeek插件Continue一站式上手!
  • 为什么研发文档的变更缺乏审批和追溯
  • 2025 大学生职业准备清单:从数据到财会,这些核心证书值得考
  • 毕业项目推荐:70-基于yolov8/yolov5/yolo11的苹果成熟度检测识别系统(Python+卷积神经网络)
  • Spring 循环依赖问题
  • 【代码随想录day 22】 力扣 40.组合总和II
  • 威科夫与强化学习状态
  • Spring Security 如何使用@PreAuthorize注解
  • srm信息系统数字化采购(程序代码部署程序包源码Java)
  • 实验3-传输层协议分析
  • [Java]PTA:jmu-Java-01入门-取数字浮点数
  • CentOS7安装Nginx服务——为你的网站配置https协议和自定义服务端口
  • js 获取字符串第一个字符
  • 《Visual Abstraction: A Plug-and-Play Approach for Text-Visual Retrieval》
  • 从 “容器保姆” 到 “云原生王者”:K8s 全方位指南
  • UCIE Specification详解(十三)
  • EPLAN 分散式端子:提升原理图设计效率的实用功能
  • 【C++】深入解析C++嵌套依赖类型与typename关键字
  • Jenkins Pipeline 语法
  • 【机器人概念设计软件操作手册】建筑与环境建模