Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45190
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-16 18:43
文章分类

全部博文(14)

文章存档

2016年(6)

2015年(8)

我的朋友

分类: LINUX

2015-12-12 17:26:06


(一)常见使用例子
#!/bin/bash
 PS3='Choose your favorite vegetable: ' # 设置提示符字串.
      #+如果不是PS3则不会显示提示符直接显示 "#?" 
 echo
 select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
 do
  echo
   echo "Your favorite veggie is $vegetable."
   echo "Yuck!"
   echo
   break # 如果这里没有 'break' 会发生什么? 
         #+ 若没有了break会循环显示提示符PS3并选择
  done
  exit 0


执行效果:
simon@kami:~/12.12.2015$ ./10-29.sh 


1) beans    #自动显示 1) 2) 3) 4) 5)
2) carrots
3) potatoes
4) onions
5) rutabagas
Choose your favorite vegetable: 1
 #若没有设置PS3提示符效果如下
 #  #? 
Your favorite veggie is beans.
Yuck!


(二)没有 in list #从命令行参数读取
#!/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
  #         $1    $2     $3      $4       $5       $6
  # 传递给choice_of() 的参数
  
  # 若choice_of 后直接写"$@"则从命令行参数读入
  #+ choice_of "$@"
 
  exit 0
阅读(920) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~