Getting Started with Basic Script Building in Linux

Cyberwizardy23
5 min readMar 1, 2023

--

Basic Script Building in Linux

Now that we’ve learned about basics of the Linux system and the commands, now we can start coding. We will learn basics of writing shell scripts.

The key to shell scripts is the ability to enter multiple commands and process the results from each command, even possibly passing the results of one command to another.

We can run two commands together, separated with a semicolon:

┌──(me㉿kali)-[~/Desktop/medium_blogs]
└─$ date ; who
Saturday 11 February 2023 04:32:29 PM UTC
me :1 2023–02–11 16:25 (:1)

This simple script uses just two bash shell commands. The date command runs first, displaying the current date and time, followed by the output of the who command, showing who is currently logged on to the system.

Using this technique is fine for small scripts, but it has major drawback:we must enter the entire command at the command prompt every time we want to run it. Instead of doing this we can combine the commands into a simple text file. When we need to run the commands, just simply run the text file.

How to Create a Script File in Linux: A Step-by-Step Guide

To place shell commands in the text file, we need a text editor to create a file and then enter the commands into the file.

When creating a shell script file, we must specify the shell we are using in the first line of the file. format for this:

#!/bin/bash

In a normal shell script line, the hash(#) sign is used as command line. A comment line in the shell script isn’t execute by the shell. but here the first line of the shell script file is special case, and the hash sign followed by the exclamation point tells the shell what shell to run the the script under.

After indicating the shell, commands are entered onto each line of the file. for example:

#!/bin/bash
# This is script displays the date and the user logged in
date
who

We can use the semicolon and put both commands on the same line and also we can list commands on separate lines. Comments are very useful when we want to leave message for ourself or for other who can use it about what’s happening in the script. and save this script in a file and give name whatever you want. after saving file if we try to run it won’t because we need to configure somethings.

The PATH environment variable is set to look for commands in a handful of directories. To get the shell to find the test script, we need to do one of two things:

  1. Add the directory where our shell script file is located to the PATH environment variable.

2. Use an absolute or relative file path to reference our shell script file in the prompt.

We will use second method to tell the shell exactly where the script file is located. reference a file in the current directory, we can use the single dot operator in the shell:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ ./test
zsh: permission denied: ./test

Now shell found the script but it indicates that we don’t have permission to execute the file. so we’ll check permission for test file:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ ls -l test
-rw-r — r — 1 me me 79 Feb 11 16:51 test

As we can see we don’t have execution permission, now we’ll give execution permission to the user using chmod u+x command:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ ./test
Saturday 11 February 2023 05:00:51 PM UTC
me :1 2023–02–11 16:25 (:1)

As we can see we’ve successfully run our script using command line.

Navigating the Shell: How to Display Messages in scripts

Most shell command produce their own output, which is displayed on the console monitor where the shell script is running. Some times we need to display our own messages to help the script user know what is happening within the script. We can do this with the echo command to display simple text:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ echo What is happening there
What is happening there

sometimes it becomes tricky when we are using quotes within our strings:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ echo we’ll see if this’ll work
well see if thisll work

The echo command uses either double or single quotes to delineate text strings. We need to use on type of quote within the text and the other type to delineate the string:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ echo “ We’ll see if this’ll work “
We’ll see if this’ll work

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ echo ‘ John says “You are good at code”.’
John says “You are good at code”.

As we can see the all quotation marks appears properly in the output.

We can add echo statements anywhere in our shell scripts where we need to display some information:

#!/bin/bash
# This is script displays the date and the user logged in
echo The time and date are:
date
echo "See who's logged into the system:"
who

The output:
┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ ./test
The time and date are:
Wednesday 01 March 2023 03:33:38 PM UTC
See who’s logged into the system:
me :1 2023–03–01 13:50 (:1)

We can use -n parameter to display a text string on the same line as a command output for in our above example add -n parameter in our first echo :

echo -n “The time and date are: “

We need to use quotes around the string to ensure that there’s a space at the end of the echoed string. The command output begins exactly where the string output stops. for example:

┌──(me㉿kali)-[~/Desktop/medium_blogs/Practice]
└─$ ./test
The time and date are: Wednesday 01 March 2023 03:33:38 PM UTC
See who’s logged into the system:
me :1 2023–03–01 13:50 (:1)

The echo command is a crucial piece of shell scripts that interact with users. it will help to display many things like values of script variable.

Thanks For Reading. In next blog we will see how to use variables in our scripts.

--

--

Cyberwizardy23

Linux and Networking Learner , Interested in Cyber Security and Active Learner