Skip to main content

Command Palette

Search for a command to run...

Basic concepts of linux

Published
3 min read
Basic concepts of linux

Day 8

Hello everyone welcome back !!!

Myself Swanik Santosh Gudekar and I am back with my blog. This is the eighth blog of Linux series. Today I attended the eighth session conducted by Master Pranav Jambare at Dr. Babasaheb Ambedkar Technological University.

Points Covered In This Session :

  • Loops

  • Common line argument

Loops :

  • In Linux, loops are used to execute a set of commands repeatedly.

  • The for loop is one of the most commonly used loops in Linux Bash scripts.

  • It is very flexible and can work with numbers, words, arrays, command line variables or the output of other commands

Types of loops:

1) While loop :

  • In Linux, the while loop is a type of loop that continues to execute as long as the programmed condition remains true.

  • It is useful when you need to execute a set of instructions a certain number of times or when you want to create an infinite loop.

  • SYNTAX :

while <condition1>
do 
    <condition2>
done

EXAMPLE :

#Program:

#!/bin/bash
 a=1
 while [ ${a} -le 10 ];
 do
     echo "Number is ${a}"
     a=`expr ${a} + 1` 
 done

#Output: 

 Number is 1
 Number is 2
 Number is 3
 Number is 4
 Number is 5
 Number is 6
 Number is 7
 Number is 8
 Number is 9
 Number is 10

2) for loop :

  • A for loop in Linux is a way of repeating a set of commands for each item in a list or a range of values.

  • The for loop is particularly useful for processing lists of items, filenames, or any other collection of data.

  • SYNTAX :

      for var in word1 word2..wordn                  --(var=variable)
      do
          <condition>
      done
    
  • EXAMPLE :

      #program:
    
       #!/bin/bash
    
       for numbers in {1..5};
       do
           echo " ${numbers} "
       done
    
       #Output
       1
       2
       3
       4
       5
    

    3) until loop :

  • An until loop in Linux is a way of repeating a set of commands as long as a given condition is false.

  • It is similar to a while loop but with the opposite logic.

  • SYNTAX :

      until <condition>
      do
          statement executed if the command is true
      done
    
  • EXAMPLE :

 #program:

 #!/bin/bash

 A=10
 until [ ${A} == 1 ];
 do
     echo "${A} is not equal to 1"
     A=`expr ${A} - 1` 
 done

# Output: 
 10 is not equal to 1
 9 is not equal to 1
 8 is not equal to 1
 7 is not equal to 1
 6 is not equal to 1
 5 is not equal to 1
 4 is not equal to 1
 3 is not equal to 1
 2 is not equal to 1

Common line argument :

  • Common line arguments are the parameters that the user provides via the terminal to a script or a command, which uses them to perform a specific task according to user input.

  • $1 $2 $3 : Arguments passed.

  • $0 : Name of the shell program.

  • $@ : One-by-one arguments passed.

  • $# : Total number of arguments.

  • $* : All arguments together.

  • $$ : PID(Process ID) of current shell. (Actually, the program PID can be achieved).

  • $? : Status of the last command.

  • $! : PID of the last command*.*

These were the topics covered by Master Pranav Jambare in the eighth session of Linux. Thankyou for reading my blog and also do check out my previous blogs !!