1
\$\begingroup\$

Please review the following Bash script that uses to establish a "lightweight" Ubuntu-Nginx server environment mainly aimed to run small WordPress sites (about 5 plugins, about 25 webpages, about 25 images) on a cloud hosting platform.

The BASHRC heredocument sets aliases for personal scripts I already have and might use me later on.

apt-get update -y && apt-get upgrade -y && add-apt-repository ppa:certbot/certbot -y
ufw enable && ufw allow 22/tcp 80/tcp 443/tcp 9000/tcp && ufw allow 53/upd 
apt-get install zip unzip tree unattended-upgrades sshguard postfix nginx python-certbot-nginx mysql-server php-fpm php-mysql php-mbstring php-mcrypt -y

sed -i "s/# gzip_/gzip_/g" /etc/nginx/nginx.conf
sed -i "s/max_size = .M/max_size = 200M/g" /etc/php/*/fpm/php.ini
sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php/*/fpm/php.ini
/etc/init.d/php*-fpm restart && systemctl restart nginx.service

cat <<-"BASHRC" >> /etc/bash.bashrc
    alias rss="/etc/init.d/php*-fpm restart && systemctl restart nginx.service"
    alias brc="nano /etc/bash.bashrc"
    alias www="cd /var/www/html"
    alias pma="tmux new-session -d 'bash ~/scripts/pma.sh"
    alias imb="bash ~/scripts/imb.sh"
BASHRC
source /etc/bash.bashrc
\$\endgroup\$
  • \$\begingroup\$ Your code is not re-entrant. It will keep adding the same lines to /etc/bash.bashrc. Unless you do something beyond what you've pasted in there's no point in sourceing anything right before you exit. You could do your source in your interactive session to get the aliases sooner. \$\endgroup\$ – chicks Jan 12 '18 at 23:41
  • \$\begingroup\$ Hmm, I see your point about entranting (sorry, not sure it's the correct adverb to describe "if set of aliases exist, don't add it; else, add it"). Regarding the source action - I totally missed your point. Will you please rephrase it? @chicks \$\endgroup\$ – Arcticooling Jan 13 '18 at 5:46
  • \$\begingroup\$ Rephrased into an answer. \$\endgroup\$ – chicks Jan 13 '18 at 14:52
1
\$\begingroup\$

Good

  • Tying together related commands with && is a good error-avoidance strategy.

Trouble

  • The code is not re-entrant because the aliases will keep getting appended every time. This is bad because you can only run or "enter" the code once safely. You could touch file if the aliases are added and use that as a flag. I would create the aliases in file and add a sourceing line to bashrc. If you add the source line before creating the aliases file the aliases file could serve as your flag file for whether to add that line or not.
  • Running source on anything that doesn't have some side effect at the end of your script is pointless. The source is causing the aliases you added to get read into the current shell. But since you're at the end of your script there's no command that will use the aliases so it was a waste of effort to read them in. Maybe you intend to do more with these later in the script, but it seems they are intended for the interactive user. So sourceing the new aliases in an interactive shell would make them available to the interactive user sooner than logging out and logging back in would.
  • Some comments might save you from rethinking what you did someday.

Maybe trouble

  • Did you leave out a single quote in the pma alias?
  • Can you have more than one version of php-fpm installed? If so your * in the command will expand in a weird way. Maybe you can put the version you expect in a variable or use packaging commands to the find the version that is installed.
  • No sanity checking. It might be good to error out if the files you are looking for turn out to not be around.
  • I hope you're storing your code in git somewhere. The lack of a github link makes me wonder about this.
\$\endgroup\$
  • \$\begingroup\$ Thanks! Indeed I don't plan to have more than one version of PHP-FPM (I install it only once, with the latest version so I just wrote version-agnostic commands for whatever version that I might install in the future). Indeed, I had a typo in the pma alias. Indeed, files are on Git. I want to point out that I use source only because I start to use the aliases right after I finished executing that script (AFAIK, it's either reboot or source /etc/bash.bashrc to make the aliases functional). \$\endgroup\$ – Arcticooling Jan 13 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.