2
\$\begingroup\$

I'm currently working on a project and currently have this code. I already improved it by having some pointers, but I want to make it more professional looking and right. What should I implement next?

installation() {

echo '------------------------------------'>&2
echo 'Installing Dependensies'>&2
echo '------------------------------------'>&2
echo "">&2

echo 'Installing pkgconf ..'>&2
apt install pkgconf
echo '------------------------------------'>&2

echo 'Installing libvte-2.91-dev ..'>&2
apt install libvte-2.91-dev
echo '------------------------------------'>&2

echo 'Installing meson ..'>&2
apt install meson
echo '------------------------------------'>&2

echo 'Installing libcairo2-dev ..' 
apt install libcairo2-dev
echo '------------------------------------'>&2

echo 'Installing libpango1.0-dev ..'>&2
apt install libpango1.0-dev       
echo '------------------------------------'>&2

echo 'Installing libgnutls28-dev ..'>&2
apt install libgnutls28-dev  
echo '------------------------------------'>&2

echo 'Installing libgtk-3-dev ..'>&2
apt install build-essential libgtk-3-dev  
echo '------------------------------------'>&2

echo 'Installing libsystemd-dev ..'>&2 
apt install libsystemd-dev
echo '------------------------------------'>&2

echo 'Installing libgirepository1.0-dev ..'>&2 
apt install libgirepository1.0-dev
echo '------------------------------------'>&2

echo 'Installing valac ..'>&2 
apt install valac

echo '------------------------------------'>&2
echo 'Finished Installing the Dependensies'>&2
echo '------------------------------------'>&2

echo 'Cloning https://gitlab.gnome.org/GNOME/vte.git/'>&2
git clone https://gitlab.gnome.org/GNOME/vte.git/

echo 'Entering vte directory'>&2
cd vte
        
echo 'Building VTE'>&2
meson _build                             
ninja -C _build                           
ninja -C _build install

echo 'Done'>&2
}

installation

Any recommendations are welcome. It's pretty simple, all it does is install different dependencies.

\$\endgroup\$
1
  • \$\begingroup\$ Is it really necessary to (e.g.) log "Installing libgtk-3-dev" when the apt install will already contain that information? I ask this because I find trying to read logs full of repeated information quite frustrating. Although I do think the echo '-------' between stages very useful. \$\endgroup\$ Commented Mar 19, 2023 at 11:33

1 Answer 1

1
\$\begingroup\$

Please start with a shebang: #! /usr/bin/env bash


It's unclear why you're logging ordinary messages to stderr, but OK, maybe that's a requirement.

Define a log function for that, and prefer it over echo.


echo 'Installing Dependensies'>&2

The typo is definitely not professional. Prefer the conventional spelling of "Dependencies", both here and for the "finished" message. (Pretty sure there's no "dependensy" British-ism.)


echo 'Installing pkgconf ..'>&2
apt install pkgconf
...
echo 'Installing libvte-2.91-dev ..'>&2
apt install libvte-2.91-dev
...

It just goes on and on in this vein, it makes my eyes water. DRY. Write a loop already:

for PKG in pkgconf libvte-2.91-dev ...

Or put the package names in an env var, one-per-line for convenient git diff'ing, and loop over that.


apt install ...

I wonder if you maybe want apt install -y ..., to prevent user prompting?

It is possible to ask apt to install a bunch of packages all at once in a single command. But that would alter the apt output and your progress reporting, so maybe you prefer not to. Total time spent waiting for downloads will often be reduced if you batch requests together, as some downloads will happen in parallel and the bottleneck router might have lots of bandwidth available.


You didn't show us where you're already cd'd to, so it's unclear where the git clone will write to, but I imagine you've got that worked out already.


ninja -C _build                           
ninja -C _build install

What a curious idiom! First build wasn't enough? Ok, I accept that maybe that's a "feature" of the build system and is really necessary for a correct install.


Here are a few non-default settings you might choose to embrace.

set -e will bail upon error, that is, upon running cd, apt, or any other command that returns non-zero status. It is useful for preventing an errant script from running amok. For example cd /tmp/baz; git clone $URL won't pollute $CWD with some annoying repo if destination dir doesn't exist. A related idiom is cd /tmp/baz && git clone $URL, another way of insisting the dest dir exists.

set -o pipefail is similar to set -e, so e.g. echo | false | false | true will bail instead of reporting $? status of 0. I didn't notice any pipelines in your script, but you might add some over time.

Suppose we never set the env var FOO.

set -u will make e.g. echo ${FOO} a fatal error, instead of interpolating a null-string value. Think of it as "lint for bash".

You don't need any of these. But they can help improve the robustness of what you write.

Often I will set -x to see which stage a script has progressed to, similar to make. But you have explicit echo statements for that, so it isn't needed in this script.

\$\endgroup\$
1
  • \$\begingroup\$ Woah you didn't just gave 101 on Shell Scripting but you made my day too :). Very help full. I'll consider them all. \$\endgroup\$
    – user270282
    Commented Mar 19, 2023 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.