分类: 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