Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

20.13. User interfaces

例 20.8. Using select to make simple menus

			
#!/bin/bash
OPTIONS="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
	fi
done
			
			

例 20.9. Using the command line

			
#!/bin/bash
if [ -z "$1" ]; then
	echo usage: $0 directory
	exit
fi
SRCD=$1
TGTD="/var/backups/"
OF=home-$(date +%Y%m%d).tgz
tar -cZf $TGTD$OF $SRCD			
			

			

例 20.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/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"			
			
			

As a variant, you can get multiple values with read, this example may clarify this.

			
#!/bin/bash
echo Please, enter your firstname and lastname
read FN LN
echo "Hi! $LN, $FN !"			
			
			

20.13.1. input

例 20.11. read

限时30秒内,输入你的名字

$ read -p "Please input your name: " -t 30 named
Please input your name: neo

$ echo $named
			

		
READ_TIMEOUT=60
read -t "$READ_TIMEOUT" input

# if you do not want quotes, then escape it
input=$(sed "s/[;\`\"\$\' ]//g" <<< $input)

# For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*//g' <<< $input)