-2

Good day SO!

I'm working on a Django Project for learning purposes. Now I have a little bash script (working properly) to migrate my Django database and create a superuser with a onetoone profile.

Now I want to update my bash script so it recognizes some parameters. For example to automatically run the model migrations (makemigrations) or run the server after successfully migrating the database.

So I want to tell the bash to make migrations before migrating and run the server when finished (keys without values).

(venv)edwin@edwin:/project/installs/$ source install_database.sh -m -r

Now, I'm quite new to Linux and completely new to Straight Bash Space Separated bash scripts. I've read that using getopt[s] has no use in this case, since it can't handle empty argument strings.

So I attempted to create the Straight Bash Space Separated like:

#!/usr/bin/env bash

while [[ %# > 1 ]]
do
    key=$1

    case $key in
        -m|--makemigrations)
        MAKEMIGRATIONS=true
        shift
        ;;
        -r|--runserver)
        RUNSERVER=true
        shift
        ;;
        *)
        echo "invalid argument"
        ;;
    esac
shift
done

echo "${RUNSERVER}"
echo "${MAKEMIGRATIONS}"

Whatever I attempt to initiate

(venv)edwin@edwin:/project/installs/$ source install_database.sh -m -r
(venv)edwin@edwin:/project/installs/$ source install_database.sh -m "test" -r "test"
(venv)edwin@edwin:/project/installs/$ source install_database.sh -m test -r test

I keep getting the result

false
false

Probably it's a beginners mistake, but I can't seem to figure it out... Can you give me an explanation of what I am doing wrong, and push me in the right direction to solve the problem?

Thanks in advance!

Nrzonline
  • 1,600
  • 2
  • 18
  • 37
  • 1
    There's no mention of "False" in your script. Where does it come from? Also, what's `%#`? – choroba Nov 30 '15 at 11:43
  • I've been searching the web to find out how to make a bash accept parameters. Which lead me tohttp://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146 I'm not sure how it gets the false, however I thought it might return false on an empty or unset variable? And about %#, I assumed it was some form of counting the amount of parameters.. Which, based on your reaction, is probably a wrong assumption (Note: False = false (lower case, my bad)) – Nrzonline Nov 30 '15 at 11:50

1 Answers1

3

Three mistakes:

1) %# should be $#

2) You have two shift instructions in case of an accepted key (one inside the case and one at the end of the while loop). Just remove the shifts inside the cases and keep the one at the end of the loop (remember you want to shift even if the key is not recognized).

3) $# gives you the number of arguments excluding the program's name, so the while loop condition should be while [[ $# > 0 ]]

davir
  • 912
  • 5
  • 7
  • Thank you for the explanation! mistake #1... let's forget about that one? good! Mistake #2 and #3 also applied... calling (venv)edwin@edwin:/project/installs/$ source install_database.sh with or without -r -m both result in true true? Any idea why? – Nrzonline Nov 30 '15 at 12:03
  • @Nrzonline that could be a Mistake #4, but I will not add it because I'm not a bash expert either. Anyway: it is a good idea (if not necessary) to initalize both variables to false before the `while` loop. – davir Nov 30 '15 at 12:28
  • As to #2 the inner `shift`s are correct for the option-accepting versions and incorrect for no-option versions. That's a complication that the script needs to handle internally either by testing the next option for a leading `-` or by requiring the flag value to be "cuddled" to the flag (i.e. not `-r test` but `-rtest`). – Etan Reisner Nov 30 '15 at 12:37
  • 4
    OP wants `-gt 0` not `> 0`. Numeric comparison not string comparison. – Etan Reisner Nov 30 '15 at 12:37