Day 48 - BASH Scripting In Linux

A Shell is a command interpreter or an interface between a computer's OS and the user that enables the user to manipulate files and run commands, programs etc.

There are many shells available for Linux like Zsh or Z-Shell and C Shell but here we are only going to talk about BASH or Bourne-Again Shell because BASH is very widely used and comes pre-installed in most of the Linux Distros and also in MAC-OS and other UNIX like OS.

BASH Scripting

Scripting allows us to run multiple commands automatically at a time that if done by a human would have to type one after another. BASH Scripting a script written to be executed by the BASH.

To write a script we need a Text-Editor and for that I will be using VI because it comes pre-installed with Linux.

Linux> touch newscript
Linux> sudo vi newscript
#! /bin/bash
echo "This is a simple bash script"

This script written above has two lines in it, the first one start with # and ! which is called a shebang and after that we define what shell should run this script which is /bin/bash

The second one is an actual command that we use in the shell. We learned about some commands in Day-34 and Day-41.

Any newly created script file is not executeable by default so you have to add execute permission to the file that we talked about in Day-39. After adding permission to we can run it in the shell.

Linux> ./newscript
This is a simple bash script

Variables and Input In BASH Script

You can use vaiables and ask for input from the user to add more functionality in your BASH scripts.

Linux> touch secscript.sh
Linux> sudo vi secscript.sh
#! /bin/bash
echo "What's your name?"
read name
echo "Hello There, " $name

Here a new keyword is used which is read which is used to read the the text that you input in the shell before you hit enter and the name after the read is a varibale that stores the string that you type in the shell. Now after adding permissions to it I will run the script.

Linux> ./secscript.sh
What's your name?
Linus
Hello There, Linus

Example Script

!# /bin/bash
nmap -sT 192.168.10.0/24 > script-results
cat script-results | grep open > open-ports
cat open-ports

This script will search for all open ports on the netwrok using nmap and save the output result in the script-result file and then grep is used to filter out lines and save these lines in open-ports file which then cat commands prints out. This is a very simple script but more complex ones can be written using common keywords that are available to us for use in the scripts.

Common Built-in BASH Commands

Thses are the most common built-in BASH commands that can be used just like a programming language to write more complex scripts.

Command Function
cd Changes Directory
echo Displays the arugment in shell
exit Exits the shell
bg Puts a job in the background
fg Puts a job in the foreground
jobs Displays list of background jobs
read Reads a user input
export Make a variable available to other programs
readonly Declares a varibale as readonly
set Displays list of all vaiables
unset Deletes values from a variable
wait Waits for a background process to complete
test Evaluates arguments
exec Executes a command without creating a new process

These are some common commands that I rememmber but there are alot of them that can help in creating more useful scripts. Scripting is essential for automating tasks and being more productive in using the Linux and the Shell.


zainscizainsci