bash,使用此处文档,如何读取多行用户输入,在空行处停止

bash, using here document, how can I read multiple lines of user input, stop on blank line

有没有办法让脚本中的 heredoc 像在提示符下一样交互?

这是通过 ssh 从 linux 到运行在 android 上的 ssh 服务器,通过 mosh 连接。

我正在制作一组一些小脚本,以允许我在我的 android 上使用应用程序 termux 下的 bash 通过 ssh 合理地发送短信。

在提示符下测试发送命令时一切正常:

termux-sms-send -n "$(tail -n1 number | tr -d ' ')" << ''

但是,当在脚本中时,这不再有效。这是结果:

./main.sh: line 34: warning: here-document at line 33 delimited by end-of-file (wanted `')
./main.sh: line 35: syntax error: unexpected end of file

我当然可以用另一种方式来做,但是使用 heredoc 方法非常简洁,这是我以前在 bash 中没有真正使用过的东西,我不确定如何获取读取命令以这种优雅的方式很好地处理多行输入。

__

编辑添加:

万一有人感兴趣并且上下文是脚本:

searchTxt=""
contacts="$(termux-contact-list | jq -r '.[].name')"

clear 

while :; do
   echo -ne "\nEnter searchterm: $searchTxt"
   read -rsn1 ret; clear

   if [ ${#ret} -eq 0 ]; then
      if [ $(wc -l <<< "$choice") -gt 1 ]; then
         echo -en "type enough characters to narrow down selecton until only 1 remains\n\n"
      else
         echo "choice = $choice"
         number="$(termux-contact-list | jq -r ".[] | select(.name==\"$choice\") | .number")"
         echo "using number: $number"
         echo "$choice" > number
         echo "$number" >> number
         break
      fi
   fi

   searchTxt+=$ret
   choice=$(grep -i "$searchTxt" <<< "$contacts")
   echo "$choice"
done

while :; do
   clear 
   echo "Type message to send, enter a blank line to send message"
   echo -n "message: "

   termux-sms-send -n "$(tail -n1 number | tr -d ' ')" << ''
done

建议的惯用语是在代码中寻找一个空行(因为代码来自正在读取 heredoc 的地方),而不是一个空行在标准输入.

这在交互式提示下有效,您的代码来自标准输入——但它在脚本中不起作用的原因应该是显而易见的。

以下循环是关于在输入流中查找的明确说明:

while IFS= read -r line; do
  [[ $line ]] || break
  printf '%s\n' "$line"
done | termux-sms-send -n "$(tail -n1 number | tr -d ' ')"