A nice way of parsing options passed to a bash script without relying on position:
#!/bin/bash for i in "$@" do case $i in --node-port=*) NODEPORT="${i#*=}" ;; --jjp-host=*) JJPHOST="${i#*=}" ;; --jjp-port=*) JJPPORT="${i#*=}" ;; --spp-host=*) SPPHOST="${i#*=}" ;; --spp-port=*) SPPPORT="${i#*=}" ;; --help) echo "" echo "Usage example: start_app --spp-host=jupiter --spp-port=2000 --node-port=3000 --jjp-host=neptune --jjp-port=4000" echo "" ;; *) ;; esac done [ "${NODEPORT}XXX" == "XXX" ] && { echo "Warning: NODEPORT env var not set"; } [ "${JJPHOST}XXX" == "XXX" ] && { echo "Warning: JJPHOST env var not set"; } [ "${JJPPORT}XXX" == "XXX" ] && { echo "Warning: JJPPORT env var not set"; } [ "${SPPHOST}XXX" == "XXX" ] && { echo "Warning: SPPHOST env var not set"; } [ "${SPPPORT}XXX" == "XXX" ] && { echo "Warning: SPPPORT env var not set"; } export NODEPORT export JJPHOST export JJPPORT export SPPHOST export SPPPORT cd /applications/my_app nohup npm run start &
No comments:
Post a Comment