Difference between revisions of "BASH/C2/Arithmetic-Comparison/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
'''Title of script:''' '''Arithmetic comparison in BASH shell scripting '''
+
'''Title of script:''' '''Arithmetic comparison in BASH'''
  
 
'''Author:''' Ashwini Patil
 
'''Author:''' Ashwini Patil
Line 19: Line 19:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn
  
* '''-eq'''
+
* '''equal to'''
* '''-ne'''
+
* '''not equal to'''
* '''-lt'''
+
* '''less than'''
* '''-le'''
+
* '''less than equal to'''
* '''-gt'''
+
* '''greater than'''
* '''-ge'''
+
* '''greater than equal to''' commands
 
+
commands
+
  
 
We will do this with the help of some examples.
 
We will do this with the help of some examples.
Line 32: Line 30:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 3
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 3
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using Ubuntu Linux 12.04 Operating System and  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
 
+
*'''Ubuntu Linux 12.04''' Operating System and  
GNU BASH version 4.1.10
+
*'''GNU BASH''' version '''4.1.10'''
  
Please note, GNU bash version 4 or above is recommended for practice.
+
Please note, '''GNU Bash''' version 4 or above, is recommended to practice this tutorial.
  
 
|-
 
|-
Line 45: Line 43:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I already have a working example of arithmetic operators. Let me switch to it now.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I already have a working example of arithmetic operators. Let me switch to it.
  
  
I have named my file as '''example1.sh'''
+
I have named my file '''example1.sh'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open a file in any editor of your choice and type the code as shown.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Open a file in any editor of your choice and type the code as shown.
  
  
Line 65: Line 63:
  
 
'''<nowiki>#!/bin/bash</nowiki>'''
 
'''<nowiki>#!/bin/bash</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code.
  
 
This is the '''shebang line'''.
 
This is the '''shebang line'''.
Line 76: Line 74:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First of all, “'''Enter filename” '''will be printed on the '''console.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First of all, “'''Enter filename” '''will be printed on the '''console.'''
  
'''read '''command reads''' one line''' of data from''' '''the '''standard input.'''
+
'''read '''command reads one line of data from the '''standard input.'''
  
 
|-
 
|-
Line 87: Line 85:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This command is enclosed within '''backticks.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This command is enclosed within '''backticks.'''
  
'''Backtick '''has a very special meaning'''. '''Everything you type between '''backticks '''is evaluated.
+
'''Backtick '''has a very special meaning. Everything you type between '''backticks '''is evaluated.
  
  
 
'''cat''' command will display the content of the file.
 
'''cat''' command will display the content of the file.
  
'''wc''' will print newline, word, and byte counts for each file.
+
'''wc''' will print newline, word, and byte counts, for each file.
  
'''-w''' will print the word count.
+
'''- (hyphen) w''' will print the word count.
  
 
What will happen is -  
 
What will happen is -  
  
* First the '''cat''' command will read the file; this is the input file
+
* First the '''cat''' command will read the file. This is the input file.
* Which is then piped or sent to the '''wc''' command
+
* Which is then piped or sent to the '''wc''' command.
* So, this statement counts the words in a given file
+
* So, this statement counts the words in a given file.
* The output is stored in variable '''x'''
+
* The output is stored in variable '''x.'''
 +
 
 +
 
  
 
|-
 
|-
Line 115: Line 115:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''if statement'''  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''if statement'''  
  
'''-eq''' command checks whether word count is equal to '''0.'''
+
'''- (hyphen) eq''' command checks whether word count is equal to zero.
  
If the condition is '''true,''' we will print the message '''“File has zero words”.'''
+
If the condition is '''true,''' we will print a message '''“File has zero words”.'''
  
 
'''fi''' is the end of first '''if''' condition.
 
'''fi''' is the end of first '''if''' condition.
Line 130: Line 130:
  
 
'''fi'''
 
'''fi'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is another '''if''' condition.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is another '''if''' statement.
  
Here, '''-ne''' command checks whether word count is not equal to '''0.'''
+
Here, '''- (hyphen) ne''' command checks whether word count is not equal to zero.
  
If the condition is '''true,''' we will print a message '''“File has so-and-so words”'''.
+
If the condition is '''true,''' we will print '''“File has so-and-so words”'''.
  
  
'''$x''' will give the word count.
+
'''$ (dollar) x''' will give the word count.
  
 
This is the end of 2nd '''if '''condition.
 
This is the end of 2nd '''if '''condition.
Line 146: Line 146:
  
  
Let us execute our '''program.'''
+
Now, let us execute our '''program.'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open the '''terminal'''
 +
 
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal'''.
  
 
|-
 
|-
Line 154: Line 159:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now first let's create a file '''list.txt'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now first let's create a file '''list.txt'''
  
So type: '''touch list.txt'''
+
Type: '''touch list.txt'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo “How are you” > list.txt'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo “How are you” > list.txt'''
  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, let's add a line in the file.
  
 +
Type:
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, let's add a line in the file
+
'''echo double quotes How are you after the double quotes greater than sign list.txt'''
 
+
Type: '''echo “How are you” '''
+
 
+
After the double quotes, '''> (greater than signs) list.txt'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal type:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type:'''
  
'''chmod +x example1.sh'''
 
  
Type: '''./example1.sh'''
+
'''chmod +x example1.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us make our script executable.  Type: '''chmod +x example1.sh'''  
+
  
Then type: '''./example1.sh'''
+
'''./example1.sh'''
  
|-
+
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type:'''
+
'''Type:'''
  
 
'''list.txt'''
 
'''list.txt'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter filename '''is displayed.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's make our script executable.
  
Type:'''list.txt'''
+
Type:
 +
 
 +
'''chmod plus x example1 dot sh'''
 +
 
 +
Now type '''dot slash example1.sh'''
 +
 
 +
 
 +
'''Enter filename '''is displayed.
 +
 
 +
Type:
 +
 
 +
'''list.txt'''
  
 
|-
 
|-
Line 199: Line 211:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch '''to example2.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch '''to example2.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me switch to another file '''example2.sh '''in my editor.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me switch to another file.  This is '''example2.sh '''.
  
 
|-
 
|-
Line 211: Line 223:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This program will check whether the word count is  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This program will check whether the word count is  
 
+
* greater or less than one
* greater or less than '''1.'''
+
* Between one and hundred
* Between 1 and 100
+
* Or above hundred
* Or above 100
+
 
+
 
+
  
 
|-
 
|-
Line 239: Line 248:
  
 
'''wc -c`'''
 
'''wc -c`'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''-c''' command is used to print the byte counts.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, '''- (hyphen) c''' command is used to print the byte counts.
  
 
|-
 
|-
Line 251: Line 260:
  
 
'''fi'''
 
'''fi'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the '''if statement''', '''-lt''' command checks whether word count is less than '''1'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the '''if statement''', '''- (hyphen) lt''' command checks whether word count is less than one.
  
  
Line 257: Line 266:
  
  
'''fi''' ends the first '''if statement.'''
+
'''fi''' ends the '''if statement.'''
  
 
|-
 
|-
Line 267: Line 276:
  
 
'''echo "$y has more than one character"'''
 
'''echo "$y has more than one character"'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The next '''if statement '''contains nested '''if statements.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The next '''if statement '''contains a nested '''if statement.'''
  
  
First the '''-gt''' command checks whether word count is greater than '''1.'''
+
First the '''- (hyphen) gt''' command checks whether word count is greater than one.
  
  
If yes, then this '''echo statement '''will get executed.
+
If yes, then this '''echo statement '''will be executed.
  
 
|-
 
|-
Line 284: Line 293:
  
 
'''fi '''
 
'''fi '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are multiple '''if statements '''within this '''if statement.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are multiple conditions within this '''if statement.'''
  
  
Here in this '''if statement -'''
+
Here, in this '''if'''
  
* '''-ge '''command checks whether word count is greater than equal to '''1 a'''nd
+
* '''- (hyphen) ge '''command checks whether word count is greater than or equal to one and
* '''-le '''command checks whether word count is less than equal to '''100.'''
+
* '''- (hyphen) le '''command checks whether word count is less than or equal to hundred.
  
 
If both the conditions are satisfied, then it prints:
 
If both the conditions are satisfied, then it prints:
Line 296: Line 305:
 
'''Number of characters ranges between 1 and 100.'''
 
'''Number of characters ranges between 1 and 100.'''
  
 +
Please note that both conditions should be true to satisfy the entire '''if condition'''.
  
Please note that both conditions should be '''true''' to satisfy the entire'' '''''if condition'''.
 
  
 
+
This is because we have included ampersand in-between both the conditions.  
This is because we have included '''&& '''in between both conditions.  
+
  
  
Line 318: Line 326:
  
  
'''-gt '''command checks whether word count is greater than 100.
+
'''- (hyphen) gt '''command checks whether word count is greater than hundred.
  
If the condition is stasified, we print '''Number of characters is above 100.'''
+
If the condition is stasified, we print '''Number of characters is above hundred.'''
  
  
'''fi '''is the end of this '''if statement.'''
+
'''fi '''is the end of '''if statement.'''
  
 
|-
 
|-
Line 339: Line 347:
  
 
'''./example2.sh'''
 
'''./example2.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program now.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our '''terminal'''.  Let us execute the program.
  
Type:
 
  
'''chmod +x example2.sh'''
+
Type:
 +
'''chmod plus x example2 dot sh'''
  
 
Type:
 
Type:
 
+
'''dot slash example2 dot sh'''
'''./example2.sh'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type: '''list.txt'''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''list.txt '''
'''list.txt'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:
+
 
+
'''list.txt'''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''list.txt''' has more than one character.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as '''''list.txt''' has more than one character.''
  
  
'''Number of characters ranges between 1 and 100 '''
+
'''''Number of characters ranges between one and hundred '''''
 
+
will be printed on the '''terminal.'''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Add or remove characters to the '''list.txt '''file.''' '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, add or remove characters to the '''list.txt '''file.
  
  
Then observe which '''if statements''' get executed.
+
Then observe which '''if statement''' gets executed.
  
 
|-
 
|-
Line 386: Line 387:
  
 
In this tutorial we learnt,
 
In this tutorial we learnt,
 
+
* '''equal to'''
'''-lt'''
+
* '''not equal to'''
 
+
* '''less than'''
'''-gt'''
+
* '''less than equal to'''
 
+
* '''greater than'''
'''-le'''
+
* '''greater than equal to''' commands
 
+
'''-ge'''
+
 
+
'''-eq'''
+
  
 
|-
 
|-
Line 401: Line 398:
  
 
'''Assignment'''
 
'''Assignment'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Write a program to demonstrate the use of '''not equal to''' operator.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment, write a program to demonstrate the use of '''not equal to''' operator.
  
Hint: '''-ne'''
+
Hint: '''- (hyphen) ne'''
  
 
|-
 
|-

Latest revision as of 14:44, 12 November 2013

Title of script: Arithmetic comparison in BASH

Author: Ashwini Patil

Keywords: video tutorial, Bash shell, -eq, -ne, -gt, -ge, -lt, -le


Visual Cue
Narration
Display Slide 1 Welcome to the spoken tutorial on Arithmetic Comparison in BASH
Display Slide 2 In this tutorial, we will learn
  • equal to
  • not equal to
  • less than
  • less than equal to
  • greater than
  • greater than equal to commands

We will do this with the help of some examples.

Display Slide 3 For this tutorial I am using
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.1.10

Please note, GNU Bash version 4 or above, is recommended to practice this tutorial.

On the editor.

example1.sh


I already have a working example of arithmetic operators. Let me switch to it.


I have named my file example1.sh

Open a file in any editor of your choice and type the code as shown.


You must be familiar how to do so, by now.

In this program, we will check whether a given file is empty or not.
Highlight

#!/bin/bash

Let me explain the code.

This is the shebang line.

Highlight


read -p "Enter filename: " y

First of all, “Enter filename” will be printed on the console.

read command reads one line of data from the standard input.

Highlight

x=`cat $y | wc -w`


This command is enclosed within backticks.

Backtick has a very special meaning. Everything you type between backticks is evaluated.


cat command will display the content of the file.

wc will print newline, word, and byte counts, for each file.

- (hyphen) w will print the word count.

What will happen is -

  • First the cat command will read the file. This is the input file.
  • Which is then piped or sent to the wc command.
  • So, this statement counts the words in a given file.
  • The output is stored in variable x.


Highlight


if [ $x -eq 0 ]; then


echo "$y has zero words"

fi

This is the if statement

- (hyphen) eq command checks whether word count is equal to zero.

If the condition is true, we will print a message “File has zero words”.

fi is the end of first if condition.

Highlight

if [ $x -ne 0 ]; then


echo "$y has $x words"

fi

Here is another if statement.

Here, - (hyphen) ne command checks whether word count is not equal to zero.

If the condition is true, we will print “File has so-and-so words”.


$ (dollar) x will give the word count.

This is the end of 2nd if condition.

Save your program file.


Now, let us execute our program.

Open the terminal Open the terminal.
Type:

touch list.txt

Now first let's create a file list.txt

Type: touch list.txt

echo “How are you” > list.txt Now, let's add a line in the file.

Type:

echo double quotes How are you after the double quotes greater than sign list.txt

Type:


chmod +x example1.sh

./example1.sh


Type:

list.txt

Let's make our script executable.

Type:

chmod plus x example1 dot sh

Now type dot slash example1.sh


Enter filename is displayed.

Type:

list.txt

Highlight

The Output

The output is displayed as:

list.txt has 3 words

Now let's learn about another set of operators.
Switch to example2.sh Let me switch to another file. This is example2.sh .
Please open a file in your editor and name it as example2.sh
Now type the code as shown here in your example2.sh file.
Let me explain the code.
This program will check whether the word count is
  • greater or less than one
  • Between one and hundred
  • Or above hundred
Highlight

#!/bin/bash

We have our shebang line here.
Highlight

read -p "Enter the filename: " y

read statement takes input as filename from the user.
Highlight

wc -c`

Here, - (hyphen) c command is used to print the byte counts.
Highlight

if [ $x -lt 1 ] ;

then

echo "No characters present in $y"

fi

In the if statement, - (hyphen) lt command checks whether word count is less than one.


If the condition is true, then we print “No characters present in file”.


fi ends the if statement.

Highlight

if [ $x -gt 1 ] ;

then

echo "$y has more than one character"

The next if statement contains a nested if statement.


First the - (hyphen) gt command checks whether word count is greater than one.


If yes, then this echo statement will be executed.

Highlight

if [ $x -ge 1 ] && [ $x -le 100 ]; then

echo "Number of characters ranges between 1 and 100"


fi

There are multiple conditions within this if statement.


Here, in this if

  • - (hyphen) ge command checks whether word count is greater than or equal to one and
  • - (hyphen) le command checks whether word count is less than or equal to hundred.

If both the conditions are satisfied, then it prints:

Number of characters ranges between 1 and 100.

Please note that both conditions should be true to satisfy the entire if condition.


This is because we have included ampersand in-between both the conditions.


fi is the end of this if statement.

Highlight

if [ $x -gt 100 ] ;

then

echo "Number of characters is above 100"

fi

Then the next if statement will be evaluated.


- (hyphen) gt command checks whether word count is greater than hundred.

If the condition is stasified, we print Number of characters is above hundred.


fi is the end of if statement.

Highlight fi Here we end the 2nd if statement.
On the terminal

Type

chmod +x example2.sh

Type:

./example2.sh

Now come back to our terminal. Let us execute the program.


Type: chmod plus x example2 dot sh

Type: dot slash example2 dot sh

Type: list.txt Type list.txt
Highlight the output The output is displayed as list.txt has more than one character.


Number of characters ranges between one and hundred

Now, add or remove characters to the list.txt file.


Then observe which if statement gets executed.

This brings us to the end of this tutorial.

Let us summarize.

Display Slide 10

Summary

Summary

In this tutorial we learnt,

  • equal to
  • not equal to
  • less than
  • less than equal to
  • greater than
  • greater than equal to commands
Display Slide 11

Assignment

As an assignment, write a program to demonstrate the use of not equal to operator.

Hint: - (hyphen) ne

Display Slide 12

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide 13

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 14

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

The script has been contributed by FOSSEE and spoken-tutorial team.

This is Ashwini Patil from IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey