Skip to main content

Command Palette

Search for a command to run...

Introduction to Shell Scripting in Linux

Day 7 : Linux Workshop

Published
•5 min read•View as Markdown
Introduction to Shell Scripting in Linux

Hey Guys,

I am back with the Linux Workshop blog series held by our alumni Mr. Pranav Jambare sir.

The following topics are covered in the Day-7 of workshop.

Content:

  1. Filters

  2. Shell Scripting

  3. If-Else Structure

Filters:

A filter is a small and specialized program in the Linux operating system to get meaningful input from the user/client and with the help of a few other filters and pipes to perform a series of operations to get a highly unique or specified result.

Some commands of Filters:

  1. Sort:
#To sort content of file by alphabetical order
  sort <filename>

#Example
sort TestFile
#To sort the content of file by reverse alphabetical order
  sort -r <filename>

#Example
sort -r TestFile
#To sort the numeric content of the file
  sort -n <filename>

#Example
sort -n TestFile1
 #To sort the month names in the file
  sort -M <filename>

#Example
sort -M TestFile2
  1. Head
#To show specific number of lines from the content of the file
head -n <number of lines> <filename>

#Example
head -n 5 TestFile
#To display the specific number of bytes of content of the file
head -c <number of bytes> <filename>

#Example
head -c 500 TestFile
#To show the content of multiple files
head <file1> <file2>

#Example
head TestFile TestFile1
#To show merged content of multiple files
head -q <file1> <file2>

#Example
head -q TestFile TestFile1
#To show the content of the file preceded by it's file name
head -v <filename>

#Example
head -v TestFile
  1. Tail
#To get the dynamic output
tail -f <filename>

#Example
tail -f TestFile
#To get the content on the particular line of the file
tail -<line number> <filename>

#Example
tail -5 TestFile
#To display specific bytes of content of the file
tail -c <number of bytes> <filename>

#Example
tail -c 200 TestFile
#To display content of multiple files
tail <file1> <file2>

#Example
tail TestFile TestFile1
  1. WC (Word Count)
 #To show the number of lines in the file
  wc -l <filename>

#Example
wc -l TestFile
#To show characters(letters) of the content in the file
  wc -m <filename>

#Example
wc -m TestFile
#To show number of words of the content in the file
  wc -w <filename>

#Example
wc -w TestFile
#To show the specific number of bytes of content of the file
  wc -c <filename>

#Example
wc -c TestFile
  1. comm
#Syntax - It shows the common content between two files
 comm <file1> <file2> 
 #Example
 comm TestFile TestFile1
  1. diff :

      #Syntax - It shows the different(uncommon) content between two files
      diff <file1> <file2>
      #Example
      diff TestFile TestFile1
    
  2. uniq

#To show the count of repeated lines in the file
uniq -c <filename>

#Example
uniq -c TestFile
#To print repeated lines in the file
uniq -d <filename>

#Example
uniq -d TestFile
#To
uniq -D <filename>

#Example
uniq -D TestFile
#To show unique lines in the content of the file
uniq -u <filename>

#Example
uniq -u TestFile
  1. grep
  #To search word - case insensative
  grep <"word to be searced"> -i <filename>

  #Example
  grep "word" -i TestFile
  #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>

#Example
grep "word" -o TestFile
  #To prints output after the result
  grep <"word to be searched"> -A <filename>

#Example
grep "word" -A TestFile
  #To prints output before the result
  grep <"word to be searched"> -B <filename>

#Example
grep "word" -B TestFile
  #To prints output after and before the result
  grep <"word to be searched"> -C <filename>

#Example
grep "word" -C TestFile
  #To print the count of the word in the file
  grep <"word to be searched"> -c <filename>

#Example
grep "word" -c TestFile
  #To print the matched word in the file
  grep <"word to be searched"> -w <filename>

#Example
grep "word" -w TestFile
  #To print multiple matched words
  grep <"word to be searched"> -e <filename>

#Example
grep "word" -e TestFile
  #To print the matched lines with line numbers
  grep <"word to be searched"> -n <filename>

#Example
grep "word" -n TestFile
  1. cut
#Syntax - Deluminator
cut -d '<deluminator>' -f <filename>

#Example
cut -d '-' -f TestFile
  1. sed
#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>

Shell Script:

A shell script is a text file that contains a sequence of commands for a UNIX-based operating system. It is called a shell script because it combines a sequence of commands, that would otherwise have to be typed into the keyboard one at a time, into a single script. The shell is the operating system's command-line interface (CLI) and interpreter for the set of commands that are used to communicate with the system.

Shebang:

  • Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Syntax:

#!/bin/bash

If-Else Structure:

1.Simple If :

  • Syntax :

        if [expression]
        then
            statement
        fi
    

    2. If-Else :

  • Syntax :

        if [expression]
        then
            statement
        else 
            statement
        fi
    

    3. Else If :

  • Syntax:

        if [expression]
        then
            Statement
        elif [expression]
        then
            Statement
        elif [expression]
        then
            Statement
        else
            Statement
        fi
    

4. Nested - If :

  • Syntax :

        if [expression]
        then
            Statement
            if [expression]
            then
                Statement
            else
                Statement
            fi
        else
            Statement
        fi
    

    All these topics were covered on the 7th Day of the Linux Workshop.

    Thank you for reading my Blog!!