前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bash shell 中,select 使用举例

Bash shell 中,select 使用举例

作者头像
耕耘实录
发布2019-07-04 15:32:24
7800
发布2019-07-04 15:32:24
举报
文章被收录于专栏:耕耘实录耕耘实录

文章目录

  • Bash shell 中,select 使用举例
    • 一 背景
    • 二 使用举例
      • 2.1 单独使用 select
    • 2.2 结合 case 使用
    • 三 总结

Bash shell 中,select 使用举例

一 背景

在最近的运维工作中,写了很多脚本,在写这些脚本时发现了一些高效的用法,现将 select 的用法简单介绍一下。

二 使用举例

select 表达式是 bash 的一种扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择。格式如下:

select var in ... ; do
    ...
done

2.1 单独使用 select

#!/bin/bash
Hostname=( 'host1' 'host2' 'host3' )
select host in ${Hostname[@]}; do
    if [[ "${Hostname[@]/${host}/}" != "${Hostname[@]}" ]] ; then
        echo "You select host: ${host}";
    else
        echo "The host is not exist! ";
        break;
    fi
done

运行结果展示:

[root@gysl ~]# sh select.sh
1) host1
2) host2
3) host3
#? 1
You select host: host1
#? 2
You select host: host2
#? 3
You select host: host3
#? 2
You select host: host2
#? 3
You select host: host3
#? 1
You select host: host1
#? 6
The host is not exist!

脚本中增加了一个判断,如果选择的主机不在指定范围,那么结束本次执行。

2.2 结合 case 使用

#!/bin/bash
Hostname=( 'host1' 'host2' 'host3' )
PS3="Please input the number of host: "
select host in ${Hostname[@]}; do
    case ${host} in
    'host1')
        echo "This host is: ${host}. "
    ;;
    'host2')
        echo "This host is: ${host}. "
    ;;
    'host3')
        echo "This host is: ${host}. "
    ;;
    *)
        echo "The host is not exist! "
        break;
   esac
done

运行结果展示:

[root@gysl ~]# sh select.sh
1) host1
2) host2
3) host3
Please input the number of host: 1
This host is: host1.
Please input the number of host: 3
This host is: host3.
Please input the number of host: 4
The host is not exist!

在很多场景中,结合 case 语句使用显得更加方便。上面的脚本中,重新定义了 PS3 的值,默认情况下 PS3 的值是:"#?"。

三 总结

3.1 select 看起来似乎不起眼,但是在交互式场景中却非常有用,各种用法希望大家多多总结。

3.2 文章中还涉及到了 bash shell 中判断值是否在数组中的用法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年04月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • Bash shell 中,select 使用举例
    • 一 背景
      • 二 使用举例
        • 2.1 单独使用 select
      • 2.2 结合 case 使用
        • 三 总结
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档