Variables aren't expanded inside a script

January 30, 2018 527 views
Nginx Ubuntu 16.04

I establish LEMP on pure Ubuntu droplets with the following script:

#!/bin/bash
ufw --force enable && ufw allow 22,25,80,443,9000/tcp
apt-get update -y && add-apt-repository ppa:certbot/certbot -y && apt-get update -y
DEBIAN_FRONTEND=noninteractive apt-get upgrade zip unzip tree unattended-upgrades sshguard postfix -y
apt-get upgrade nginx python-certbot-nginx mysql-server php-fpm php-mysql php-mbstring php-mcrypt -y
sed -i "s/post_max_size = .M/post_max_size = 200M/ ; s/upload_max_filesize = .M/upload_max_filesize = 200M/ ; s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/*/fpm/php.ini
/etc/init.d/php*-fpm restart && systemctl restart nginx.service

wget -P ~/myAddons/ https://raw.githubusercontent.com/benqzq/unse/master/{nwsm.sh,dbm.sh,imb.sh,daily.sh,weekly.sh,appendix.sh}
curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > /usr/local/bin/wp
chmod +x ~/myAddons/* /usr/local/bin/wp
cat >> "/etc/bash.bashrc" <<< "source ~/myAddons/appendix.sh"
source ~/myAddons/appendix.sh /etc/bash.bashrc
echo -e "0 0 * * * ~/myAddons/daily.sh \n0 0 * * 0 ~/myAddons/weekly.sh" | crontab

wget -P ${drt}/ https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip
find ${drt}/ -type f -iname "*phpmyadmin*.zip" -exec unzip {} \;
find ${drt}/ -type d -iname 'phpmyadmin-*' -exec mv {} phpmyadmin \;
find ${drt}/ -type f -iname "*phpmyadmin*.zip" -exec rm {} \;
chown -R www-data:www-data ${drt}/
chmod -R a-x,a=rX,u+w ${drt}/

One can directly run the script this way:

source <(curl -s https://raw.githubusercontent.com/benqzq/unse/master/unse.sh | tr -d '\r')

The problem is that the last portion of the script regarding PHPmyadmin install, fails to run correctly --- the variables aren't expanded, hence that part of the script runs in a broken way, and I need to correct the paths after installation, to actually use PMA.

Why are the variables aren't expanded in script execution and what will you say, is the best way to cope with that?

Thanks,

1 comment
  • Please ignore this link:

    source <(curl -s https://raw.githubusercontent.com/benqzq/unse/master/unse.sh | tr -d '\r')
    
1 Answer

I don't see where ${drt} is defined. How is this script run? The environment it is run under may change what variable hold values.

For example, take a script named foo.sh that contains:

#!/bin/bash
echo ${FOO}

If you simply run ./foo.sh it will only print an empty line as ${FOO} has not been defined in the script. You could define it when it is run, like:

  • FOO=bar ./foo.sh

This would print bar. The same would happen if you run:

  • export FOO=bar
  • ./foo.sh

Another possibility is to define its value in a separate file and include it by sourcing that file. Perhaps you are doing that here:

cat >> "/etc/bash.bashrc" <<< "source ~/myAddons/appendix.sh"
source ~/myAddons/appendix.sh /etc/bash.bashrc

If so, my advice would be to use the full path for ~/myAddons/appendix.sh (e.g. /home/username/myAddons/appendix.sh) when possible. The ~ will be expanded differently depending on the user the script is run under. So you will have different results based on how the script is run.

  • Originally I ran the entire script by sourcing it this way: source <(curl -s URL | tr -d '\r').

    Indeed I forgot to define drt as for ${drt} in the example.

    Anyway, in the end I went on a much neater approach, and my script is much more beautiful and wisely built now, as you could see here:

    https://github.com/benqzq/unse/blob/master/install.sh

    Note the sourcing of files there, especially assingments.sh which contains variable expansions, in the right time in the script.

Have another answer? Share your knowledge.