Posts

Showing posts from February, 2023

If…elif…else Statement in Shell Script

Image
  If…elif…else Statement This is one of the type of selection statement.   Note It is an important to note that, the simple if, if else and if-elif-else statements should be closed by fi keyword. Syntax if [ condition ] then      true statement elif [ condition ] then      true statement else      false statement fi Example 1 - Shell Style Code Biggest of Three Numbers Source Code echo "Enter the inputs: " read a b c if [ $a -gt $b -a $a -gt $c ]          # -a indicates logical AND then     echo "$a is big" elif [ $b -gt $c ] then     echo "$b is big" else     echo "$c is big" fi Output NOTE It is not possible to directly use logical operator symbols in shell script. The statement below the shows the example Logical Operators     Operator                                Operators in Shell         Logical AND                    &&                   -a     Logical OR                              ||                         -o

While Loop in Shell Script Examples

Image
WHILE LOOP IN SHELL SCRIPT While Loop It is an example of looping statements or iteration statements It is used to execute the statements , if the condition is true otherwise it will skip the loop It is an alternative for normal for loop. Syntax      while [ condition ]       do true statement      done Types of Styles Shell Style C Style Shell Style  Here, we have to use the default shell style of syntax for writing the expression in the shell script. C Style in Shell Script Here, we can write code for expression using c language style using double braces ((expression)) 1. C STYLE BASED FACTORIAL CODE Source Code       echo "--------------------------"      echo -e "\t Factorial Program"      echo "--------------------------"      echo "Enter a Number: "      read n # define the variable      f=1      i=1 # define the while loop      while ((i<=n))       do          f=$((f*i))          ((i++))      done      echo "Fact is: $f" Ou

Shell Script If Statement

Image
  Shell Script If Statement If Statement In shell script, if statement is used to execute a single condition or rule If the condition is true , then true statements will be executed otherwise false statements will be executed. Syntax          if [ condition ]         then                true statements         else                false statements         fi Note The keyword fi should be used to complete the if syntax. Example (p1.sh) Source Code      echo "--------------------------------"      echo -e "\t If Statement Example"      echo "--------------------------------"      echo "Enter a number: "      read n # get a remainder of base 2 using c style code      r=$((n%2)) # find the odd or even number using remainder option      if [ $r -eq 0 ]      then          echo "This is Even Number"      else          echo "This is Odd Number"      fi Output Video Demo

Rig Linux Command

Image
  Rig Command in Linux Rig Command Rig stands for R andom I dentity G enerator This utility command is used to display the random address and identity This shows the following Random Address (Male or Female Address) Random male address   (using option -m ) Random female address (using option -f ) Random Number of Bulk Addresses using option -c [number] Syntax     rig Example (Displaying Random Addresses) Video Demo

Shell Script Input Arguments in Linux

Image
Shell Script Input Arguments In this blog, the input arguments of shell script will be discussed. Shell script supports three types of input statements. They are Static Inputs  Command Arguments (Positional Arguments) Dynamic Arguments (Runtime Arguments)  1. Static Inputs (Static Arguments) Process of assigning the values to the variables is called as static inputs This is done by using the equal operator named = Syntax     variable-name=initial-value Example     i=99 Script Example (is1.sh) Source Code echo "-----------------------" echo -e "\t Static Inputs" echo "-----------------------" name="Sachin" id=91 echo -e "Name\t: $name" echo -e "Id\t\t: $id" Note Here option -e allows the escape characters such as /t, /n, etc,... Output 2. Command Arguments (Positional Arguments) Process of assigning the values to the script at the time of execution is called as command inputs This is done by using the special parameters like

4 Linux Tips to Show File Size in Linux

Image
  4 Linux Tips to Show File Size in Linux In this blog, you will get four different tips to get a file size in linux / unix using commands. They are using ls command (ls -s) using du command (du -s) using stat command using wc command (wc -m) 1. using ls command This command is used to show the file size in KB using the option -s . Syntax                              ls -s <filename> Contents of Input file msg.txt Example:          ls -s msg.txt 2. using du command Like ls command, this command is also used to show the file size in  KB using the option -s . Syntax                         du -s <filename> Example:        du -s msg.txt 3. using stat command This command is optional for ls command This is also used to display the file size   Unlike ls and du command, this command will display the size in bytes . Syntax                         stat <filename> Example:        stat msg.txt 4. using wc command (wc -m) This command is also used to display the size of file

Figlet Command in Linux

Image
 In this blog, you will learn one of the interesting command which is called as figlet command in linux terminal. Figlet Command This command is used to display the  text / message into big fancy letters in linux terminal. This command will take only one argument which is the text message. Syntax           figlet message Example      $ figlet good morning Video Demo [தமிழில்]

SL (Steam Locomative) in Linux

Image
 SL Command in Linux / Unix SL Command SL stands for Steam Locomative This command is used to display the ASCII art of running train on your linux shell terminal This does not take any arguments. Simply run this command on terminal. Note This command is not installed on your machine by default. So you need to install it using package manager before to use it. Syntax     sl Example:        $ sl Video Demo [ தமிழில்]

3 Linux Tips to Display Message in Terminal

Image
 3 Linux Tips to Display Message in Terminal In this blog, you will learn 3 linux tips to display the message in linux terminal. This is done by using three commands. They are using echo command using printf command using read command with option -p 1. using echo command This popular command is mainly used to show the message on terminal. This will take message as an input and display the input on terminal. Example - echo "Hello World" 2. using printf command This command is optional for echo command This will take message as an input and display the input on terminal. Example - printf "Hello World\n" 3. using read command This optional command is used to print the message with help of special option -p. p stands for printing the message This will take message as an input and display the input on terminal. Example - read -p "Good Morning"

Nl command in Linux

Image
  Nl Command in Linux Nl Command This command is used to display the contents of file with line numbers . This command takes file as an input and return the contents of file with line numbers. The file is regular text file (.txt) Syntax      nl filename Example:      $nl city.txt Video Demo

Fortune Command in Linux

Image
 Fortune Command in Linux Fortune This is an one of the interesting command used in linux This command is used to display the random and possibly meaningful message It is note that, you need to install this command in your linux distribution before to use. Install Fortune on Debian Based Linux OS     sudo apt install fortune Install Fortune on RPM Based Linux OS     sudo dnf install fortune-mod Syntax     fortune Example In the example above, six times fortune command is tested in online linux terminal. In the six times, the command was displayed the random messages...

Cowsay Command in Linux

Image
  Cowsay Command This command is used to display the message in ASCII part of images of a cow shape in linux / unix terminal. Note This is not default command. So you have to install this command in your linux distribution before to use Syntax     cowsay message Example     $      cowsay Good Morning Tools Used https://repl.it (Online Linux Terminal)

Diff Linux Command

Image
  Diff Linux Command In this blog, you will learn how to use the diff command in linux / unix operating system with example. Diff Command This command is used to display the differences in files by comparing them line by line. Syntax     diff file1 file2 where,     file 1 can be text file     file 2 can be another text file. Example Video Demo

Touch Command in Linux

Image
  Touch Linux Command In this blog, you will learn how to use touch command in linux / unix operating system with example. Touch Command This command is used to create an empty text file quickly in the terminal. This command is also used to create multiple empty files This takes filename as an argument. Syntax     touch filename Example 1 - Creating Single Empty File Video Demo