\$\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
146k22 gold badges191 silver badges481 bronze badges
asked Jan 12, 2018 at 17:55
Arcticooling
2401 silver badge10 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
touchfile if the aliases are added and use that as a flag. I would create the aliases in file and add asourceing line tobashrc. If you add thesourceline before creating the aliases file the aliases file could serve as your flag file for whether to add that line or not. - Running
sourceon anything that doesn't have some side effect at the end of your script is pointless. Thesourceis 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. Sosourceing 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-fpminstalled? 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
gitsomewhere. The lack of a github link makes me wonder about this.
answered Jan 13, 2018 at 14:50
chicks
2,8893 gold badges18 silver badges30 bronze badges
-
\$\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
sourceonly because I start to use the aliases right after I finished executing that script (AFAIK, it's eitherrebootorsource /etc/bash.bashrcto make the aliases functional). \$\endgroup\$Arcticooling– Arcticooling2018年01月13日 15:07:02 +00:00Commented Jan 13, 2018 at 15:07
You must log in to answer this question.
lang-bash
/etc/bash.bashrc. Unless you do something beyond what you've pasted in there's no point insourceing anything right before you exit. You could do yoursourcein your interactive session to get the aliases sooner. \$\endgroup\$sourceaction - I totally missed your point. Will you please rephrase it? @chicks \$\endgroup\$