如何在名为init.sh
的shell脚本下运行?我正在运行像这样的./init.sh test123
这样的脚本,它给了我消息MONGODB_PASSWORD not defined
。作为shell脚本的新手,我不知道这是什么意思?
-z ==>
Checks if the given string operand size is zero; if it is zero length, then it returns true.
实际文件详细信息:
#!/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
我正在使用
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 .
答案 0 :(得分:2)
使用
test -z "$MONGODB_PASSWORD"
您正在测试变量$MONGODB_PASSWORD
是否不为空。
在运行脚本之前,您必须将密码存储在变量中:
MONGODB_PASSWORD="password" ./init.sh