Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116935
  • 博文数量: 73
  • 博客积分: 66
  • 博客等级: 民兵
  • 技术积分: 497
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-22 14:59
文章分类

全部博文(73)

文章存档

2015年(65)

2013年(5)

2012年(3)

我的朋友

分类: LINUX

2015-02-10 14:30:43



#!/bin/bash echo -n "Type a digit or a letter > " read character case $character in # Check for letters
    [[:lower:]] | [[:upper:]] ) echo "You typed the letter $character"
                                ;;

                                # Check for digits
    [0-9] ) echo "You typed the digit $character"
                                ;;

                                # Check for anything else
    * ) echo "You did not type a letter or a digit" esac  

Notice the special pattern "*". This pattern will match anything, so it is used to catch cases that did not match previous patterns. Inclusion of this pattern at the end is wise, as it can be used to detect invalid input.

simple example of a program that counts from zero to nine:

#!/bin/bash

number=0 while [ "$number" -lt 10 ]; do echo "Number = $number"
    number=$((number + 1)) done  

On line 3, we create a variable called number and initialize its value to 0. Next, we start the while loop. As you can see, we have specified a command that tests the value of number. In our example, we test to see if number has a value less than 10.

The until command works exactly the same way, except the block of code is repeated as long as the specified command's exit status is false. In the example below, notice how the expression given to the test command has been changed from the while example to achieve the same result:

#!/bin/bash

number=0 until [ "$number" -ge 10 ]; do echo "Number = $number"
    number=$((number + 1)) done  


阅读(458) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~