小弟寫了一段指令要識別本機是否為c1若是則執行mongodb指令,
echo "$(hostname)"
回復c1
if ["$(hostname)" == "c1"]; then
mongod -f /usr/local/mongodb/config/mongod-configsvr.conf
fi
回復-bash: [c1: command not found
為何兩個""包起來了linux並沒有把他當成String,那要怎將"$(hostname)"轉成String
發現也可以用py執行
import socket,os
hostName = socket.gethostname()
if hostName =="c1" :
os.system("mongod -f /usr/local/mongodb/config/mongod-configsvr.conf")
if [ $HOSTNAME = "c1" ]; then mongod -f /usr/local/mongodb/config/mongod-configsvr.conf; fi