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 ...
随机推荐
- Floyd多源最短路
可以对每一个顶点使用Dijkstra算法求多源最短路. 这里我们来介绍另一种解法:Floyd Floyd算法的主要思想是迭代.每次迭代会朝着答案更近一步. 首先定义一个二维数组Dk[i][j](k初始 ...
- docker 容器和镜像理解
1.镜像是Docker容器的基石,容器是镜像的运行实例,有了镜像才能启动容器.容器和镜像是一对一的,一个容器里就运行一个镜像. 1.base镜像----提供了一个基本的操作系统环境,用户可以根据需要安 ...
- 用 Docker 构建、运行、发布来一个 Spring Boot 应用
本文演示了如何用 Docker 构建.运行.发布来一个 Spring Boot 应用. Docker 简介 Docker 是一个 Linux 容器管理工具包,具备“社交”方面,允许用户发布容器的 im ...
- 将Linux(ubuntu)安装到U盘上,实现即插即用
说明: 本教程是说明如何将ubuntu系统安装到U盘上(也就是把U盘当做电脑的硬盘),可以实现U盘插到任何电脑上都能够在实体机上运行ubuntu系统,而且所有的运行配置都能被保存,相当于随身携带的一个 ...
- iOS调用QQ发起临时会话
iOS调用QQ发起临时会话 iOS调用qq前先判断是否安装qq, 之后通过OpenURL打开对用的qq NSURL *url = [NSURL URLWithString:@"mqq://& ...
- 精通Linux
1, linux 启动流程,详细 2,grub , grub2 3, 文件系统,不同文件系统的特性 ext3 , ext 4 ,xfs 4, 不同目录的作用, 分区 5,用户管理 6,文件权限,目录挂 ...
- javascript实现二分法
js 实现数组查找二分法 二分法实现原理:二分查找可以解决已经排好序数组的查找问题:只要数组中包含target(即要查找的值),那么通过不断缩小包含target数组的范围,最终就可以找到它. 其算法流 ...
- Liferay7 BPM门户开发之11: Activiti工作流程开发的一些统一规则和实现原理(完整版)
注意:以下规则是我为了规范流程的处理过程,不是Activiti公司的官方规定. 1.流程启动需要设置启动者,在Demo程序中,“启动者变量”名统一设置为initUserId 启动时要做的: ident ...
- python 开发环境配置
上篇文章配置了虚机基础环境,本篇文章介绍配置python开发环境 配置YUM源 使用国内yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos ...
- iOS事件拦截及应用
1.概述 我们知道事件的分发是由Application到Window再到各级View的,所以显然最安全可靠的拦截地方是Application.这里拦截事件后如果不手动往下分发,则进入hit-test ...