0

So I recently tried getting a string as an input in a bash script which looked a little something like this:

#!/bin/bash
while getopts s: flag
do
    case "${flag}" in
        s|--sample) sample=${OPTARG}
        echo $sample
        ;;
    esac
done

The credit for most of this code goes to baeldung at 2.2.

If I try to run this code, it works, but only if I use -s. If I use --sample, it just echo's ample. Is there an easy way around this

gurkensaas
  • 793
  • 1
  • 5
  • 29
  • @kara I have now gotten it to work, however, it still outputs `/path/to/my/files: illegal option -- -`. Is there a way to fix this? – gurkensaas Sep 09 '21 at 20:26
  • Your question seems to be already answered in-depth in the question marked as duplicate (so I'm voting to this one as well) → TL;DR: `getopts` only supports *short options*, not *--long-options*. But many alternatives are available, and FWIW [this particular answer](https://stackoverflow.com/a/38297066/9164010) based on [argbash](https://argbash.io/) looks like the best solution to me for this use case. – ErikMD Sep 09 '21 at 20:26

0 Answers0