caseselect结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支.

select

  select结构是建立菜单的另一种工具, 这种结构是从ksh中引入的.

  select variable [in list]

    do 

    TT class="REPLACEABLE" >command... 
  break 
  done

提示用户输入选择的内容(比如放在变量列表中). 注意: select命令使用PS3提示符, 默认为(#?), 当然, 这可以修改.

#!/bin/bash

PS3='Choose your favorite vegetable: ' # 设置提示符字串.

echo

select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break # 如果这里没有 'break' 将不停循环进行选择
done exit

如果忽略了in list列表, 那么select命令将会使用传递到脚本的命令行参数($@), 或者是函数参数(当select是在函数中时).

与忽略in list

  for variable [in list]

结构比较一下.

  例: 使用函数中的select结构来创建菜单

#!/bin/bash

PS3='Choose your favorite vegetable: '

echo

choice_of()
{
select vegetable
# [in list]被忽略, 所以'select'使用传递给函数的参数.
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break
done
} choice_of beans rice carrots radishes tomatoes spinach
# $ $ $ $ $ $
# 传递给choice_of()的参数 exit

case (in) / esac

在shell中的case结构与C/C++中的switch结构是相同的. 它允许通过判断来选择代码块中多条路径中的一条. 它的作用和多个if/then/else语句的作用相同, 是它们的简化结构, 特别适用于创建菜单.

case "$variable" in 
  "$condition1" )
  TT class="REPLACEABLE" >command... 
  ;;

"$condition2" )
  TT class="REPLACEABLE" >command... 
  ;;
esac

例: 简单的字符串匹配

#!/bin/bash
# match-string.sh: 简单的字符串匹配 match_string ()
{
MATCH=
NOMATCH=
PARAMS= # 此函数需要2个参数.
BAD_PARAMS= [ $# -eq $PARAMS ] || return $BAD_PARAMS case "$1" in
"$2") return $MATCH;;
* ) return $NOMATCH;;
esac } a=one
b=two
c=three
d=two match_string $a # 参数个数错误.
echo $? # match_string $a $b # 不匹配
echo $? # match_string $b $d # 匹配
echo $? # exit

shell中select、case的使用的更多相关文章

  1. golang中select case 的用途到底是啥

    https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/select-operation.html ------------------- ...

  2. python如何实现像shell中的case功能

    我们知道在shell脚本里是支持case语句,当位置参数为空时,会提示我们怎么使用脚本 那么在python怎么实现呢?也使用case吗? python里不支持case语句,但是也有实现case的方法. ...

  3. shell中的case表达式

    语法格式 case var in pattern1 | patter2) command1 command2;; pattern3) command1 command2;; *) default co ...

  4. Shell中的case命令

    case语句和判断语句[if...elif...else]功能类似;当在逻辑判断比较简单的情况下,比后者的代码量要少许多.case用法,用变量来匹配某值,如果匹配成功则执行它下面的命令,直到 ::为止 ...

  5. shell中的case语句

    case语法: case $arg in arg1) 语句1 ;; arg2) 语句2 ;; *) help 语句 ;; esac eg: eg:

  6. shell编程学习笔记(九):Shell中的case条件判断

    除了可以使用if条件判断,还可以使用case 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script08.sh 开始编写scrip ...

  7. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  8. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  9. shell中case的用法学习笔记

    这篇文章主要为大家介绍shell中的case语句:可以把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码. 本文转自:http://www.jbxue.com/article ...

随机推荐

  1. tcp注意点

    tcp注意点 tcp服务器一般情况下都需要绑定,否则客户端找不到这个服务器 tcp客户端一般不绑定,因为是主动链接服务器,所以只要确定好服务器的ip.port等信息就好,本地客户端可以随机 tcp服务 ...

  2. [Ramda] Sort, SortBy, SortWith in Ramda

    The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: ta ...

  3. [Angular] @ViewChildren and QueryLists (ngAfterViewInit)

    When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This ...

  4. html5--6-33 CSS定位是什么

    html5--6-33 CSS定位是什么 一.总结 一句话总结: 1.常规文档流是一套体系,浮动是另外一套体系. 2.标签清除浮动之后会跑到常规文档流它本来的地方. 3.浮动是否占据常规文档流:应该不 ...

  5. 一个完整配置例nginx.conf(生产环境中使用)

    一个完整的nginx配置案例,生产环境 一个完整配置例(生产环境中使用) user nobody nobody; worker_processes 4; worker_rlimit_nofile 51 ...

  6. CORDOVA :添加cordova-plugin-file-opener2插件cordova打包报错

    原文:CORDOVA :添加cordova-plugin-file-opener2插件cordova打包报错 最近在接触android项目,其中涉及到APP自动更新的问题,当新APP下载成功后需要打开 ...

  7. win10 uwp 线程池

    原文:win10 uwp 线程池 如果大家有开发 WPF 或以前的程序,大概知道线程池不是 UWP 创造的,实际上在很多技术都用到线程池. 为什么需要线程池,他是什么?如何在 UWP 使用线程池,本文 ...

  8. 把搜狗输入法词库导入Google拼音输入法

    为PC端Google拼音输入法增加词库 为什么折腾词库 都在说百度.讯飞等输入法上传用户词库,为了安全建议大家使用google输入法之类,话说回来,要想使用智能联想功能是不是就得把你输入习惯放在他的里 ...

  9. C#6

    C#6   1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: 1 public string FirstName { get; privat ...

  10. Extension of write anywhere file system layout

    A file system layout apportions an underlying physical volume into one or more virtual volumes (vvol ...