您需要使用bash中提供的read内置函数并将多个用户输入存储到变量中,
read -p "Enter the files you would like to install: " arg1 arg2 arg3
用空格分隔输入.例如,在运行上面的时候,
Enter the files you would like to install: spreadsheet json diffTool
现在,上述每个输入都在变量arg1,arg2和arg3中可用
上面的部分回答了你的问题,你可以在一个空格分隔中输入用户输入,但如果你有兴趣在一个循环中读取多个,有多个提示,这里是你在bash shell中的方法.以下逻辑获取用户输入,直到按下Enter键,
#!/bin/bash
input="junk"
inputArray=()
while [ "$input" != "" ]
do
read -p "Enter the files you would like to install: " input
inputArray+=("$input")
done
现在,您的所有用户输入都存储在数组inputArray中,您可以循环读取值.要一次性打印它们,请执行
printf "%s\n" "${inputArray[@]}"
或者更合适的循环
for arg in "${inputArray[@]}"; do
[ ! -z "$arg" ] && printf "%s\n" "$arg"
done
并将单个元素作为“${inputArray [0]}”,“${inputArray [1]}”等访问.