0

I'm writing a bash script where it connects to the mongodb in different ways and I'll run this script on various projects - some of them require --ssl connection and some of them don't. So, I wanted to know a way for me to maybe declare a variable on top which will turn on or off depending on whether the project needs --ssl connection.

ssl="--ssl" #how do I determine whether to turn this variable on or off depending on whether the project needs --ssl?

Example of where its used in bash script

`master_var=`mongo ${ssl_mode} --eval  "db.isMaster.ismaster"`

Another example in the bash script where I connect to mongo:

mongo --quiet ${ssl_mode} ${name_db} <<EOF
#some commands
EOF

Edit: I want all of this to be done on the bash script itself.

Shaun the Sheep
  • 22,353
  • 1
  • 72
  • 100
  • 1
    Does this answer your question? [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – kaylum Dec 29 '20 at 11:23
  • Define a command line option for your own script that turns on ssl. Same idea as the `--ssl` option that is defined by `mongo`. – kaylum Dec 29 '20 at 11:23
  • @kaylum I'm sorry, I'm very new at this. Could you show me what you mean? But then again, how would the script know when to turn that variable on? – confusedcoder Dec 29 '20 at 11:30
  • Did you read the linked post? *how would the script know when to turn that variable on.* Whoever runs your script tells it to. Just like you tell `mongo`. You add code that accepts an optional `--ssl` in your own script which parses that and passes it onto `mongo`. So `./you_script` will run `mongo` without ssl and `./your_script --ssl` will run `mongo` with ssl. That's just an example. You don't even have to use `-ssl` and can use any option you want. – kaylum Dec 29 '20 at 11:33
  • @kaylum ok that's more clear now! so how would I pass that argument in Bash? For instance over here when I'm connecting to mongo, how would I refactor the code here? `master_var=`mongo --eval "db.isMaster.ismaster"` – confusedcoder Dec 29 '20 at 11:39
  • @kaylum I tried reading that link but it went over my head, I'd appreciate the help! extremely new here! Thanks a ton! – confusedcoder Dec 29 '20 at 11:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226555/discussion-between-confusedcoder-and-kaylum). – confusedcoder Dec 29 '20 at 11:41
  • @kaylum also I would like this to be done in the bash script itself so I don't think your linked post helps me at all. – confusedcoder Dec 29 '20 at 12:57
  • So who/what decides to turn ssl on or off? If it is not the user then you could be right. But then you need to tell us what are the conditions that determine that decision? – kaylum Dec 29 '20 at 20:32

1 Answers1

0

You can use environment variables:

if [ -n "$MYSCRIPT_ENABLE_SSL" ]; then
    ssl_mode="--ssl"
fi

And from where you call the script:

MYSCRIPT_ENABLE_SSL=1 ./myscript.sh

or

export MYSCRIPT_ENABLE_SSL=1
./myscript.sh
yemre
  • 708
  • 3
  • 11