Introduction to Shell Scripting

Pranav Kulshrestha
Pranav Kulshrestha
Published in
3 min readJun 10, 2019

Whenever we are using the terminal on a Linux/Unix based operating system, you are actually interacting with the shell. Shell accepts human-readable commands from the user and converts them into something which kernel can understand. The shell gets started when the user logs in or start the terminal. Anything that can be executed in the command-line can be written into a shell-script.

In this blog, we will discuss how to make a simple shell script that will automatically run your favorite Youtube video, in your default browser after a timer runs off. The arguments to the scripts will be the hours and minutes for the timer. Next, we read lines from a text file that contains the links of your favorite Youtube videos, select a random link from the file and open it in your default web-browser. This simple shell script will brief you with the basics of shell-scripting and then you can make more complex scripts for e.g;(making your own custom Linux system).

Let’s start…

The first line of a shell-script determines the interpreter for your shell script, this is a comment in the shell script which starts with a shebang then the location of the interpreter, something like

#!/bin/bash

Implementing the timer functionality

We will have to get the arguments hours and minutes for the timer to run, we will take them from the user using the concept of Positional Arguments. for e.g; if the user calls the script like:

./Video.sh <hour> <minutes>

Then, by the concept of positional parameters:

  • the value of the numbers of hours can be accessed in the script using $1
  • Similarly, the numbers of minutes can be accessed by using $2

Note: the value of the script is stored in the variable $0.

Now for the functionality of the timer, we take out the current time using the date command in the shell, then we take out the time at which timer runs off(TARGETTIME) using the number of hours and the minutes.

#Timing functionality
CURRENTTIME=$(date +%s)
TARGETTIME=$(($CURRENTTIME + $1*3600 + $2*60))
echo $TARGETTIME

Next, we will update the value of the variable CURRENTTIME, until it is equal to the variable TARGETTIME; this will complete the timer functionality.

while [ "$CURRENTTIME" != "$TARGETTIME" ]
do
CURRENTTIME=$(date +%s)
done

Before we write the code for opening the video, we will create a text file containing the links of the youtube videos. Populate this text file with the links to your favorite youtube videos, a line on the file must be a link to the video.

An example of the file can be: videos.txt

Reading the file and Opening the video

First, we will create a variable that contains a random number, using the RANDOM variable which is maintained by the shell. As we want the number within a range where the upper-limit is the number of links present in the text file. In the example, I have taken 10 links hence I will take the upper-limit as 10.

R=$(($RANDOM%10))

We will first create a counter variable that represents the current line number. Next, we will view the contexts of the file using the less command, and redirect the output of the less command to a while loop which we will use the read command to read every line present in the text file.

On subsequent iteration, we will increase the value of the counter variable by 1 until it’s value is equal to the value of the random variable, and then we will open the link of the youtube video present on that line number using the xdg-open command.

#Reading the text file and opening the site 
LINE_NUM=1
less ./videos.txt | while read LINE
do
if [ "$R" == "$LINE_NUM" ]
then
L=$LINE
echo $L
xdg-open $L
fi
((LINE_NUM++))
done

That finishes our small shell script, I think you will get some idea of how variables, loops, arguments work in a shell script. We have used a small number of commands in this script, but there are N number of commands in the shell. You can find more information about them, on the internet.

If you want complete code about this script and s other instructions, you can find it here: Youtube-AlarmClock.

--

--

Pranav Kulshrestha
Pranav Kulshrestha

Open-Source Contributor, Developer, Around bugs and exceptions most of the time