【问题标题】:"Illegal option -n" and "[[: not found" with answer options [duplicate]带有答案选项的“非法选项-n”和“[[:未找到”[重复]
【发布时间】:2019-10-25 16:25:54
【问题描述】:

运行此 sh 脚本时,我收到“Illegal option -n”和“[[: not found”。

该脚本过去可以正常运行很长时间,然后在我在 Ubuntu 18.04.3 VPS 上更新某些内容时开始出现问题。

read -p "Do you wish to enable SSL for this domain? [Y/n]" -n 1 -r 
echo
if [[ $REPLY =~ ^[Nn]$ ]]
then
  # something here
else
  # something else here
fi

“Illegal option -n”与#1行有关

"[[: not found" 部分与第 3 行有关

【问题讨论】:

  • [[ 是 ksh 和 bash 扩展。 sh 不支持。
  • 我猜你曾经运行一个古老的 Ubuntu 版本,/bin/sh 由 bash 提供,然后升级到现代版本,它由 dash 提供。任何使用 bash 扩展的脚本都应始终以 #!/bin/bash#!/usr/bin/env bash 或类似名称开头 -- not #!/bin/sh -- 或以 bash foo 调用,而不是 sh foo
  • read -n 是另一个仅限 bash 的功能;如果需要,您需要使用 bash,而不是 sh

标签: bash sh ubuntu-18.04


【解决方案1】:

要为 POSIX sh 编写代码,您不能使用 read -pread -n[[

printf '%s' "Do you wish to enable SSL for this domain? [Y/n]"
read -r reply
case $reply in
  [Nn]) : "something here" ;;
  *)    : "something else here";;
esac

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-13
    • 2017-05-03
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多