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 ...
随机推荐
- Python-字典(dict)
字典是一种key-value的数据类型,字典里必须写Key和value: 字典的优点是取数方便和速度快. 字典的定义使用大括号{ },每个值用“,”隔开,key和value使用“:”分隔. value ...
- 使用pwm进行呼吸灯的设计
本文源码已经上传至csdn: 程序源码如下; #define LEDC_IO_0 (2) esp_err_t app_main() { ledc_timer_config_t ledc_timer = ...
- 基于ESP32的uart通讯
本文源码地址为:http://download.csdn.net/download/noticeable/9961054 ESP32上有三个UART通讯接口,设备号,从0~2,即UART0,UART1 ...
- 长方体类Java编程题
1. 编程创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length).宽(width)和高(heigth),再定义一个方法void setBox(int l, int w ...
- Lambda表达式where过滤数据
使用Lambda的表达式来过滤符合条件的数据.下面的代码实现,是把字符阵列中,把名字长度等于3元素找出来. class Bv { public void LambdaExpression() { st ...
- select 的问题
#include <errno.h> #include <string.h> #include <fcntl.h> #include <sys/socket. ...
- Java学习笔记51(综合项目:家庭记账系统)
javaEE的开发模式 1.什么是模式 模式在开发过程中总结出的“套路”,总结出的一套约定俗成的设计模式 2.javaEE经历的模式 model1模式: 技术组成:jsp+javaBean model ...
- [Swift]LaunchScreen.storyboard设置启动页
设置[General]->[App Icons and Launch Images] 注意:当App不是用LaunchScreen.xib来配置启动页时,[LaunchScreen File]置 ...
- java相关技术问答(二)
String为什么是final的 首先是为了安全性,final表示不可变,不可被继承,不能修改其方法保证安全 在多线程环境下,final类型的String保证线程安全 String支持字符串常量池,相 ...
- SqlServer 更改数据库名称
1.首先选中需要更改数据库右击属性找到文件, 此处可直接修改数据库逻辑名称 2.选中数据库右击选择重命名修改数据库名称. 3.将数据库进行分离,找到数据库文件mdf与ldf文件,直接更改文件名称 4. ...