The Bash Shell also offer select Loop, the syntax is:

select varName in list
do
command1
command2
....
......
commandN
done

OR (combine both select and case statement)

select varName in list
do
case $varName in
pattern1)
command1;;
pattern2)
command2;;
pattern1)
command3;;
*)
echo "Error select option 1..3";;
esac
done
  • Select command use PS3 variable to print its prompt.
  • Each word in list is printed on screen preceded by a number.
  • If the line consists of the number corresponding to one of the displayed words (from the list), then varName is set to that word. You can use if..else.fi or case..in..esac to make a decision.
  • If the line is empty, WORDS and the prompt are redisplayed.
  • If EOF (end of file) is read, the command completes.
  • The loop continues until a break (CTRL+C) is encountered.

Example

Create a shell script called select.sh:

#!/bin/bash
# Set PS3 prompt
PS3="Enter the space shuttle to get more information : " # set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
echo "$shuttle selected"
done

Save and close the file. Run it as follows:

chmod +x select.sh
./select.sh

Sample outputs:

/tmp/x.sh
1) columbia 3) challenger 5) atlantis 7) pathfinder
2) endeavour 4) discovery 6) enterprise
Enter the space shuttle name to get more information : 1
columbia selected
Enter the space shuttle name to get more information :

Combining the select and the case statement

Another select loop example and decision making does with case..in..esac statement (selectshuttle.sh):

#!/bin/bash
# The default value for PS3 is set to #?.
# Change it i.e. Set PS3 prompt
PS3="Enter the space shuttle to get quick information : " # set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
case $shuttle in
columbia)
echo "--------------"
echo "Space Shuttle Columbia was the first spaceworthy space shuttle in NASA's orbital fleet."
echo "--------------"
;;
endeavour)
echo "--------------"
echo "Space Shuttle Endeavour is one of three currently operational orbiters in the Space Shuttle."
echo "--------------"
;;
challenger)
echo "--------------"
echo "Space Shuttle Challenger was NASA's second Space Shuttle orbiter to be put into service."
echo "--------------"
;;
discovery)
echo "--------------"
echo "Discovery became the third operational orbiter, and is now the oldest one in service."
echo "--------------"
;;
atlantis)
echo "--------------"
echo "Atlantis was the fourth operational shuttle built."
echo "--------------"
;;
enterprise)
echo "--------------"
echo "Space Shuttle Enterprise was the first Space Shuttle orbiter."
echo "--------------"
;;
pathfinder)
echo "--------------"
echo "Space Shuttle Orbiter Pathfinder is a Space Shuttle simulator made of steel and wood."
echo "--------------"
;;
*)
echo "Error: Please try again (select 1..7)!"
;;
esac
done

Save and close the file. Run it as follows:

chmod +x selectshuttle.sh
./selectshuttle.sh

Sample outputs:

selectshuttle.sh output

Select loop的更多相关文章

  1. Android系统启动过程

    首先Android框架架构图: Linux内核启动之后就到Android Init进程,进而启动Android相关的服务和应用. 启动的过程如下图所示:(图片来自网上,后面有地址)   下面将从And ...

  2. Oracle Cursor

    1.概念 游标:从字面来理解就是游动的光标.用数据库语言来描述,游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了.将游标放置到某行后,即可对该行数据进行操作, ...

  3. MicroStation VBA 可视化界面

    第十章 可视界面 Private Sub UserForm_Initialize() Dim ViewCen As Point3d Dim MyView As View For Each MyView ...

  4. Zygote进程【2】——Zygote的分裂

    在Zygote的诞生一文中init进程是如何一步步创建Zygote进程的,也了解了Zygote的进程的作用.Zygote进程的诞生对于整个Java世界可以说有着"开天辟地"的作用, ...

  5. Zygote进程【1】——Zygote的诞生

    在Android中存在着C和Java两个完全不同的世界,前者直接建立在Linux的基础上,后者直接建立在JVM的基础上.zygote的中文名字为"受精卵",这个名字很好的诠释了zy ...

  6. Python Twisted、Reactor

    catalogue . Twisted理论基础 . 异步编程模式与Reactor . Twisted网络编程 . reactor进程管理编程 . Twisted并发连接 1. Twisted理论基础 ...

  7. Oracle 中循环遍历某张表,并对符合条件的进行Update操作

    BEGIN FOR L_RECORD IN (select RECORD_ID,CURR_PERIOD,PERIOD_START_DATE, (sysdate- PERIOD_START_DATE) ...

  8. ACE - Reactor模式源码剖析及具体实现(大量源码慎入)

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...

  9. shell Builtin variables(shell内建变量)

    内容来自:abs-guide $BASH The path to the Bash binary itself bash$ echo $BASH /bin/bash $BASH_ENV An envi ...

随机推荐

  1. 有关STL 标准模板库

    1.vector  本质:对数组的封装  特点:读取能在常数时间内完成

  2. 写出易调试的SQL—西科软件

    1.前言 上篇 写出易调试的SQL , 带来了一些讨论, 暴露了不能重用执行计划和sql注入问题, 十分感谢园友们的建议 . 经过调整后 ,将原来的SQLHelper 抓SQL 用做调试环境用, 发布 ...

  3. 关于SharpZipLib压缩分散的文件及整理文件夹的方法

    今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为SharpZipLib没有提供压缩进某个指定文件夹的功能,在反复分析了SharpZipLib提供的各个接口方法后,终于找到 ...

  4. TCP数据包的封包和拆包

    //该段博文为引用,非原创. 封包和拆包 作者:fengge8ylf  博客:http://blog.csdn.net/fengge8ylf 对于基于TCP开发的通讯程序,有个很重要的问题需要解决,就 ...

  5. DB2 runstats、reorgchk、reorg 命令

    runstats.reorgchk.reorg 1.runstats runsats可以搜集表的信息,也可以搜集索引信息.作为runstats本身没有优化的功能,但是它更新了统计信息以后,可以让DB2 ...

  6. HTML之电话: 邮箱: 网址

    <p> 电话: <a href="tel:电话">电话</a> </p> <p> 邮箱: <a href=&quo ...

  7. 02 Apache Solr: 概览 Solr在信息系统架构中的位置

    概述:      Apache Solr是一个用JAVA语言构建在Apache Lucene项目上的开源的企业级搜索平台.主要特性包含:全文搜索.命中高亮.片段式搜索.实时索引.动态集群.数据库集成. ...

  8. iOS强制横屏

    由于项目需求,需要整个项目页面都是竖屏,唯独一个折线图页面强制性横屏显示. 网上逛了许多帖子,也看了好多大神的提供的方法,都没能够实现本屌丝想要的效果.没办法自己研究自己搞,借鉴各路大神的思路,最后费 ...

  9. kaggle 竞赛之套路

    图片数据:卷积还是王道,有几个比较通用性的框架被人拿来改来改去 非图片特征数据:用分类: boost系列算法:牛逼的框架实现 xgboost AdaBoost算法针对不同的训练集训练同一个基本分类器( ...

  10. Mapped Statements collection does not contain value fo

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...