1

I am getting "Illegal option -n" and "[[: not found" when running this sh script.

This script used to work error-free for a long time and then at some point started breaking when I've updated something on my 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" is related to line #1

"[[: not found" part is related to line #3

justshipit
  • 11
  • 3
  • `[[` is a ksh and bash extension. `sh` does not support it. – Charles Duffy Oct 25 '19 at 16:29
  • 1
    I'm guessing that you used to be running an ancient version of Ubuntu where `/bin/sh` was provided by bash, and upgraded to a modern one where it's provided by dash. Any script using bash extensions should always start with `#!/bin/bash`, `#!/usr/bin/env bash`, or similar -- **not** `#!/bin/sh` -- or invoked with `bash foo`, not `sh foo`. – Charles Duffy Oct 25 '19 at 16:31
  • `read -n` is another bash-only feature; if you want it, you need to use **bash**, not `sh`. – Charles Duffy Oct 25 '19 at 16:32

1 Answers1

1

To write code for POSIX sh, you can't use read -p, or read -n, or [[.

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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441