2. Starting a project
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.
-
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
andy
to quit.mv page.html sys_info.sh
vim sys_info.sh
:1,$ s/^/echo "/
:% s/$/"/
Type
1G
, then capitalO
, and write these lines:#!/bin/bash
# Program to output a system information pagePress ESC and type
:wq
.chmod +x sys_info.sh
./sys_info.sh
./sys_info.sh > sys_info.html
lynx sys_info.html
Type
q
andy
to quit. -
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.
-
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
-
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