简体   繁体   中英

Escaping variables in bash file

I'm creating a init.d script within a bash file, which goes as follows:

# AUTOSTART
$APPDIR=somedir
$APPCONF=somedir
$APPVENV=somedir
$APPUSER=someuser
cat <<EOF >/etc/init.d/uwsgi
#!/bin/bash
daemon=$APPVENV/bin/uwsgi
args="--emperor $APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile $APPDIR/emperor.pid --gid `id -g $APPUSER`"
pid=$APPDIR/emperor.pid
case "$1" in
    start)
        echo "Starting uwsgi"
        start-stop-daemon -m -p $pid --start --exec $daemon $args
        ;;
    stop)
        echo "Stopping script uwsgi"
        start-stop-daemon --signal INT -p $pid --stop $daemon $args
        ;;
    reload)
        echo "Reloading conf"
        kill -HUP $(cat $pid)
        ;;
    *)
        echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
        exit 1
    ;;
esac
exit 0
EOF

It is my understanding, from help, that $APPCONF, $APPVENV, $APPUSER and $APPDIR need to be escaped because I define them outside the file. So is it correct that I simply put a back slash in front of the variable like this:

daemon=\$APPVENV/bin/uwsgi
args="--emperor \$APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile \$APPDIR/emperor.pid --gid `id -g \$APPUSER`"
pid=\$APPDIR/emperor.pid

It still doesn't seem to work though, the service doesn't start, so I think I might have done something else wrong. Can anyone confirm I am escaping properly please?

The dollar sign is only used for reading/using the value of a variable, not when setting its value. Thus, setting APPDIR to somedir would look like this:

APPDIR=somedir

Depending on how you call the script, you may also want to export the variable:

export APPDIR=somedir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM