DEV Community

mtwtkman
mtwtkman

Posted on

Shell script is such a powerful

Anyone knows, yes, this is known all over the world. But let me say, shell script is such a powerful.

I have tried some ways of initial setup my linux environment like dotfiles repository, nix, chef.

All of them are not so bad, but not perfect to me.

For example,

  • dotfiles repository is separated from system package installation.
    • I know that all I need is just preparing single install script.
  • nix is easy but complex and too large to me, sometimes building package time is so long.
  • chef is overspec for single host machine.

My requirements for setup are below.

  • 100% for me
    • I don't need a general system.
  • Basically oneshot
    • But I can setup as many times.
  • Simple
    • All I needed is just 2 layers of install packages and user configuration for packages.
  • On Linux
    • Use shell script, yeah.
  • Always latest
    • Arch like rolling release provides me this spec.

So, now I have had a non-difficut (for me) shell script of bash.

Detail

I need 2 layers of install and configuration.

Install

I defined install as 2 type of standard install and custom install.

Standard install

Standard install is just package manager of OS built-in.
In Arch, via pacman. In Debian, via apt.

Custom install

On the other hand custom install is user-defined installation like using git, curl or make.

Configuration

I defined configuration is just configuration. No trick.

For example create a .bashrc symlink to ${HOME}, create a neovim config directory symlink to ${XDG_CONFIG_HOME}/nvim.

Picture

Image description

Constructure

So simple.
I put shell scripts for install and configuration and packages directory to detect my own necessary.

setup.sh
install_batch.sh
install_single.sh
configure_batch.sh
configure_single.sh
install_custom_batch.sh
install_custom_single.sh
configure_custom_batch.sh
configure_custom_single.sh
packages/
Enter fullscreen mode Exit fullscreen mode

packages directories has subdirectory for each pacakges.

packages/
  bash/
    .bashrc
    .bash_profile
    configure.sh
    install.sh
  tmux/
    tmux.conf
    install.sh
    configure.sh
...    
Enter fullscreen mode Exit fullscreen mode

And each implementations are like this. Basicly, I don't create functions intentinally for maintenance.

I decided that each shellscript files should be seemed as isolated module or namespace.

# setup.sh
sh "./install_batch.sh"
sh "./configure_batch.sh"
sh "./install_custom_batch.sh"
sh "./configure_batch.sh"
Enter fullscreen mode Exit fullscreen mode
# install_batch.sh

pushd packages
for package in *
do
  sh "${package}/install.sh"
done
popd
Enter fullscreen mode Exit fullscreen mode
# <pacakge name>/install.sh

sh "$(sh install_command.sh) ${1}"
Enter fullscreen mode Exit fullscreen mode

About configure is as same as install.

This design allows me do install or configuration isolated for single package like sh ./install_single.sh podman. 100% for me.

Conclusion

Shell script is powerful to make modular system.

Though I don't write any tests, maybe I can write test easily.

Top comments (2)

Collapse
 
josephj11 profile image
Joe

Not bad. Would be clearer if it also had one example that used all the levels with a specific package/application.

Nits:
Constructure hasn't been used much since around 1920. Construction is current. First time I've seen it.

Transposition error in comment: pacakge name

Just curious: almost everything has bash installed these days. Why use sh and .sh? In Ubuntu, you'll get dash by default when you do that. Not sure what other distros do.

Years ago, when Ubuntu switched from sh (as a mode of bash) to dash, a lot of my scripts broke. I changed their shebangs to use bash itself and they all worked again.

Collapse
 
mtwtkman profile image
mtwtkman

Thank you for your comment.

You can refer actual implementation from github.com/mtwtkman/mi.

Of course you can use your preffered shell and I use sh/.sh just because I have no problem with them.
I know that this design is not for everyone or general, it's for me 100%.