【问题标题】:Why is bash swallowing characters为什么 bash 会吞下字符
【发布时间】:2021-08-03 11:39:40
【问题描述】:

我在 bash 脚本中有以下代码:

ssh_management
if [[ "$PROMPT_SSH" == "true" ]]; then
  read -p "Generate and Install SSH keys? [y/n]" -n 1 -r
  echo ""
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    generate_container_ssh
    install_ssh_keys
    check_ssh_state
  else
    echo "Skipping SSH key install"
  fi
fi
install_docker

但脚本错误:

Skipping container SSH key install
bash: line 102: nstall_docker: command not found

似乎读取命令以某种方式吞噬了install_docker 上的第一个字符

不知道为什么会发生这种情况或如何解决它。

【问题讨论】:

  • It appears that the read command 好的,然后删除read 并重试。 why this is happening第102行是哪一行?文件是否在运行时被修改?文件中没有nstall_docker 吗?文件中是否有不可打印的字符?
  • 当您使用需要 LF 的操作系统时,您的文件是否可能具有 Windows 样式的 CRLF 行尾?
  • @KamilCuk 删除读取行解决了问题(这就是我知道它是读取命令的方式)。第 102 行是“install_docker”函数。
  • @Shawn 我正在 Windows 上开发,但 vscode 说行尾是 LF。我正在 WSL 和 Raspberry Pi 上测试这个脚本。

标签: bash stdin


【解决方案1】:

似乎有什么东西弄乱了我的 STDIN。

read -p "Generate and Install SSH keys? [y/n]" -n 1 -r < /dev/tty

解决问题。

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2010-11-02
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多