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

shell脚本基础(if/else结构)

命令是双向选择语句,当用户执行脚本时如果不满足if后的表达式也会执行else后的命令,所以有很好的交互性。其结构为:
if expression1
then
command

command
else
command

command
fi

++++++++++++++++++++++++++++++++++++++++++++++

vim ifelse_exam.sh 
#ifelse_exam.sh
#!/bin/bashecho "Please input the file which you want to delete:"
read fileread -p "Please input the file which you want to delete:" fileif rm -f "$file"
thenecho "Delete the file $file  sucessfully!"
elseecho "Delete the file $file failed!"
fi

++++++++++++++++++++++++++++++++++++++++++++++

vim ifelse_exam1.sh 
# ifelse_exam1: input a file name and test the file exist or not.
#!/bin/bash
if [ ! -e "$1" ]
thenecho "file $1 do not exist."exit 1
elseecho "file $1 exits."
fi

++++++++++++++++++++++++++++++++++++++++++++++

vim  ifelse_exam2.sh 
#ifelse_exam2.sh
#!/bin/bash
if [ "$1" ]
then   echo "What you input is not null!"if [ -e "$1" ]thenecho "The file $1 is existence !"elseecho " The file $1 is not existence !"fi
elseecho "what you input is null!"
fi

++++++++++++++++++++++++++++++++++++++++++++++

vim ifelse_exam3.sh 
#!/bin/bash
# script to determine a letter grade from a test score#提示用户输入分数(0~100)
echo "Please Input a integer(0-100): "
read score#判断学生的分数类别
if [ "$score" -lt 0 -o "$score" -gt 100 ]
thenecho "The score what you input is not integer or the score is not in (0-100)."
elseif [ "$score" -ge 90 ]thenecho "The grade is A!"elseif [ "$score" -ge 80 ]thenecho "The grade is B!"elseif [ "$score" -ge 70 ]thenecho "The grade is C!"elseif [ "$score" -ge 60 ]thenecho "The grade is D!"elseecho "The grade is E!"fifififi
fi

++++++++++++++++++++++++++++++++

# vim love.sh
#!/bin/bash
read -p "请输入你是否爱我,我爱你|我不爱你:" loveif [  我爱你 == "$love" ]
thenecho "我也爱你!"
elseecho "我也不爱你!"
fi
# chmod +x love.sh
# ./love.sh
http://www.lryc.cn/news/350079.html

相关文章:

  • 万字长文破解 AI 图片生成算法-Stable diffusion (第一篇)
  • Linux---编辑器vim的认识与简单配置
  • lucene中Collector类、CollectorManager类区分和用法
  • Android之给Button上添加按压效果
  • python EEL + vue3.js 项目中如何把组件中的函数提升为全局函数
  • sqli-labs靶场第十四关
  • 【C语言】6.C语言VS实用调试技巧(1)
  • AIGC行业现在适合进入吗
  • ubuntu CUDA 驱动更新,版本更新,多CUDA版本管理
  • effective python学习笔记_类与接口
  • 如何去除字符串两侧的空白字符?
  • Flutter 中的 PageStorage 小部件:全面指南
  • 头歌实践教学平台:CG1-v2.0-直线绘制
  • Nacos+GateWay 搭建微服务架构
  • 【2024华为HCIP831 | 高级网络工程师之路】刷题日记(18)
  • 在抖音做电商,没有货源,不懂直播怎么办?分享一种解决方案!
  • 基于单片机的智能安防系统设计(32+4G+WIFI版)-设计说明书
  • 云服务器配置mysql允许被远程连接从而使用图形化界面
  • 【软件测试】需求概念|软件的⽣命周期|开发模型|测试模型
  • SQL中的LAG函数与LEAD函数用法
  • 数据结构------二叉树经典习题1
  • 汇聚荣:拼多多长期没有流量如何提高?
  • Chrome的常用操作总结
  • dvwa靶场 JavaScript Attacks(js攻击)全难度教程(附代码分析)
  • Flutter 中的 checkboxListTile 小部件:全面指南
  • 前馈神经网络FNN、多层感知机MLP和反向传播推导
  • QML笔记八
  • 运维别卷系列 - 云原生监控平台 之 00.prometheus 监控汇总
  • 信息系统安全与对抗-网络侦查技术与网络扫描技术(期末复习简答题)
  • 【python量化交易】—— Alpha选股策略 - Qteasy自定义交易策略【附源码】