Beginners Guide to Shell Scripting

Beginners Guide to Shell Scripting

In this article, I will talk about shell scripting, how to write script using shell to perform various tasks. We will cover the basics of shell scripting and some of the common shell scripting commands.

A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

We are mostly familiar with Graphics User Interface (GUI). The use of computer graphics to interact with the computer which translates clicks and double-clicks into commands to open files and run programs. GUI allow us to make use buttons, windows to perform various tasks But The tasks can be done using CLI ( Command Line Interface). CLI allows user to interact with operating system by entering commands in the terminal and the user press enter key , the terminal executes the commands and display the result. A shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line. most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.

This article will cover:

  1. Working with Files and Directories.

  2. Manipulating Data.

  3. Combining Tools.

Manipulating files and directories.

First lets display the username of the current user by typing whoami and then hit enter key, it will print out the name. We can also print out the current directories. Type pwd command and hit enter key.

printing out the username shell 1.png

Getting the current working directories shell 2.png

cd

Just like the GUI that let you move from one working directories to another, The CLI let you do that also by entering cd follow by the name of the directories. For example typing cd Desktop and hit enter will change your current directories to Desktop. shell3.png

ls

We can list out the content of a directory by typing the ls command follow by the name of the directory, and if you want to list the content of the current directory you are, typing just ls will list all the folders and file in that directory.

shell 4.png

cp, mv & rm

cp is use to copy file from one directory to another,mv is use to move file from directory to another and rm is use to delete file. When you delete files in CLI, it is deleted permanently. shell 5.png

we can duplicate the test.csv by typing the code snippet below

 cp  test.csv test_duplicate.csv

shell 6.png

While cp copies a file, mv moves it from one directory to another. It handles the parameters the same as cp. It can also used to rename files when run mv test.csv test_old.csv shell 7.png Note it overwrites the existing files.

Unlike GUI that pushed whatever you deleted to trash, It removes the file you're trying to delete permanently from the computer. rm old_test.csv test_duplicate.csv removes both old_test.csv and test_duplicate.csv files.
shell 8.png

rmdir

Directory can be removed with rmdir command but this can only be possible if the directory is empty. So you have to delete all the files in the directory before you can delete that particular directory.

Manipulating data.

cat

Sometimes before you delete files you may want to look at their content, cat commands is used to print out the content of a file. shell 9.png

head & tail

head command as the name suggest prints out the first 10 lines of a file, head -n 5 let you display the first 5 lines instead. shell 10.png

Just like head print of out the first 10 lines of a file, tail can be use to print the last 10 lines.

cut

While head and tail are use to print out the rows of a file, cut can be use to print out the columns of a file. cut uses -f (fields) flag to specify columns and -d (delimiter) flag to specify the separator. cut -f 2-5,8 -d , test.csv command will print the following results. shell 12.png

grep

grep takes a piece of text followed by the filename(s) and prints all of the lines in those files. grep comes with some flags that can be attached to it when entering the commands.

  • -c: print a count of matching lines rather than the lines themselves
  • -h: do not print the names of files when searching multiple files
  • -i: ignore case (e.g., treat "Regression" and "regression" as matches)
  • -l: print the names of files that contain matches, not the matches
  • -n: print line numbers for matching lines
  • -v: invert the match, i.e., only show lines that don't match

We can get the number of times Reg appears in the test.csv and train.csv files with grep -c Reg test.csv train.csv commands. shell 13.png

word count

The command wc short for word count, counts the character, word and line of a file with the use of -c, -w, -l flags respectively. shell 15.png

sort

sort arrange the content of the file in ascending alphabetical order. It comes with various flags that can be use to manipulate the sorting of the data.

  • -n is used to sort a data numerically.
  • -r is used to arrange a data in a reverse order.
  • -b tells it to ignore leading blanks.
  • -f tells it to fold case i.e ignore case sensitivity.
    cut -d , -f 2 lesson/Test.csv | grep -v Reg | sort
    

    uniq

    uniq is often use with sort. It removes the adjacent duplicated lines. if we have file that contains duplicate words following each other, it will keep only the first one and discard the rest but if the duplicate words are not following each other then it will print the file as it is. ``` head -n 10 shell | sort | uniq -c
### Combining tools
####  Redirection
We can save the output of a command to anywhere by using **redirection** `>` command.  `head -n 2 top_5.txt > top_2.txt` will save the first two line of **top_5.txt** to **top_2.txt** 

#### Pipe 
The pipe ` |` symbol tells the shell to use the output of the command on the left as the input to the command on the right.  You can combine as many commands together using `|` command

head -n 20 shell | grep -c shell

``` The above chain of commands will:

  1. select the first 20 lines of shell file
  2. count the number of times the word shell appears.

Conclusion

In this article, we started off with how to get the username of the current user and how to print the current working directory using pwd.

We proceed to how to change working directories using cd and you can list out the files and folders present in a directory using ls. We discuss how we can copy, move and delete files using (cp, mv, rm), we also discussed how to delete directories.

We then check out how to print out the content of a file using cat, head and tail, how to count words words, characters and line in a file.