我正在使用bash脚本但是我需要脚本来询问值,直到该值变为空并将值放入变量中。让我们举一个例子:
Please enter value: value1
VAR="value1"
Please enter value: value2
VAR="value1 value2"
Please enter value: value3
VAR="value1 value2 value3"
Please enter value: // script should stop asking for the same value
// and continue the execution of code below this part
VAR="value1 value2 value3"
我怎么能在Bash中做到这一点?
答案 0 :(得分:4)
您可以使用数组:
var=()
while true; do
read -p "Please enter value: " val
[[ -z $val ]] && break
var+=("$val")
done
for elem in "${var[@]}"; do
do_something_with "$elem"
done
答案 1 :(得分:3)
while read -p "Please enter value: " value
do
[ -z "$value" ] && break
VAR="$VAR $value"
done