0

When I put an option in the middle, the case doesn't work. After, the script continues to work.

while getopts "t:target:" opt; do
  case opt in
    t)
      test = "$OPTARG"
      ;;
    target)
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

echo $TEST

if [[ "$2" != "-t" && "$2" != "-target" ]]; then
  echo "The -t or -target option must be the second argument."
  exit 1
fi

When i am doing :

./myScript file_path -t

I am getting :


realpath: '': No such file or directory
Directory not found at the second given path :

But normally it would work :) - case option

option requires an argument -- o

Please, help)

max12345
  • 9
  • 1
  • 4
    getopts doesn't support long options like `target` – oguz ismail Jan 15 '23 at 06:37
  • Also, `getopts` only recognizes options passed *before* positional arguments (i.e. `./myScript -t file_path`). And there are several syntax errors in the script; I recommend running it through [shellcheck.net](https://www.shellcheck.net) and fixing what it points out. – Gordon Davisson Jan 15 '23 at 09:50
  • Gordon, thank you. Added getopts before positional arguments. – max12345 Jan 15 '23 at 14:40
  • @GordonDavisson Then I had luck until now ;) I removed my comment. – Wiimm Jan 16 '23 at 07:29

0 Answers0