-1

How to run below shell scripts which name is init.sh ? I am running this sheel script like this ./init.sh test123 and its giving me the message MONGODB_PASSWORD not defined. Being new to the shell-script, I am not aware what does this mean ?

-z   ==>
Checks if the given string operand size is zero; if it is zero length, then it returns true.    

Actual File details:

#!/bin/bash
if test -z "$MONGODB_PASSWORD"; then
    echo "MONGODB_PASSWORD not defined"
    exit 1
fi

auth="-u user -p $MONGODB_PASSWORD"

# MONGODB USER CREATION
(
echo "setup mongodb auth"
create_user="if (!db.getUser('user')) { db.createUser({ user: 'user', pwd: '$MONGODB_PASSWORD', roles: [ {role:'readWrite', db:'piggymetrics'} ]}) }"
until mongo piggymetrics --eval "$create_user" || mongo piggymetrics $auth --eval "$create_user"; do sleep 5; done
killall mongod
sleep 1
killall -9 mongod
) &

# INIT DUMP EXECUTION
(
if test -n "$INIT_DUMP"; then
    echo "execute dump file"
    until mongo piggymetrics $auth $INIT_DUMP; do sleep 5; done
fi
) &

echo "start mongodb without auth"
chown -R mongodb /data/db
gosu mongodb mongod "$@"

echo "restarting with auth on"
sleep 5
exec gosu mongodb /usr/local/bin/docker-entrypoint.sh --auth "$@"

EDIT-1: cat .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export JAVA_HOME=/opt/jdk1.8.0_181
export M2_HOME=/usr/local/apache-maven-3.5.4
export MAVEN_OPTS=-Xms256m -Xmx512m
export M2=$M2_HOME/bin


# THIS IS SETTING TO RUN THE PiggyMetrix
export CONFIG_SERVICE_PASSWORD=test123
export NOTIFICATION_SERVICE_PASSWORD=test123
export STATISTICS_SERVICE_PASSWORD=test123
export ACCOUNT_SERVICE_PASSWORD=test123
export MONGODB_PASSWORD=test123

export PATH=$M2:$PATH:/opt/jdk1.8.0_181/bin/:$HOME/.local/bin:$HOME/bin

I am using

uname -a
Linux ech-10-XX-XX-11 4.1.12-61.1.18.el7uek.x86_64 #2 SMP Fri Nov 4 15:48:30 PDT 2016 x86_64 x86_64 x86_64 GNU/Linux

EDIT-2:

[dc-user@ech-10-XX-XX-11 ~]$ whoami
dc-user
[dc-user@ech-10-XX-XX-11 ~]$ ls -ltra
total 8664
-rw-r--r--  1 dc-user dc-user     231 Oct  7  2016 .bashrc
-rw-r--r--  1 dc-user dc-user      18 Oct  7  2016 .bash_logout
drwxrwxr-x  3 dc-user dc-user      23 Sep  5 13:43 .m2
-rw-r--r--  1 dc-user dc-user     677 Sep  5 13:56 .bash_profile
-rw-------  1 dc-user dc-user    3478 Sep  5 13:56 .viminfo
drwx------  8 dc-user dc-user    4096 Sep  5 13:56 .
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186
  • You can't safely store lists of arguments in string-type variables (which the code does in several places). See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) -- right now a password with spaces in it could add any number of extra arguments it wanted, for example. – Charles Duffy Sep 05 '18 at 19:42
  • Anyhow, if you want to set your password from `$1` (which you shouldn't -- command-line arguments can be read from anyone on the system), you'd run `MONGODB_PASSWORD=$1` – Charles Duffy Sep 05 '18 at 19:44
  • ...this isn't a "-z parameter" -- `test -z` is just how it's tested whether the value is empty. – Charles Duffy Sep 05 '18 at 19:46
  • Arguably, this is a duplicate of [How do I parse command-line arguments in bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash), or simpler, [How to set a variable from an argument with a default value](https://stackoverflow.com/questions/16074786/bash-how-to-set-a-variable-from-argument-and-with-a-default-value) -- if you'd set your shell variable from the command-line argument, this would be moot. – Charles Duffy Sep 05 '18 at 19:54

1 Answers1

2

With

test -z "$MONGODB_PASSWORD"

you are testing if the variable $MONGODB_PASSWORD is not empty.

Before running the script you will have to store your password in the variable:

MONGODB_PASSWORD="password" ./init.sh
Matteo
  • 14,696
  • 9
  • 68
  • 106
  • See http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html Are you sure you are using a login shell? Can you try to edit .bashrc? – Matteo Sep 05 '18 at 19:36
  • And adding the export to .bashrc? – Matteo Sep 05 '18 at 19:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179493/discussion-between-sayali-and-matteo). – Jeff Cook Sep 05 '18 at 19:42
  • Sorry for the typo, added all exports to the .bashrc. Do I neecd to set PATH variable for all of these ? – Jeff Cook Sep 05 '18 at 19:44
  • 1
    Much better to run `MONGODB_PASSWORD=password ./init.sh` (all on one line); that way it's only exported for the one command and doesn't stay in the shell's environment (and get inherited by every other program you start later in that same shell). – Charles Duffy Sep 05 '18 at 19:44
  • If you don't export it, it will not be set in a subprocess – Matteo Sep 05 '18 at 19:47
  • 1
    @Matteo, `var=value someprocess` exports the `var` only for the duration of `someprocess`, and doesn't set it at all (even as a regular shell variable) after. You can test this yourself: `foo=bar env | grep foo` shows `foo` as being set during the invocation of `env`, but doesn't leave `foo` still in the environment later. – Charles Duffy Sep 05 '18 at 19:49
  • @ Charles Duffy - How to set all those exports permanently like its works fine for java --version? – Jeff Cook Sep 05 '18 at 19:55
  • Configuring environment variables permanently isn't on-topic here -- the details depend on context (it's different setting it for a service vs an interactive shell vs whatever else -- and for a service, it's different based on whether you're using systemd or launchd or runit or upstart or supervisord). Questions about how to configure your UNIX system, as opposed to questions about how to write software, are a better fit for [unix.se]. – Charles Duffy Sep 05 '18 at 19:57
  • But if you look at shellscrit in the post, it accepts many parameters, how to cover all those ? – Jeff Cook Sep 05 '18 at 20:00
  • As I said, how/where to configure environment variables depends on how the script is being run -- which OS you're running it on, (for a service) how you're supervising that service, etc. `.bash_profile` is only for interactive (login) shells, not services. It's not a topical question here on SO -- though there are some old instances from before that was as strictly enforced a rule in the knowledgebase, for example at https://stackoverflow.com/questions/3864877/how-can-i-set-environment-variables-in-my-linux-service-for-asterisk-even-though – Charles Duffy Sep 05 '18 at 20:05