Linux Shell脚本编程case条件语句
1,判断一个数字是否则在1,2,3之中.
#!/bin/bash
read -p "pls input a number:" n
case "$n" in
)
echo "变量是1"
;;
)
echo "变量是2"
;;
)
echo "变量是3"
;;
*)
echo "pls input a number between 1 and 3"
exit;
esac
2,多级if语句改写
#!/bin/bash
read -p "pls input a number:" n
if [ $n -eq ]; then
echo "$n是变量1"
elif [ $n -eq ]; then
echo "$n是变量2"
elif [ $n -eq ]; then
echo "$n是变量3"
else
echo "pls input a number between 1 and 3"
fi
3,if..else嵌套,实现
#!/bin/bash
read -p "pls input a number:" n
if [ $n -eq ]; then
echo
else
if [ $n -eq ]; then
echo
elif [ $n -eq ]; then
echo
else
echo "pls input a number [1-3]"
fi
fi
4,判断 分数等级
#!/bin/bash read -p "pls input score to test level:" score if [ $score -ge ]; then
echo "优秀"
elif [ $score -ge ]; then
echo "良好"
elif [ $score -ge ]; then
echo "中等"
elif [ $score -ge ]; then
echo "及格"
else
echo "不及格"
fi
5,给文字加颜色
#!/bin/bash
RED_COLOR='\e[1;31m'
GREEN_COLOR='\e[1;32m'
YELLOW_COLOR='\e[1;33m'
BLUE_COLOR='\e[1;34m'
RESET_COLOR='\e[0m' echo '
, 悟空
, 八戒
, 唐僧
, 白龙马
'
read -p "pls input a number:" n case $n in
)
echo -e "${RED_COLOR}悟空${RESET_COLOR}"
;;
)
echo -e "${GREEN_COLOR}八戒${RESET_COLOR}"
;;
)
echo -e "${YELLOW_COLOR}唐僧${RESET_COLOR}"
;;
)
echo -e "${BLUE_COLOR}白龙马${RESET_COLOR}"
;;
*)
echo "you need input a number in {1|2|3|4}"
esac
另一种写法:
#!/bin/bash
RED_COLOR='\e[1;31m'
GREEN_COLOR='\e[1;32m'
YELLOW_COLOR='\e[1;33m'
BLUE_COLOR='\e[1;34m'
RESET_COLOR='\e[0m' function menu(){
cat <<END
, 悟空
, 八戒
, 唐僧
, 白龙马
END
} function select_type(){
read -p "pls input a number:" n
case $n in
)
echo -e "${RED_COLOR}悟空${RESET_COLOR}"
;;
)
echo -e "${GREEN_COLOR}八戒${RESET_COLOR}"
;;
)
echo -e "${YELLOW_COLOR}唐僧${RESET_COLOR}"
;;
)
echo -e "${BLUE_COLOR}白龙马${RESET_COLOR}"
;;
*)
echo "you need input a number in {1|2|3|4}"
esac
} function main(){
menu
select_type
} main
读取命令行参数,给内容设置颜色
#!/bin/bash
RED_COLOR='\e[1;31m'
GREEN_COLOR='\e[1;32m'
YELLOW_COLOR='\e[1;33m'
BLUE_COLOR='\e[1;34m'
RESET_COLOR='\e[0m' if [ $# -ne ]; then
echo "Usage $0 input {red|green|yellow|blue}"
exit
fi case $ in
red)
echo -e "${RED_COLOR}$1${RESET_COLOR}"
;;
green)
echo -e "${GREEN_COLOR}$1${RESET_COLOR}"
;;
yellow)
echo -e "${YELLOW_COLOR}$1${RESET_COLOR}"
;;
blue)
echo -e "${BLUE_COLOR}$1${RESET_COLOR}"
;;
*)
echo "usage $0 input {red|green|yellow|blue}"
exit
esac
修改成函数调用方式
#!/bin/bash
function toColor(){
RED_COLOR='\e[1;31m'
GREEN_COLOR='\e[1;32m'
YELLOW_COLOR='\e[1;33m'
BLUE_COLOR='\e[1;34m'
RESET_COLOR='\e[0m' if [ $# -ne ]; then
echo "Usage $0 input {red|green|yellow|blue}"
exit
fi case $ in
red)
echo -e "${RED_COLOR}$1${RESET_COLOR}"
;;
green)
echo -e "${GREEN_COLOR}$1${RESET_COLOR}"
;;
yellow)
echo -e "${YELLOW_COLOR}$1${RESET_COLOR}"
;;
blue)
echo -e "${BLUE_COLOR}$1${RESET_COLOR}"
;;
*)
echo "usage $0 input {red|green|yellow|blue}"
exit
esac
} function main(){
toColor $ $
} main $*
Linux Shell脚本编程case条件语句的更多相关文章
- Linux Shell脚本编程while语句
Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo uptime sleep 3done 2,把监控结果保存 ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- SHELL脚本编程的条件测试
SHELL脚本编程的条件测试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.条件测试概述 判断某需求是否满足,需要由测试机制来实现 专用的测试表达式需要由测试命令辅助完成测试过 ...
- Linux Shell脚本编程--Linux特殊符号大全
Linux Shell脚本编程--Linux特殊符号大全 linux_shell 特殊符号的介绍 2011
- Linux Shell脚本编程-基础1
概述: shell脚本在Linux系统管理员的运维工作中非常重要.shell脚本能够帮助我们很方便的管理服务器,因为我们可以指定一个任务计划,定时的去执行某一个脚本以满足我们的需求.本篇将从编程基础 ...
- 【学习】Linux Shell脚本编程
1.脚本的组成和执行 Linux shell脚本的结构并不复杂,其主要由变量.内部命令以及shell的语法结构和一些函数.其他命令行的程序等组成,以下是一个简单的shell脚本. #!/bin/bas ...
- [linux] shell脚本编程-xunsearch安装脚本学习
安装脚本setup.sh #!/bin/sh # FULL fast install/upgrade script # See help message via `--help' # $Id$ # s ...
随机推荐
- IIS 设置文件传输大小限制
IIS默认传输文件大小为30M,最大允许传输为2G. 1.通过webconfig配置节点设置 在IIS 6.0 设置如下配置节点: 但是IIS 7.0-8.0还要做添加如下配置节点才能正确,否则还是默 ...
- 三.mysql表的完整性约束
mysql表的完整性约束 什么是约束 not null 不能为空的 unique 唯一 = 不能重复 primary key 主键 = 不能为空 且 不能重复 foreign key ...
- Android-Java-饿汉式单例模式(内存图)
描述Single对象: package android.java.oop14; public class Single { // 默认构造方法 私有化 不让外界调用 private Single() ...
- bash编程-执行流程
1.顺序执行 shell脚本按从上到下的顺序依次执行,除非使用了选择.循环等执行流程. 2.选择执行 2.1 if # 格式一 if 条件; then # 语句 fi # 格式二 if 条件; the ...
- 2019年微服务5大趋势,你pick哪个?
2018年对于微服务来说是非常重要的一年,这一年Service Mesh开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...
- 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- Objective-C优缺点
优点: 1:Category,使用category可以在不改变原来类的同时为类增加新的方法或者重写原来类的方法实现(使用runtime方法还可以在分类中实现方法交换和添加属性操作) 2:运行时 动态识 ...
- linux下报错bash: service: command not found
在linux下操作的时候经常会遇到,bash: service: command not found这个错误,以前在网上找了,照着弄了,也没细看原因,今天又碰到这个问题,就顺便研究一下. 1.通常这种 ...
- Android 框架式编程 —— 起篇
一般的,在开发的时候,写过的代码在需求变更后,发现需要改动非常多的地方,那么说明之前的代码的架构肯定是存在问题的. 下面我们结合面向对象的六大基本原则谈Android 框架式编程.首先先介绍一下面向对 ...
- 从小白到使用antd+react+react-router+issue+es6搭建博客
概述 本身是前端小白,学过html,css,js的各种书,各种视屏,就是没有接触web开发的内容.偶然看见一个朋友用react搭建了一个博客,于是本着程序员无所不能的精神,也尝试着用react搭建博客 ...