Shell Scripting
October 30, 2019

Shell scripts | An overview

Scripting automates processes that are tedious to repeat. Well known scripting languages like Python or Lua are good to know and perfect to use, however in *nix systems there is shell scripting too.

Most of the things that can be done on the command line can be done via scripting. Scripts are to make our life easier, from commands' automation to network pen-testing.

Why Shell scripting?

It's not hacking, it's just finding things you don't know.

Shell scripting is native and doesn't require to install anything since the interpreter is the shell and comes preinstalled by default.

  • Shell scripting helps automating processes that we repeat at the command-line, like driving data through pipes and storing logs.
  • It allows us to create power tools and utilities and simple applications, even graphical ones.
  • Creating scripts gives us the advantage of testing commands, so we can ensure a low error rate when executing them in system tasks.

How it works

As a first look on how Shell scripting works, Let's explore the creation of a simple script.

— Scripts can be stored anywhere in the computer, but let's keep things tidy and make a room for them.

  • The fastest way to create a directory is by typing in the terminal emulator the mkdir command. Navigate to your $HOME directory and type the following:
$ mkdir .scripts

You should have a new hidden folder named .scripts.

  • The fastest way to create a file is by using the touch command:
$ touch .scripts/demo.sh

The .sh extension marks the file as a shell script although it isn't necessary at all.

  • Shell scripts need to be allowed to execute. By default every file created (unless specified) has only read/write permissions so in order to allow execution we need to call chmod.
$ doas chmod +x .scripts/demo.sh

  • Finally we have to indicate that this is a script to be executed by our interpreter of choice. Our first line of every shell script should be:
#!/bin/sh

#! tells the OS we're providing the name and the path of the scripting language. In this case is sh but the same would be for other scripting languages like perl. After that you can type any command you want to be executed by the script.

Open the file with your favorite text editor and make some cool hacky coding (:

This is a basic "hello world" example. Try running it inside your computer.
#!/bin/sh
printf "%s\n" "You chose the red pill but you're not Neo, who are you?"
read USERNAME
printf "%s\n" "Welcome to the Matrix, $USERNAME"

What to expect

Through this guide's episodes we're going to take a look at what the command line tools offer us to work and how shell scripting works inside a *nix system. Whether you're new to shell scripting or an advanced user I hope you can find the material useful as a guide to learn or as a reminder to certain things.

Here's an index for the episodes:

  1. about scripting the shell
  2. working with directories, files and data
  3. regular expressions and pattern matching
  4. control structures, arrays and functions I
  5. control structures, arrays and functions II
  6. external programs: awk & sed