Shell usage is the most important skill of a Linux user, which is why we first introduced the concept during week 2. So far, we used bash interactively, by typing commands one after another. Often, it makes sense to execute a chain of commands automatically. Remember how repetitive it was to conduct arpspoof attacks? Actions can be automated with scripts. A basic introduction to bash syntax is available on our wiki. Below is an example of a pointless script. It does not take full advantage of bash’s many features, so that the syntax would be easier to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash

if [[ "$1" = *"-h"* ]]; then
  cat << EOF
usage: ./example.sh [OPTION]

optional arguments:
  -h, --help        show this help message and exit
  --create-folder   make a folder
EOF
  exit 0

elif [ "$1" = "--create-folder" ]; then
  
  folder_name=tux_the_penguin
  mkdir $folder_name
  echo "Made a folder, it's name is \"$folder_name\""

  printf "Should I remove that folder now? [y/N] " # no new line
  read response
  if [ "$response" = "y" ] || [ "$response" = "Y" ] || [ "${response,,}" = "yes" ]; then
    rmdir $folder_name
    echo "I removed the folder."
  fi
fi

echo "Goodbye"

Steganography (potato followup)

You can review the solution here. https://github.com/twlinux/potato/wiki

It’s your turn! Write a bash script which appends plaintext to image files. Below is sample output.

$ ./hide.sh image.jpg
Enter secret text: chicken nugger and french fried

$ ./hide.sh --reveal image.jpg
The secret is: chicken nugger and french fried

Feel free to add more features, the most impressive script will win the sticker below.

bash logo sticker