linux shell 之结构化语法
一、case分支语句
语法格式:
case $变量名 in模式1)命令序列1;;模式2)命令序列2;; *)默认执行的命令序列 ;; esac
case代码实例
#!/bin/bash read -p "press some key ,then press return :" KEY case $KEY in [a-z]|[A-Z]) echo "It's a letter." ;; [0-9]) echo "It's a digit." ;; *) echo "It's function keys、Spacebar or other ksys." ;; esac
其中read -p 是从控制台读取字符串
二、if分支
语法格式
if command thencommands fi或者 if command:thencommands fi或者 if command thencommands elsecommands fi或者if command1 thencommands elif command2 thencommands fi
test命令语法
if test condition thencommands fi或者if [condition] thencommands fi
test数值的比较
n1 -eq n2 检查n1是否与n2相等
n1 -ge n2 检查n1是否大于等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 。。。。。
n1 -lt n2 。。。。。
n1 -ne n2 。。。。。。
test字符串比较
str1 = str2
str1 != str2
str1 < str2
str1 > str2
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
注意,使用的时候<或者 >需要转义\
代码实例
#!/bin/bash var1=10 var2=11if [ $var1 -gt 5 ] thenecho "The test value $var1 is greater than 5" fiif [ $var1 -eq $var2 ] thenecho "The values are equal" elseecho "The valus are fifferent" fi运行结果: The test value 10 is greater than 5 The valus are fifferent需要注意的是[]左右内侧必须有空格,否则运行报错
三、文件比较
比较 | 描述 |
-d file | 检查file是否存在并是一个目录 |
-e file | 检查file是否存在 |
-f file | 检查file是否存在并是一个文件 |
-r file | 检查file是否存在并可读 |
-s file | 检查file是否存在并非空 |
-w file | 检查file是否存在并可写 |
-x file | 检查file是否存在并可执行 |
-O file | 检查file是否存在并属于当前用户所有 |
-G file | 检查file是否存在并且默认组与当前用户相同 |
file1 -nt file2 | 检查file1是否比file2新 |
file1 -ot file2 | 检查file1是否比file2旧 |
代码实例
#!/bin/bash if [ -e $HOME ] thenecho "home exist" elseecho "home not exist" fi
四、for语法
for var in list do commands done
实例
#!/bin/bash for var in Alaska Ae we wer doecho the next state is $var done运行结果: $ sh fortest.sh the next state is Alaska the next state is Ae the next state is we the next state is wer
解析文本文件,并循环输出文件内容
#!/bin/bash IFS.OLD=$IFS IFS=$'\n' for entry in `cat /etc/passwd` doecho "values in $entry"IFS=:for value in $entrydoecho " $value"done doneIFS=$IFS.OLD
while,until,..
linux shell 之结构化语法相关推荐
- Linux shell 脚本结构化命令 if-then
shell 脚本结构化命令 1. if-then 语句基本使用 if-then 语句的基本格式: if command thencommands elifcommands elsecommands f ...
- linux SHELL之结构化命令
SHELL之使用结构化命令 使用if-then语句 Bash代码 if command then commands fi 如果if后面的命令退出状态码=0,那么就执行then 另外一种形式 Bas ...
- linux shell 脚本编程基本语法
linux shell脚本编程基本语法 Shell script是利用shell的功能所写的一个"程序",这个程序是使用纯文本文件,将一些Linux Shell的语法与命令(含外部 ...
- Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)...
一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...
- 使shell用结构化命令
shell--使用结构化命令 使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻 ...
- Linux shell 的条件 / 比较语法
Shell 其实并不常写,每次写的时候就总得查一下比较语句的语法该怎么写.本文章就作为笔记记录这些资料吧. 本文地址:https://segmentfault.com/a/11900000060978 ...
- linux shell 三元运算符,关于语法:Bash中的三元运算符(?:)
有什么办法可以做这种事吗 int a = (b == 5) ? c : d; 使用BASH? @荷兰语的答案表明,bash确实有类似于"三元运算符"的东西,但是在bash中,这被称 ...
- linux shell的for循环语法是怎样的?
答:如下: for ((i=0;i<100;i++))doecho "i=${i}"done 转载于:https://www.cnblogs.com/dakewei/p/10 ...
- Linux循环中累计数据,linux shell 读取for循环中出现难处理的数据之单引号错误实例...
原语句: #!/bin/bash for test in I don't know if this'll work do echo "work:$test" done 结果: wo ...
最新文章
- python 代码-代码的重试机制(python简单实现)
- 【目录】Python 入门基础篇 <(^-^)>
- 笑话(15) 这是地球
- 数据库 MySQL 如何设置表的主键自增起始值
- 线程控制 12 | 线程属性
- 第一个将Palette Mode引入VVC(H.266),阿里云在JVET会议上引起关注
- flutter tab选项卡appbar下的选项卡
- 使用Maven自动部署Java Web项目到Tomcat问题小记
- 江西单招主计算机学校,2019年江西单招学校,江西单招信息技术试题,2019江西单招综合素质...
- Java程序设计基础——cmd编译运行java文件详细步骤
- Java关键字this详解
- 围观了张一鸣近10年的微博,我整理了这231条干货
- json在线解析(json在线解析)
- U盘装系统后容量变小解决方法
- 计算机专业博士阶段研究方向,国内计算机专业博士研究方向
- SQL Server Management Studio(SSMS)对象资源管理器的使用以及查询分析器的使用
- matplotlib函数库使用imshow绘制像素图片
- ssm+java农村快递代取平台52wxh(程序+lw+源码+远程部署)
- 在安卓手机上安装Linux子系统
- 《X3D: Expanding Architectures for Efficient Video Recognition》论文详解