我正在尝试编写一个shell脚本来收集用户输入,以便稍后将其附加到文本文件中。 以下是我正在使用的内容:
note_file=~/project_2/file
loop_test=y
while test "$loop_test" = "y"
do
clear
tput cup 1 4; echo " Note Sheet Addition "
tput cup 2 4; echo "============================="
tput cup 4 4; echo "Note: "
tput cup 5 4; echo "Add Another? (Y)es or (Q)uit: "
tput cup 4 10; read note
if test $note = "q"
then
clear; exit
fi
if test "$note" != ""
then
echo "$note:" >> $note_file
fi
tput cup 5 33; read hoop_test
if [ "$hoop_test" = "q" ]
then
clear; exit
fi
done
现在我的问题是我想在note变量中存储整个句子,而不仅仅是一个争论。 即note ="这是一个注释或其他一般文本内容"
答案 0 :(得分:0)
除了显而易见的hoop
loop
拼写问题,以及:
附加到每个note
(可能是故意的,dunno)之外,您的整个问题都会导致输入多个单词时出现的错误是您在使用test
时未能 引用 您的变量,例如
if test "${note,,}" = "q" ## always quote variables in test
注意"..."
(没有引号,它是test some words = "q"
,它会提示错误:test: too many arguments
- 听起来很熟悉吗?)正确引用它是test "some words" = "q"
这很好。
(,,
的参数展开只是将note
的内容转换为小写以处理Q
或q
以便退出)
除了这些问题(并且需要在最后明确地将loop_test
重置为y
),您的脚本运行良好:
#!/bin/bash
note_file=~/project_2/file
note=""
loop_test=y
while test "$loop_test" = "y"
do
clear
tput cup 1 4; echo " Note Sheet Addition "
tput cup 2 4; echo "============================="
tput cup 4 4; echo "Note: "
tput cup 5 4; echo "Add Another? (Y)es or (Q)uit: "
tput cup 4 10; read note
if test "${note,,}" = "q" ## always quote variables in test
then
clear; exit
fi
if test "$note" != ""
then
echo "$note" >> "$note_file"
fi
tput cup 5 34; read loop_test
if [ "${loop_test,,}" = "q" ]
then
clear; exit
else
loop_test=y
fi
done