I have a string that might be:
user:pass@host// case #1user@host// case #2host// case #3
(It's a mongoDB connection string, if you're curious)
I need to end up with:
Case #1: user:pass@host
$hostVar->host$userVar->user$passVar->pass
And then run:
mongodump --host $host -u $user -p $pass ...
Case #2: user@host
$hostVar->host$userVar->user$passVar-> (empty)
And then run:
mongodump --host $host -u $user ...
Note: the -p parameter to mongo is not passed.
Case #3: host
$hostVar->host$userVar-> (empty)$hostVar-> (empty)
And then run:
mongodump --host $host ...
Note that -u and -p are not passed.
So...
I am going insane because of the optional nature of the parameters. My first solution was a classic:
user=$(echo $DBHOST | cut -d : -f 1)
pass=$(echo $DBHOST | cut -d : -f 2 | cut -d @ -f 1)
host=$(echo $DBHOST | cut -d : -f 2 | cut -d @ -f 2)
However, if bits are missing, this completely breaks down.
I tried conditional parsing, based on the presence of :, but the result was... well, embarrassing to show here.
The second issue is then: is there a way to "maybe" pass parameters to a command without conditionals? Something like hostParam="--host $host" and then passing $hostParam would work?
ALL of this is because mongodump doesn't support mongo's connection string, and I have a setting variable in the format I showed in the config file...