“MASTERING SHELL SCRIPTING”

Varun
5 min readApr 27, 2021

“SHELL SCRIPTING WORKSHOP” UNDER THE MENTORSHIP OF THE WORLD RECORD HOLDER — Mr. VIMAL DAGA SIR ORGANIZED BY LinuxWorld.

Hello Everyone!!

I am delighted to share that I have attended Two days workshop on the topic “Shell Scripting” under the mentorship of Mr. Vimal Daga Sir. The workshop covered in-depth knowledge as well as practical implementation on how to automate command-line tasks using bash shell scripting and learning real-life industry use-cases.

The key learnings from the workshop are :

DAY-1

👉Shell scripting is a concept of creating scripts for doing tasks in an automated way. Scripts include sequences of commands that are executed by the shell. It is a default facility provided by Linux-based OS.

👉 For using an OS, we are provided with two ways:

1. GUI — Graphical user interface — provided with graphical applications.

2. CLI — Command-line interface — includes black screen/console that supports commands only.

👉 CLI is a way of accessing OS through writing commands/programs in terminal. It includes a black console and no graphical interface. It saves memory, CPU, more efficient, and looks techier.

👉 Shell is a concept. It is like an invisible program that accepts user input in form of a command, processes it, passes it to OS, and in return provides output. It is just like a medium between user and OS. It is provided in CLI mode only.

👉 BASH (Bourne Again Shell) is the standard shell provided in Linux based OS.

👉 Variable is like a container that stores a value according to its datatype for a temporary period of time.

Types of variables:

👉 Pre-defined variables: by default created by systems.

👉 User-defined variables: created by users according to their use-cases.

👉 As a user, on executing any command/program in CLI we can check whether that cmd has run successfully or not with the help of exit code.

Exit code is a numeric value that is returned by almost every command after execution. By default it is stored in “?” system variable. If its value is “0”, it means cmd ran successfully, and if any other value b/w 1–256, it means there is some error in executing.

👉 Suppose we have some cmds that need to be run again and again in CLI. So, rather than typing the same cmd, again and again, we create a file and put all cmd in it. So, whenever we have to run that cmd again we will run that file only. This file is just like a script and this script is executed by BASH shell. So, it is known as Bash Shell Scripting.

👉 A command is used for executing a single task whereas a script is like a file containing sets of commands that is used to achieve automation in tasks.

#! Represents shebang/hashbang. It is included in scripts for instructing which shell to be used for executing that script.

👉 For passing parameters in script at runtime, along with script name we pass arguments that are stored in system variables like 1,2,3,.. inside our script using $ sign, we can retrieve those arguments and can use them.

👉 For creating loop conditions, we use: for and while loops.

👉 Live_interpreter means it will run every line of code just after completion of the line it does not wait for the completion of code.

👉 Migration_operation is the way to move data from one system to other, this is used during backup.

👉 I/O redirection redirects the standard output of a command to a file without overwriting the file’s existing contents. we use >,<. Use 2 in front of redirection to redirect error and & if you don’t know will it give an error or not

👉 For taking input from the shell we use read keyword: read -p “enter the value” fir /*prompt for input and store in variable fir while is used to run till the condition get failed*/

👉 while loop is used in such conditions where a number of loops aren’t predefined.

👉 For cutting any field from any file we use it like >cut -d “:” -f 3 <filename> /*it takes field 3(3rd column) which is determined by the pattern “:”.By default value of the delimiter is “”(space)*/

👉 To add a timestamp to any file during the backup process >touch <file_name>_$(date +%e-%b-%G).txt where %e=date %b=month %G=year

DAY- 2

👉 Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing.

👉watch command in Linux is used to execute a program periodically, showing output in fullscreen.

👉for getting the total number of false client hits we use command “awk ‘$9==404 { print $1}’ access_log | sort | uniq | wc -l”

👉Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates.

👉date +%e:%b:%G will give filter dd:mm: yy from date command.

👉To run multiple commands altogether we can use &&(and), ||(or) operators.

👉There are many options available for the awk command, some are: -F for the field, -BEGIN for beginning a new block of code, -NF for the number of fields, -NR for the number of records, -END for the last line, etc.

👉To create function in shell script we use following syntax: function_name() { function_command }

👉To sort and count the line log file the command considering that it is an httpd log file: $ awk ‘{print $1}’ | sort | uniq -c | sort -nrk 1 .

👉Creating a function in a shell script:

A function named ‘abc’ can be created as:

abc { echo “ This is the ab function” }

This function can be called as: abc

👉A command is an instruction provided to the OS to perform some task using command line interface through shell whereas a function is a set of commands written together to avoid rewriting and we can use them whenever required by calling the function name.

👉To pass the value to command which requires value on run time we can use “ — stdin”

👉For searching a specific pattern using awk command:

$ awk ‘/<pattern>/ {print}’ filename”.

👉The ‘sed’ command is a stream editor and can be used to search and replace a string based on regex from a file.

In the end, I would like to thank Vimal Sir and Preeti Ma’am for always supporting us and for conducting an amazing workshop and providing such in-depth knowledge and practical implementation of the latest technologies.

I am so proud of myself for being a student of Vimal Sir and a part of LinuxWorld.

THANK YOU SO SO MUCH!!!!

HOPE YOU LIKE IT!!

#shellscripting #bash #bashshellscripting #shellscriptingbyLW #righteducation #vimaldaga #worldrecordholder #shellscriptingworkshop #workshop #terminal

--

--