![如何获取具有特定模式的变量的值?](https://linux22.com/image/128582/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E5%85%B7%E6%9C%89%E7%89%B9%E5%AE%9A%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%8F%98%E9%87%8F%E7%9A%84%E5%80%BC%EF%BC%9F.png)
read -p "Please enter the number of loops:" num
for((i=1;i<=$num;i++))
do
read -p "Please enter number $i:" number$i
done
我想获取具有特定模式“数字”的变量集的值。
答案1
由于您似乎正在使用bash
:
read -p 'Please enter the number of iterations: ' niter
for (( i = 0; i < niter; ++i )); do
read -p "Please enter number $(( i + 1 )): " num[i]
done
echo 'All numbers:'
printf '\t%s\n' "${num[@]}"
也就是使用数组。
运行它:
$ bash script.sh
Please enter the number of iterations: 3
Please enter number 1: 12
Please enter number 2: 21
Please enter number 3: tom
All numbers:
12
21
tom
您遇到的下一个问题是验证用户是否输入了数字。