Writing your own shell scripts

Written by Anachron on 2020-01-10

Read the article or go back.

  1. bldr: My Void Linux builder
  2. mksh: Create shell scripts
  3. ted: Edit scripts with your $EDITOR
  4. tedw: Edit scripts from your $PATH
  5. Comments

The $SHELL is a very famous place for linux enthusiasts and programmers.

Many people spend hours every day infront of a text console, inside their favourite $SHELL.

Today I want to talk about why I keep (re)writing tools for my $SHELL and what benefits (and problems) that brings.

# bldr: My Void Linux builder

In Void Linux we use xbps and xbps-src to build packages. But those tools don’t come with all bells and wistles. More stuff can be found in xtools, though this collection is far from complete.

For someone who spends a lot of time trying and debugging this can become quite burdensome:

So what bldr allows me is to execute the following:

bldr -pn fff d (or bldr -pn fff t l c b T p respectively).

This runs the default actions for the fff package which are:

t: Tabs (Convert spaces to tabs)
l: Lint (Check if package template is malformed)
c: Clean (Remove everything from build-dir)
b: Build (Build package)
T: Tree (List all contents of the build inside a tree)
p: Package (Create .xbps file for repo)

Example:

$ ls ~/.bldr/logs/fff-x86_64-*.log
/home/anon/.bldr/logs/fff-x86_64-build.log  /home/anon/.bldr/logs/fff-x86_64-pkg.log
/home/anon/.bldr/logs/fff-x86_64-clean.log  /home/anon/.bldr/logs/fff-x86_64-tree.log
/home/anon/.bldr/logs/fff-x86_64-lint.log

Inside /home/anon/.bldr/logs/fff-x86_64-tree.log:

[12.01.2021-10:28:47] (fff) tree:start
/home/anon/.bldr/master/builddir/fff-2.2
├── LICENSE.md
├── Makefile
├── README.md
├── fff
└── fff.1

0 directories, 5 files
[12.01.2021-10:28:47] (fff) tree:finish (RC=0)

# mksh: Create shell scripts

Pretty simple script to create more shell-scripts when desired

#!/bin/sh

test -n "${1}" || exit 1
! test -f "${1}" || exit 2

cat > "${1}" << EOF
#!/bin/sh

EOF
test "$?" -eq 0 || exit 1
chmod +x "${1}"
"${EDITOR}" "${1}" || { rm "${1}"; exit 1; }

# ted: Edit scripts with your $EDITOR

This is just a wrapper around my $EDITOR ENV for convinience.

This maybe could be an alias but I tinker around a lot so it’s easier and faster for me to keep it as a shell script.

#!/bin/sh

exec ${EDITOR} "${1}"

# tedw: Edit scripts from your $PATH

Most times when I need to edit scripts they are already in my $PATH. That`s where this little helper comes into play.

#!/bin/sh

w=$(which "${1}") || exit 1
exec ted "${w}"

I can easily edit my bldr script from anywhere (given that its inside $PATH) using tedw bldr.


Comments are disabled for now