0

I am trying to understand a piece of bash script that uses getopts.

I have

#!/bin/bash

function Argparser () {
    while getopts 'o:dfath' arg "$@"; do
        case $arg in
          'o')
             echo "oooooh"
             output_dir=${OPTARG}
             ;;
          'd')
             echo 'ddddddd'
             use_data_calib=true
             ;;
          ?)
             echo "UNKNWON ARGS: ${OPTARG} "
             exit 1
             ;;
        esac
    done
}

#----------------------------------------
#       user parameters
#----------------------------------------
# data directory(required)
data_root_dir=$1
# output directory
output_dir=${data_root_dir}/videos

declare others=${@:2}
Argparser ${others}

#declare use_data_calib=false  #<<-----HERE

echo ${output_dir}
echo ${data_root_dir}
echo ${others}
echo ${use_data_calib}

First I would like to understand what dfath does, and what arguments does this expect by using o:dfath. How should I call this script and with what options?

Another thing that called my attention was the commented line (HERE) . I commented it and now I can set use_data_calib to true or false. However in the original code I am reading the line was not commented. Doesn't that line defeat the purpose of the argument d? Because with that line, use_data_calib is always false...

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Dec 12 '21 at 23:48

1 Answers1

1

o:dfath is just a string that refers to what options the script takes.

The letters are all of the options it can take, as in, -o, -d, etc. Letters to the left of the colon indicate options which expect an argument, like -o myargument. Letters to the right of the colon do not expect an argument.

To your second point, I believe you are correct. The script you are working with is likely incomplete or incorrect.

  • Thanks. If the letters are options, then `-f` must be an option. But then in the case, if I put that, it will print "Unknown args" right? – KansaiRobot Dec 13 '21 at 00:31