Linux学习笔记(9)之Shell编程入门--判断语句
来源:互联网 发布:seo职位 编辑:程序博客网 时间:2024/06/01 09:14
语法一:
if [ 条件表达式 ]
then
命令序列 1
else
命令序列 2
fi
语法二:
if [ 条件表达式 ]; then
命令序列
fi
语法三:
if test 条件表达式 1
then
命令序列 1
elif [ 条件表达式 2 ]
then
命令序列 2
else
命令序列 3
fi
编写shell脚本时,注意条件表达式与“[“ ”]“之间的空格
实例分析:
#!/bin/bash#script4-1.shvar1="welcome to use Shell script"echo $var1pwdls -i#判断当前目录下是否存在某文件#!bin/bash#script4-2.shecho "Enter a filename"read fileif [-f $file]then echo "File $file exists."fi#判断当前用户名和输入的用户名是否一致#!/bin/bash#script4-3.shecho -n "Enter your login name:"read nameif test "$name" = "$USER"then echo "Hello,$name"else echo "You're not $USER"fi#比较两个数的大小#!/bin/bash#script4-4.shecho "Enter the first integer:"read firstecho "Enter the second integer:"read secondif test "$first" -gt "$second" then echo "$first is greater than $second" elif test "$first" -lt "$second" then echo "$first is less than $second" else echo "$first is equal to $second"fi
#判断myfile文件中是否含有“GNU“字符串#!/bin/bash#script4-5.shif grep "GNU" myfile >/dev/nullthen echo "\"GNU\" occurs in myfile"elseechoecho "\"GNU\"does not occurs in myfile"fi
0 0
- Linux学习笔记(9)之Shell编程入门--判断语句
- Linux学习笔记之<Shell编程初入门>
- Linux学习笔记(8)之Shell编程入门--输入输出、引号
- Linux学习笔记之SHELL编程
- 《Linux Shell编程学习笔记之三》
- linux shell编程入门笔记
- linux shell编程入门笔记
- Shell编程之流程控制语句(条件判断)
- shell编程之循环与判断语句
- 学习笔记之Linux网络编程入门
- hadoop学习序曲之linux基础篇--shell编程入门
- Linux Shell编程学习笔记
- linux Shell编程学习笔记
- linux shell编程学习笔记
- Linux Shell编程 学习笔记
- shell编程(条件判断与流程控制)学习笔记
- linux之shell编程笔记
- Linux学习笔记——轻松学Linux shell编程 第一部分:Linux初学者编程入门
- WebKit之Major对象介绍
- Leetcode 第119题 Pascal's Triangle II
- JVM内存管理与调优
- https(ssl)连接之python实现
- WebKit之应用场景的思考
- Linux学习笔记(9)之Shell编程入门--判断语句
- linux socket编程简单例子//代码
- JAVA_SE-----基础知识总结-----多线程
- 黑马程序员--Java编程之数组篇
- 显示一个文件的某几行
- source和build分离
- Java中静态变量与静态方法的继承
- PHP常驻内存与单例模式
- 黑马程序员__Java入门知识,变量,数据类型……