0

I am trying to create a simple bash function that uses 2 command line parameters. -v for verbosity and -d for a date.

I want to use verbosity false as default and todays date as default for -d.

Now if I provide actual other values, my defaults dont get overwritten. Here is an example output:

$ cruns -v -d 2019-09-10
2019-09-12
false
if check
test false

The date output should be sept. 10th instead of 12th.

And here is my code:

EDIT: I fixed the variable assignment $, but the output still does not use my provided parameters.

cruns(){
        local VERBOSE=false
        local STARTDATE="$(date +"%Y-%m-%d")"

        while getopts "vd:" option; do

        case $option
        in
        v) VERBOSE=true
           ;;

        d) STARTDATE=$OPTARG
           ;;
        esac
    done


        echo $STARTDATE;
        echo $VERBOSE;

        echo "if check"

        if $VERBOSE; then
        echo "test true";#              cjobs $STARTDATE | awk -F"_AS-|_Und|_all" '{a[$1]++} END{for(x in a){print x}}'
        else
    echo "test false";#             cjobs $STARTDATE | awk -F"__|_AS-|_Und|_all" '{a[$2]++} END{for(x in a){print x}}'
        fi
}

EDIT: I fixed the variable assignment $, but the output still does not use my provided parameters. I store the function in my bashrc, source bashrc and call the function from terminal if this is of importance.

What do I need to change to make the variables get filled with the provided commandline parameters in my getopts case?

voiDnyx
  • 975
  • 1
  • 11
  • 24
  • 1
    bash variable assignments never use '$' on the left hand side of the equals – Calvin Taylor Sep 12 '19 at 12:46
  • Possible duplicate of [Using getopts inside a Bash function](https://stackoverflow.com/questions/16654607/using-getopts-inside-a-bash-function). The critical thing here is that you must make `OPTIND` local, or `getopts` gets very confused. Also, I recommend using lower- or mixed-case variables for your own things (e.g. `verbose` instead of `VERBOSE`), to avoid confusion with the manu all-caps variables with special meanings. – Gordon Davisson Sep 12 '19 at 15:21

1 Answers1

2

Assign to the variable name, not it's value. So it's VERBOSE=true and STARTDATE=$OPTARG.

case $option
in
v) VERBOSE=true
   ;;

d) STARTDATE=$OPTARG
   ;;
esac

Also, as you appear to be using the crun command from shell, you need to source the file containing the cruns definition into your current shell.

source cruns.sh

suspectus
  • 16,548
  • 8
  • 49
  • 57
  • This did not fix it, I still get the same output as before. Is it because of my use of local variables maybe? – voiDnyx Sep 12 '19 at 13:28
  • Are you using those variables outside the function? If so, yes, remove the local declarations. – suspectus Sep 12 '19 at 13:33
  • No I do not use them for anything else. I only declare them in the function, want to re-assign their value if a parameter is given and use them in the same function 2 lines later. – voiDnyx Sep 12 '19 at 13:39
  • See updated post, `source ` - does that help? – suspectus Sep 12 '19 at 13:51
  • I did / do source the file (my .bashrc) after the changes. No change in output though. – voiDnyx Sep 12 '19 at 15:07