Shell Scripting in Linux

Om KadamOm Kadam
3 min read

The lecture of the Linux workshop is taken by Mr Pranav Jambare. We are covered that in Day-7 of Linux workshop.

**1)Filters:-**In UNIX/Linux, filters are the set of commands that take input from the standard input stream i.e. stdin, perform some operations and write output to the 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, uniq, diff, and sort.

1)Grep:-

 #To search word - case insensative
  grep <"word to be searced"> -i <filename>

  #Example
  grep "word" -i main
  #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 main
  #To prints output after the result
  grep <"word to be searched"> -A <filename>

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

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

#Example
grep "word" -C main
  1. Uniq:-
#To show the count of repeated lines in the file
uniq -c <filename>

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

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

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

#Example
uniq -u main
  1. Diff:-
  1.   #Syntax - It shows the different(uncommon) content between two files
        diff <file1> <file2>
        #Example
        diff main main1
    
  1. Sort:-
#To sort content of file by alphabetical order
  sort <filename>

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

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

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

#Example
sort -M main2

2)Shell Scripting:- Shell scripting is an important part of process automation in Linux. Scripting helps you write a sequence of commands in a file and then execute them.

This saves you time because you don't have to write certain commands again and again. You can perform daily tasks efficiently and even schedule them for automatic execution.

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 script:- A bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line. For example, you can navigate to a certain path, create a folder and spawn a process inside it using the command line.

  1. Shebang:- This #! is called shebang or hashbang. The shebang plays an important role in shell scripting, especially while dealing with different types of shells.

3) 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
    

Thanks for reading my blog......!!

10
Subscribe to my newsletter

Read articles from Om Kadam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Om Kadam
Om Kadam