#!/bin/bash# posit-param2: script to display all arguments
count=1while [[ $#-gt0 ]]; doecho"Argument $count = $1"
count=$((count + 1))
shift
done
case分支
基本语法
case word in
[pattern [| pattern]...) commands ;;]...
esac
模式匹配 pattern
举例
#!/bin/bashread -p "enter word > "case$REPLYin
a|A) echo"is a or A." ;;
[[:alpha:]]) echo"is a single alphabetic character." ;;
[ABC][0-9]) echo"is A, B, or C followed by a digit." ;;
???) echo"is three characters long." ;;
*.txt) echo"is a word ending in '.txt'" ;;
*) echo"is something else." ;;
esac
匹配多个测试条件
#!/bin/bash
# case4-2: test a character
read -n 1 -p "Type a character > "
# ;;& 用于匹配多个测试条件,匹配到一个后继续匹配下一个而不是直接退出。
case $REPLY in[[:upper:]]) echo "'$REPLY' is upper case." ;;&
[[:lower:]]) echo "'$REPLY' is lower case." ;;&
[[:alpha:]]) echo "'$REPLY' is alphabetic." ;;&
[[:digit:]]) echo "'$REPLY' is a digit." ;;&
[[:graph:]]) echo "'$REPLY' is a visible character." ;;&
[[:punct:]]) echo "'$REPLY' is a punctuation symbol." ;;&
[[:space:]]) echo "'$REPLY' is a whitespace character." ;;&
[[:xdigit:]]) echo "'$REPLY' is a hexadecimal digit." ;;&
esac