Skip to main content

2. Starting a project

note

Before starting, let's get first some example files:

mkdir -p examples
cd examples/

wget https://linux-cli.fs.al/examples/lesson10.tgz

tar xfz lesson10.tgz

cd lesson10/
ls

We will start to write a script that generates a report about various status and statistics of the system. This report will be in the HTML format.

  1. The first step is to write a program (script) that outputs a basic HTML page to the standard output. An example of a basic HTML page is on the file page.html:

    cat page.html
    lynx page.html

    Type q and y to quit.

    mv page.html sys_info.sh
    vim sys_info.sh
    :1,$ s/^/echo "/
    :% s/$/"/

    Type 1G, then capital O, and write these lines:

    #!/bin/bash

    # Program to output a system information page

    Press ESC and type :wq.

    chmod +x sys_info.sh
    ./sys_info.sh
    ./sys_info.sh > sys_info.html
    lynx sys_info.html

    Type q and y to quit.

  2. We can make this script more simple and clear by using a single echo:

    vim sys_info.sh
    :6,$ s/echo "//
    :5,$-1 s/"$//
    :wq
    ./sys_info.sh

    A quoted string may contain newlines, and therefore contain multiple lines of text.

  3. Let's put some data on the report:

    vim sys_info.sh
    :% s/Page Title/System Information Report/
    :% s#Page body#<h1>System Information Report</h1>#
    :wq
    ./sys_info.sh
  4. We can use a variable to avoid the repetition of the text "System Information Report":

    vim sys_info.sh
    :% s/System Information Report/$title/
    /echo

    Press capital O to open a new line above and write:

    title="System Information Report"

    Press ESC and type :wq.

    ./sys_info.sh
Loading asciinema cast...