Extreming the globe of Linux
Hello everyone !!!
Myself Smaran Sandep Dambe , and I am back with my next blog . So this is gonna be my 7 blog which is held in DBATU by Master Pranav Jambare . In this session the points that are covered are listed below :
1 . Filters
2 . Shell scripting introduction
3 . if-else statement
So let's start our blog !!!
1 . Filters :
In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin, perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, more, sort.The FILTER command is used to append to or replace the current path name filter. If the current display is a personal list, then the filter can only be replaced.
- Linux Filter Commands with syntax and examples :
Sort :
Syntax
#To sort content of file by alphabetical order sort <filename> #To sort the content of file by reverse alphabetical order sort -r <filename> #To sort the numeric content of the file sort -n <filename> #To sort the month names in the file sort -M <filename>
Example :
#Lets assume we have TestFile with normal unsorted content #TestFile1 with numerical content #And TestFile2 with month names sort TestFile sort -r TestFile sort -n TestFile1 sort -M TestFile2
Tail :
Syntax
#To get the dynamic output tail -f <filename> #To get the content on the particular line of the file tail -<line number> <filename> #To display specific bytes of content of the file tail -c <number of bytes> <filename> #To display content of multiple files tail <file1> <file2>
Example :
tail -f TestFile tail -5 TestFile tail -c 200 TestFile tail TestFile TestFile1
Head :
Syntax
#To show specific number of lines from the content of the file head -n <number of lines> <filename> #To display the specific number of bytes of content of the file head -c <number of bytes> <filename> #To show the content of multiple files head <file1> <file2> #To show merged content of multiple files head -q <file1> <file2> #To show the content of the file preceded by it's file name head -v <filename>
Example :
head -n 5 TestFile head -c 500 TestFile head TestFile TestFile1 head -q TestFile TestFile1 head -v TestFile
WC (word count) :
Syntax
#To show the number of lines in the file wc -l <filename> #To show characters(letters) of the content in the file wc -m <filename> #To show number of words of the content in the file wc -w <filename> #To show the specific number of bytes of content of the file wc -c <filename>
Example :
wc -l TestFile wc -m TestFile wc -w TestFile wc -c TestFile
uniq :
Syntax
#To show the count of repeated lines in the file uniq -c <filename> #To print repeated lines in the file uniq -d <filename> #To uniq -D <filename> #To show unique lines in the content of the file uniq -u <filename>
Example :
uniq -c TestFile uniq -d TestFile uniq -D TestFile uniq -u TestFile
comm :
#Syntax - It shows the common content between two files comm <file1> <file2> #Example comm TestFile TestFile1
diff :
#Syntax - It shows the different(uncommon) content between two files diff <file1> <file2> #Example diff TestFile TestFile1
grep :
Syntax
#To search word - case insensative grep <"word to be searced"> -i <filename> #To print only the matched parts of a matching line,with each such part on a separate output line grep <"word to be searched"> -o <filename> #To prints output after the result grep <"word to be searched"> -A <filename> #To prints output before the result grep <"word to be searched"> -B <filename> #To prints output after and before the result grep <"word to be searched"> -C <filename> #To print the count of the word in the file grep <"word to be searched"> -c <filename> #To print the matched word in the file grep <"word to be searched"> -w <filename> #To print multiple matched words grep <"word to be searched"> -e <filename> #To print the matched lines with line numbers grep <"word to be searched"> -n <filename>
Example :
grep "word" -i TestFile grep "word" -o TestFile grep "word" -A TestFile grep "word" -B TestFile grep "word" -C TestFile grep "word" -c TestFile grep "word" -w TestFile grep "word" -e TestFile grep "word" -n TestFile
awk :
Syntax
#To print the content of the file awk '{print}' <filename> #To print the lines which matches the given word in the file awk '/word/' '{print}' <filename> #To print the specific words of the lines in the file awk '{print $<line number>, $<line number>}' <filename> #To print the last line of the file awk '{print $NF}' <filename> #To print the particular words of particular lines of the file awk 'NR==<number of word>, NR==<number of word>' '{print $<number of line>, $<number of line>}' <filename>
Example :
awk '{print}' TestFile awk '/computer/' '{print}' TestFile awk '{print $1, $3}' TestFile awk '{print $NF}' TestFile awk 'NR==1, NR==3' '{print $1, $3}' TestFile
cut :
#Syntax - Deluminator cut -d '<deluminator>' -f <filename> #Example cut -d '-' -f TestFile
sed :
Syntax
#To replace the word by occurance sed 's/<word to be replaced>/<replaced with>/<occurance>' <filename> #To replace the word globally sed 's/<word to be replaced>/<replaced with>/g' <filename> #To replace the word on the specific line sed '<line number> s/<word to be replaced>/<replaced with>/ ' <filename> #To delete the last line sed '$d' <filename> #To delete the particular lines sed '<number of line>d' <filename> #To delete the word matching lines sed '/<word to be matched>/d' <filename>
Example :
sed 's/desktop/laptop/2' TestFile sed 's/desktop/laptop/g' TestFile sed '3 s/desktop/latop/ ' TestFile sed '$d' TestFile sed '2d' TestFile sed '/computer/d' TestFile
2 . Shell Scripting Introduction :
A shell script is a computer program designed to be run by a 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.Shell scripting is not a single language but, because it uses some natural language commands, it's easy to learn, even without a programming background. However, each shell scripting dialect is considered a language, and if you plan more complex activities, shells take a lot of practice.They are used to get routine backups by admins. They are easier to write and debug than other programming languages like C or C++. We can transfer the shell script to other UNIX and similar operating systems and execute. Shell scripts are also used to monitor systems regularly.
You can also set certain scripts to execute on startup such as showing a particular message on launching a new session or setting certain environment variables.
1 . Bash Scripts :
Bash scripting is a subset of shell scripting. Shell scripting is a method to automate tasks as a collection of commands. The bash script is one form of shell script. Shells may be one of Korn, C shell, Bourne, Bash, etc.Bash as a scripting language. To create a bash script, you place #!/bin/bash at the top of the file. To execute the script from the current directory, you can run ./scriptname and pass any parameters you wish. When the shell executes a script, it finds the #!/path/to/interpreter .
2 . Shebang :
On Linux, a shebang (#!) is a special line at the beginning of a script that tells the operating system which interpreter to use when executing the script. This line, also known as a hashbang, shabang or "sharp-exclamation", is the first line of a dash and starts with "#!" followed by the path to interpreter.#!/bin/bash is a sequence of characters (#!) called shebang and is used to tell the Linux OS which interpreter to use to parse the rest of the file. You will always see #!/bin/bash or #!/usr/bin/env bash as the first line when writing or reading bash scripts
#Syntax #!/bin/bash
Creating the script :
Create a file with
.sh
extension :COPY
vim TestScript.sh
Modify the same file :
#Syntax #!/bin/bash/ <command> #Example #!/bin/bash/ cd /tmp/ touch testfile ls -l
Then save and exit.
Run the script :
#Run by giving the execute permissions chmod u+x TestScript.sh ./TestScript.sh #Run without giving execute permissions bash TestScript.sh
Variable declaration :
Temporary variables :
#Syntax <variable name>=<content> #Example name=Shreyas
Permanent variables :
#Navigate through /etc/bashrc file vim /etc/bashrc #Add your variables here #Syntax <variable name>=<content> #Example name=Shreyas
Important Point :
Operator | Respective Option | ||
\> | -gt | ||
< | -lt | ||
&& | -a | ||
-o |
3 . if-else statement
Simple If :
Syntax :
if [expression] then statement fi
Example :
if [ ${A} -gt ${B} ] then echo "${A} is greater than ${B}" fi
If-Else :
Syntax :
if [expression] then statement else statement fi
Example :
if [ ${A} -gt ${B} ] then echo "${A} is greater than ${B}" else echo "${B} is greater than ${A}" fi
Multiple If :
Syntax :
if [expression] then Statement elif [expression] then Statement elif [expression] then Statement else Statement fi
Example :
if [ ${age} -gt 0 -a ${age} -lt 15 ] then echo "You are a kid" elif [ ${age} -gt 15 -a ${age} -lt 18 ] then echo "You are a teen" else echo "Invalid age" fi
Nested - If :
Syntax :
if [expression] then Statement if [expression] then Statement else Statement fi else Statement fi
Example :
if [ ${Fname} == Shreyas ] then echo "First name matched" if [ ${Sname} == Limaye ] then echo "First name and Surname both matched" else echo "First name matched but surname doesn't matched" fi else echo "First name doesn't matched stopped execution" fi
So this was all about what I learnt in session 7 of Linux . This session was so good because all the practical knowledge which was given by Pranav Jambare sir was up yo mark...... So please read it ..... and like it!!!!!!
Subscribe to my newsletter
Read articles from Smaran Sandeep Dambe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by