Shell Scripting
This weekend , Mr Vimal Daga sir and Preeti ma’am had arranged a Live Workshop on shell scripting . It was conducted by the world record holder Mr. Vimal Daga .
We came across many new conceptual things and terminology. It was overall a great experience. While attending the session we came to alot of concepts like commands to interact with os using CLI and GUI ,bash shell ,exit codes , shebang, live interpreter, timestamp ,awk, sed, watch and many more concepts and commands.
Session Briefing
👉 Scripts :- In Linux, shells like bash support programming construct which are saved as scripts. These scripts become shell commands and hence many Linux commands are script. it is used to automate the script.A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter.
👉 An operating system is a program that controls the execution process so we can communicate with computer. Interact with OS using CLI or GUI or series of command know as scipt.
👉 The command-line interface is used to process the commands and helps interact with the OS and software. A command-line interface (CLI) processes commands to a computer program in the form of lines of text.
👉 Shell is the program that take commands and executes them on the system.It is a program that allows users to type text commands instructing the computer to do specific tasks.
👉 bash shell is one of the variety of shell to interact with kernel. Bash Shell(Bourne Again Shell) is the most used Unix Shell.Bash is an implementation of Shell and allows you to efficiently perform many tasks.
For example, you can use Bash to perform operations on multiple files quickly via the command line.
👉 Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process.Usually, for success, the exit code is 0 and for the failure or any error exit code is other than zero(any number from 1 to 255 to tell us the program has crashed.). exit code is been stored in the “?” environmental variable
to know whether the command run successfuly or not we can check its exit code:
# echo $?
👉 shebang is the character sequence consisting of the characters number sign and exclamation mark ( #!) at the beginning of a script. It is used to tell the operating system which interpreter to use to parse the rest of the file.It is also called sha-bang, hashbang, pound-bang, or hash-pling.
It starts with #!/bin/bash . It is basically to refer the path to the interpreter. # is often called sharp and ! is called Bang, hence the name sharp bang, but generally, people say it shebang instead of a sharp bang.
👉 We can pass any parameters after the “bash <script_name>” to retrive these parameters in script we use $1,$2 or $@.
👉 Live interpreter means it will run every line of code just after completion of line. It does not wait for completion of code. A command line where we get direct output is live interpreter.
👉 Migration operation means migrating the whole data from one OS to another OS to balance the workload.
👉 “read” command is used for getting user input from keyboard in a Linux shell script.
👉 timestamp can be added by using [ $(date +%F) ] command. Or may be like this
date +%e/%b/%G
👉 Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then performs the associated actions.The awk command programming language requires no compiling and allows the user to use variables, numeric function, string function and logical operators.
👉 The Watch command is used to display the output of the command at regular intervals(by default 2secs). In linux it is used to execute a program periodically showing output in fullscreen.
👉 The command for getting the total number of false client hits
awk ‘ $6==404 { print $1 “ “ $6}’ acess_log |sort|uniq -c |wc -l
👉 ‘tail’ command is used to get the output of last 10 lines of a file by default. -n <number> option can be given to change no of lines.
👉 To get filter dd:mm:yy from date output use the command date +%e:%b:%G
we can use “ man date “ command to get the mannual of date command.
👉 To run multiple commands in a single line we can separate the commands by “;” or “&&” or “||”, ‘,’ depending on the use case. touch a{1..n}.txt for creating n files without redundancy.
👉 Different option available with awk command are:
“-F” for specifying delimitor,
“END” for getting last line of output produced by awk ,
“NF” display total field in unsorted data, “NR” display row number with each row
-v var = value to declare a variable.
👉 For sort and count the line log file we have sort command and wc -l command for counting no. of line. For counting different clients that access our page the command is awk’ {print $0}’ access_log | sort l uniq -c | wc -l.
👉 To create function in shell script” function_name(){ body; } “ and calling using function_name.
👉 To pass the value to the command which requires value on the run time we can use — stdin.
eg: echo (password) passwd {user_name} — stdin.
👉 Sed command stands for streaming editor, through this we can perform lots of functions on the file like searching, replace, deletion, insertion.