Linux Workshop

Ayush BhosaleAyush Bhosale
6 min read

Hello guys....!!!

I am back with another exciting blog about the series of the Linux workshop.

This is the seventh blog regarding the Linux workshop series conducted by Mr. Pranav Jambare sir.

In this blog, we going to discuss "Shell Script" in Linux.

Let's start with content.

Content

  • Filters in Linux

  • Shell Script

  • If-Else Statements

Filters in Linux

  • A filter is a program that takes plain text (stored in a file or generated by another program) as standard input, converts it to a meaningful format, and then returns it as standard output.

  • In Linux, filters are programs or commands that take input from standard input (stdin), process it in some way, and then output the result to standard output (stdout).

  • Filters are often used in combination with other commands to create powerful and flexible pipelines for processing data.

    Filters can be combined using the pipe symbol (|) to create complex processing pipelines.

  • Filters are a powerful tool for working with data in Linux and are an essential part of the command line interface.

  • Some common filters in Linux include:

    1. -grep commands :-

      • -i :- For case insensitive.

      • -o :- For match part only

  • -A -B -C :- After Before Both

  • -w :- For exact word

  • -n :- for match line and number

  1. -sed commands :-

    • sed ‘s/word to be replaced/replacing word/2’ filename = Replace the second occurrence of the word to be replaced

    • sed ‘s/word to be replaced/replacing word/g’ filename = Replaces all occurrences of the word to be replaced

    • sed ‘3 s/ word to be replaced/replacing word/g’ filename = ‘3’ represents the line number and replace the word to be replaced at 3rd line

    • sed ‘1,3d’ filename = ‘1,3’ is line range and this is deleted as there is ‘d’ means delete and other lines are printed

    • sed ‘1,3p’ filename = Prints all the entries along with 1-3 range again after it

    • sed ‘/word/d’ filename = Delete entries with the specified word.

  2. -awk commands -

    • awk '{print}' filename

    • awk '/word/' '{print}' filename

    • awk '{print $1, $4}' filename

    • awk '{print $1, $NF}' filename

    • awk 'NR==3, NR==6 ' {print NR ,$0}' filename

  3. uniq commands :-

    • uniq -c :- For count repetition.

    • uniq -d :- For only print repeted lines.

    • uniq -u :- For uniq line.

    • uniq -D :- Repeted with occurence.

  4. Sort Command :-

    syntax :- Syntax – sort options [filename]

    • sort -n filename = sorting by numbers

    • sort -nr filename = sorting by reverse numbering

    • sort -c filename = checks whether files are sorted or not aip

    • sort -u filename = sort and remove duplicate entry

    • sort -M filename = sorting by months

  5. Tail command :-

    Syntax – tail options [filename]

    • tail -n filename = ‘n’ stands for number and prints last entries

    • tail -f filename1 filename2 = To get dynamic output

    • tail -q filename = More than one file

  6. wc ( word count ):-

    Syntax – wc options [filename]

    • wc -l filename = Counts number of lines.

    • wc -m filename = Counts number of characters.

    • wc -c filename = Counts a number of bytes.

    • wc -w filename = Counts number of words.

  7. Head :-

    Syntax – head options filename

    • head -n filename = ‘n’ stands for number and prints top number of entries
  8. comm :-

    Syntax – comm Fileno.1 Fileno.2

    • Compare two sorted files line by line and prints the lines that are unique to each file.
  9. diff :-

    Syntax – diff Fileno.1 Fileno.2

    • Compare the contents of two files and prints the differences between them.
  10. cut :-

    • cut -d” ” -f2 filename = Prints the field 2

Shell Script

  1. A shell script in Linux is a program written in a scripting language that can be executed in a Unix/Linux shell.

    List of commands in a program/code that is run by the Unix shell.

  2. Shell scripts are used to automate tasks, execute commands, and perform system administration tasks.

  3. They are typically written in Bash shell.

  4. Shell scripts can perform a wide variety of tasks, from simple file operations to complex system administration tasks. Some common uses of shell scripts include :-

    • Automating backups and file transfers

    • Managing system processes and services

    • Monitoring system logs and performance

    • Configuring system settings and preferences

Shebang

  1. A shebang in a shell script is a special sequence of characters, at the beginning of the script that tells the operating system which interpreter to use to execute the script.

  2. shebang is a first line of script

  3. It consists of the characters "#!" (hash and exclamation point), followed by the path to the interpreter that should be used to run the script.

  4. If a shell script is written in Bash, the shebang line at the beginning of the script would typically be:

    #!/bin/bash

Creating a Script

  • Create file using .sh :
  vim shell.sh

Modify The File :-

#Syntax
  #!/bin/bash/
  <command>

  #Example
  #!/bin/bash/
  cd /tmp/
  touch user
  ls -l

After this save the file and exit.

Run Script File

 #Run by giving the execute permissions
  chmod u+x shell.sh
  ./shell.sh

  #Run without giving execute permissions
  bash shell.sh

Dclaration of Variable

There is two types of Variable

1. Temporary Variable

2. Permanent variable

Temporary Variable :-

 #Syntax
  <variable name>=<content>
  #Example
  name=Ayush

Permanent variable :-

 #Make changes in for Permanent changes  /etc/bashrc file 
  vim /etc/bashrc
  #Add your variables here in bashrc file
  #Syntax
  <variable name>=<content>
  #Example
  name=Ayush

If-else statement

  1. Simple if

Syntax :-

 if [expression]
  then
      statement
  fi

Example :-

#!/bin/bash
a=25
b=40

if [ $a -lt $b ]
then
        echo "a is less than b"
fi
  1. if-statement :-
  • syntax :-
 if [expression]
  then
      statement
  else 
      statement
  fi

Example :

#!/bin/bash
a=20
b=40

if [ $a -lt $b ]
then
        echo "a is less than b"
else
        echo "b is less than a"
fi
  1. Multiple If-Else Statement
    Syntax :-
if [ expression1 ]
then
   statement1
   statement2
   .
   .
elif [ expression2 ]
then
   statement3
   statement4
   .
 eg.Syntax  .
else
   statement5
fi

Example :-

#!/bin/bash
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"
 elif [ ${age} -gt 18 -a ${age} -lt 30 ]
  then
      echo "You are a adult"
 elif [ ${age} -gt 30 -a ${age} -lt 60 ]
  then
      echo "You are a senior citizen"
  else
      echo "Invalid age"
  fi

Nested if-Else statement :-
Syntax :-

if [ expression1 ]
then
   statement1
   statement2
   .
else
   if [ expression2 ]
   then
      statement3
      .
   fi
fi

Example :-

  •    if [ ${Firstname} == Ayush ]
        then
            echo "First name matched"
            if [ ${Surnamename} == Bhosale ]
            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
    

These important topics are covered on the day - 7 of the Linux Workshop.

Thank you for reading the blog....!!


22
Subscribe to my newsletter

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

Written by

Ayush Bhosale
Ayush Bhosale

Student at Dr. Babasaheb Ambedkar University Lonere, in computer science