linux-tutorial/codes/shell/输入和输出/获取用户输入.sh
2019-10-29 18:22:19 +08:00

28 lines
738 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# testing the reading command
echo -n "Enter your name:"
read name
echo "Hello $name, welcome to my program"
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old"
#指定多个变量,输入的每个数据值都会分配给表中的下一个变量,如果用完了,就全分配各最后一个变量
read -p "Please enter name:" first last
echo "Checking data for $last. $first..."
#如果不指定变量read命令就会把它收到的任何数据都放到特殊环境变量REPLY中
read -p "Enter a number:"
factorial=1
for (( count = 1; count <= $REPLY; count ++ ))
do
factorial=$[ $factorial * $count ]
done
echo "The factorial of $REPLY is $factorial"