Shell Script Workshop

Ayush Garg
3 min readApr 27, 2021

--

In this session I learned:-

Shell is a component which accepts the input from the user and processes the data.
An operating system is a program that controls the execution process so we can communicate with computer
CLI which process the command given by user and executes the command
Shell take input, run and return the output
Bash is a command which provides commands to the shell.
Variables are used to store the values.
There two types of variable
>Predefine Variables- they are pre-created by developer
>User define Variables- created by user
Exit codes are a number between 0 and 255, which tells whether the output of command is right or wrong.
A script is a set of multiple tasks or commands to be performed in a particular order.
A command is a single task to be performed while a script is a set of commands to be performed in a particular order.
we have to write #! in the beginning of script it is called as hashbang/shebang.
To parameterize the script pass the parameters while running the script
for i in a b c d e; do echo “$i”;done is an example of loops.
Live interpreter gives the output of a command instantly.
Migration operation is the process of transferring files from one machine to another.
I/O redirection means take input and user further, send some output by using symbols (<, >)
input out put redirection <> is used to redirect the input or output to any place like a file for example
While loop is used to run until we reach a specific condition.
To cut a field from file we use cut command
Timestamp can be added with the { } by storing the time in a variable

“awk” is a function provided by Bash shell, which helps to search in some data & once it finds the matching keywords then it print the output. “watch” is a linux command which helps to run any program in a chronic order.
“awk ‘$9==404 {print $0}’ access_log | sort | uniq | wc -l” is the command for getting the total number of false client hits.
“tail” command is used to print the last few lines from a file. We can use “date +%e:%b:%G” to format the output of date command into dd:mm:yy.
To run multiple commands in shell altogether we can use “&&” and “||” sign in between the two commands.
To create multiple files without redundancy we can use “touch file(1..10).txt” command.
awk command has lots of different features but some of the very much useful options are “END” — which helps to get the last line from the o/p of the awk command. Next “NR” helps to show the row number & “NF” helps to calculate the total number of fields the lines are taking in each row.
To sort & count the number lines from the log file, we just need to send the log file via “sort” command to “wc -l” command using pipe (|) operation.
To create function in shell scripting we need to follow the syntax — “Function_Name () { Function_Definition }”
Commands are programs that we install in our OS & then when we call that command the respective interpreter do the further process, whereas function is just a sample definition in shell, so that using that function we can run some commands to automate tasks.
Using “read” keyword we can pass the arguments to our commands while run time.
In “awk” command we can use “/PATTERN/” before the actual code block to search a pattern. “sed” is a command that helps us to find or search some string from a file, also it has the capability to replace some strings.

--

--

Ayush Garg
Ayush Garg

Written by Ayush Garg

0 Followers

I am writer

No responses yet