交互式脚本编程:键盘输入与循环控制
1. 键盘输入的读取
在计算机编程中,许多程序都需要与用户进行交互,接收用户的输入。在之前编写的脚本中,往往缺乏这种交互性。例如下面这个评估整数的脚本:
#!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if [[ "$INT" =~ ^-?[0-9]+$ ]]; then if [ "$INT" -eq 0 ]; then echo "INT is zero." else if [ "$INT" -lt 0 ]; then echo "INT is negative." else echo "INT is positive." fi if [ $((INT % 2)) -eq 0 ]; then echo "INT is even." else echo "INT is odd." fi fi else echo "INT is not an integer." >&2 exit 1 fi每次想要改变INT的值,都需要