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

shell简单使用(-)判断

记几个判断相关的简单例子

1. 文件相关判断比较

#!/bin/bash
if [ $# -lt 1 ]thenecho "No file name"exit 1
elif [ -d $1 ]thenecho $1 is directory
elif [ -b $1 ]then echo $1 is block device
elif [ -h $1 ]thenecho $1 is link file
elif [ -c $1 ]thenecho $1 is character device
elif [ -f $1 ]thenecho $1 is normal file
else echo "Don't recognize $1"
fiif [ -r $1 ]thenecho $1 is can be read
fiif [ -w $1 ]then echo $1 is can be write
fiif [ -x $1 ]thenecho $1 is can be excute
fiif [ -u $1 ]thenecho $1 has SUID attribution
fiif [ -g $1 ]thenecho $1 has SGID attribution
fiif [ -k $1 ]thenecho $1 has Sticky bit attribution
fiif [ -s $1 ]thenecho $1 is a xxx
fiexit 0

执行结果如下:

2. 数字相关判断

a.

#!/bin/bashif [ "$1" -eq "$2" ]; then
# if [ "$1" -ne "$2" ]; thenecho $1 equal $2fiif [ "$1" -lt "$2" ]; then
# if [ "$1" -le "$2" ]; thenecho $1 less than $2
fiif [ "$1" -gt "$2" ]; then
# if [ "$1" -ge "$2" ]; thenecho $1 grate then $2
fi

b.

#!/bin/bashread -p "Input the age: " agecase $age in[0-9]|[1][0-2])echo "child";;[1][0-9])echo "young";;[2-5][0-9] )echo "adult";;[6-9][0-9])    echo "old";;*    )echo "Not people";;
esac
exit 0

执行结果如下:

   c. 

#!/bin/bash
read -p "Iuput your birthday (MMDD.ex> 0709): " bir
now=`date +%m%d`
if [ "$bir" == "$now" ];thenecho "Happy birthday !!!"
elif [ "$bir" -gt "$now" ];thenyear=`date +%Y`total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))echo "Your birthday will be $total_d later"
elseyear=$((`date +%Y`+1))total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))echo "Your birthday will be $total_d later"
fi

执行结果如下:

3. 字符串比较

#!/bin/bashif [ "$1" == "$2" ]; then
# if [[ "$1" == "$2" ]]; thenecho $1 equal $2
fiif [ "$1" != "$2" ]; thenecho $1 no equal $2
fiif [[ "$1" > "$2" ]]; then
# if [ "$1" \> "$2" ]; thenecho $1 grate then $2
fiif [[ "$1" < "$2" ]]; then
# if [ "$1" \< "$2" ]; thenecho $1 less then $2
fiif [ -z "$1" ]; thenecho $1 is empty
fiif [ -n "$1" ]; thenecho $1 is no empty
fiif [[ "$1" == 1* ]]; thenecho $1 ==  "1*" 1111111111
fiif [[ "$1" == "1*" ]]; thenecho $1 == \"1*\" 2222222222
fi

执行结果如下:

两次对1*的比较,分别是模式匹配和全字符串匹配

http://www.xdnf.cn/news/1467451.html

相关文章:

  • 在Windows中已经启动的容器(比如xinference),如何设置让其在每次Docker启动时能自动启动
  • Java对象在内存中的布局详解
  • 【mysql】SQL查询全解析:从基础分组到高级自连接技巧
  • 如何将联系人从 iPhone 转移到 Redmi 手机
  • 亲戚关系计算器,秒懂全家称呼!
  • 基于YOLO目标检测模型的视频推理GUI工具
  • 超越自动化:为什么说供应链的终局是“AI + 人类专家”的混合智能?
  • Web服务与Nginx详解
  • 【服务器】英伟达M40显卡风冷方案心得
  • Git 工具的「安装」及「基础命令使用」
  • 从零到上线:Docker、Docker Compose 与 Runtime 安装部署全指南(含实战示例与应用场景)
  • 小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
  • DL3382P6平替RClamp3382P.TCT
  • JavaWeb —— 异常处理
  • iPhone17全系优缺点分析,加持远程控制让你的手机更好用!
  • Ubuntu 18.04 上升级 gcc 到 9.4
  • 敏捷开发-Scrum(下)
  • 服务器为啥离不开传感器?一文看懂数据中心“隐形守护者”的关键角色
  • 【前端】使用Vercel部署前端项目,api转发到后端服务器
  • 数据结构初阶:树的相关性质总结
  • 如何使用自签 CA 签发服务器证书与客户端证书
  • 假设一个算术表达式中包含圆括号、方括号和花括号3种类型的括号,编写一个算法来判别,表达式中的括号是否配对,以字符“\0“作为算术表达式的结束符
  • 【Linux系统】POSIX信号量
  • Jenkins环境搭建与使⽤
  • C语言(长期更新)第15讲 指针详解(五):习题实战
  • Kimi K2-0905重磅发布:月之暗面再次引领AI编程新纪元
  • 【Rust 入门】01. 创建项目
  • Rust 的生命周期与借用检查:安全性深度保障的基石
  • 极快文本嵌入推理:Rust构建高性能嵌入推理解决方案
  • Qoder 全面解析:三大模式与开发者实战指南