\$\begingroup\$
\$\endgroup\$
3
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
200_success
145k22 gold badges190 silver badges478 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
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 asource
ing line tobashrc
. If you add thesource
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. Thesource
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. Sosource
ing 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.
answered Jan 13, 2018 at 14:50
-
\$\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 eitherreboot
orsource /etc/bash.bashrc
to make the aliases functional). \$\endgroup\$Arcticooling– Arcticooling2018年01月13日 15:07:02 +00:00Commented Jan 13, 2018 at 15:07
lang-bash
/etc/bash.bashrc
. Unless you do something beyond what you've pasted in there's no point insource
ing anything right before you exit. You could do yoursource
in your interactive session to get the aliases sooner. \$\endgroup\$source
action - I totally missed your point. Will you please rephrase it? @chicks \$\endgroup\$