本文共 1263 字,大约阅读时间需要 4 分钟。
例 1.8. Using select to make simple menus
#!/bin/bashOPTIONS="Hello Quit"select opt in $OPTIONS; do if [ "$opt" = "Quit" ]; then echo done exit elif [ "$opt" = "Hello" ]; then echo Hello World else clear echo bad option fidone
例 1.9. Using the command line
#!/bin/bashif [ -z "$1" ]; then echo usage: $0 directory exitfiSRCD=$1TGTD="/var/backups/"OF=home-$(date +%Y%m%d).tgztar -cZf $TGTD$OF $SRCD
例 1.10. Reading user input with read
In many ocations you may want to prompt the user for some input, and there are several ways to achive this. This is one of those ways:
#!/bin/bashecho Please, enter your nameread NAMEecho "Hi $NAME!"
As a variant, you can get multiple values with read, this example may clarify this.
#!/bin/bashecho Please, enter your firstname and lastnameread FN LNecho "Hi! $LN, $FN !"
例 1.11. read
限时30秒内,输入你的名字
$ read -p "Please input your name: " -t 30 namedPlease input your name: neo$ echo $named
READ_TIMEOUT=60read -t "$READ_TIMEOUT" input# if you do not want quotes, then escape itinput=$(sed "s/[;\`\"\$\' ]//g" <<< $input)# For reading number, then you can escape other charactersinput=$(sed 's/[^0-9]*//g' <<< $input)
原文出处:Netkiller 系列 手札
本文作者:陈景峯 转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。