我问的是关于"如何在 Linux shell 脚本中提示是/否/取消输入?"中的答案。
代码:
state="CA"
city="Los Angeles"
while true; do
read -e -p "State: " -i $state state
read -e -p "City: " -i $city city
echo "Your state: $state"
echo "Your city: $city"
done
这就是它的运行方式。
$ ./test.sh
State: CA
City: Los
Your state: CA
Your city:
State: DE
City: city
Your state: DE
Your city:
第一个输入:城市未正确显示
第一个输出:我没有输入城市,所以它应该显示"Los",对吧?
第二个输入:状态更改为"DE",工作正常,这意味着外壳支持该
但是城市默认为"城市",我再次没有输入
第二次输出:城市不正确
无论如何,我认为问题出在"洛杉矶"的空间。如何解决?
多谢!
额外信息
这是我正在使用的 bash。
$ bash --version
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
找到答案
应该在 -i 中使用引号,例如
read -e -p "City: " -i "$city" city
read -e -p "City: " -i $city city
$city
有空格,所以 -i 是'Los'
而不是'Angeles'
因此,Angeles
将是read
设置的变量
你对这个问题的看法是正确的;你面前有关于如何修复它的信息......
read -e -p "City: " -i "$city" city