Shell script is such a powerful
Tags: shellscript, bash
Published: 2024-06-06 09:14:15

---
title: Shell script is such a powerful
published: true
description: 
tags:
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-06-06 08:17 +0000
---

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1k6qsu4v8yuj7mmavqka.png)



## Constructure

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

```txt
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/
```

`packages` directories has subdirectory for each pacakges.

```txt
packages/
  bash/
    .bashrc
    .bash_profile
    configure.sh
    install.sh
  tmux/
    tmux.conf
    install.sh
    configure.sh
...    
```

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. 

```bash
# setup.sh
sh "./install_batch.sh"
sh "./configure_batch.sh"
sh "./install_custom_batch.sh"
sh "./configure_batch.sh"
```

```bash
# install_batch.sh

pushd packages
for package in *
do
  sh "${package}/install.sh"
done
popd
```

```bash
# <pacakge name>/install.sh

sh "$(sh install_command.sh) ${1}"
```

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.