Difference between revisions of "BASH/C2/Arithmetic-Comparison/English"
Nancyvarkey (Talk | contribs) |
|||
Line 13: | Line 13: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 1 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arithmetic Comparison in BASH | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arithmetic Comparison in BASH''' |
|- | |- | ||
Line 25: | Line 25: | ||
* '''-gt''' | * '''-gt''' | ||
* '''-ge''' | * '''-ge''' | ||
+ | |||
+ | commands | ||
We will do this with the help of some examples. | We will do this with the help of some examples. | ||
Line 34: | Line 36: | ||
GNU BASH version 4.1.10 | GNU BASH version 4.1.10 | ||
− | Please note, GNU bash version 4 or above is recommended | + | Please note, GNU bash version 4 or above is recommended for practice. |
|- | |- | ||
Line 50: | Line 52: | ||
|- | |- | ||
| 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;"| | + | | 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. |
− | + | ||
− | + | ||
− | Open a file in any editor of your choice and type the code as shown. | + | |
Line 68: | Line 67: | ||
| 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 now. | ||
− | This is the shebang line. | + | This is the '''shebang line'''. |
|- | |- | ||
Line 86: | Line 85: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;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 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. | ||
Line 99: | Line 98: | ||
What will happen is - | What will happen is - | ||
− | * First the '''cat''' command will read the input file | + | * First the '''cat''' command will read the file; this is the input file |
− | * Which | + | * 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 122: | Line 119: | ||
If the condition is '''true,''' we will print the message '''“File has zero words”.''' | If the condition is '''true,''' we will print the message '''“File has zero words”.''' | ||
− | '''fi''' is the end of '''if''' | + | '''fi''' is the end of first '''if''' condition. |
|- | |- | ||
Line 133: | 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''' | + | | 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. |
Here, '''-ne''' command checks whether word count is not equal to '''0.''' | Here, '''-ne''' command checks whether word count is not equal to '''0.''' | ||
− | If the condition is '''true,''' we will print a message '''“File has | + | If the condition is '''true,''' we will print a message '''“File has so-and-so words”'''. |
'''$x''' will give the word count. | '''$x''' will give the word count. | ||
− | This is the end of 2nd '''if ''' | + | This is the end of 2nd '''if '''condition. |
|- | |- | ||
Line 149: | Line 146: | ||
− | + | Let us execute our '''program.''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 180: | Line 161: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;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;"| Now, let's add a line in the file |
− | Type: | + | 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;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal type: |
+ | '''chmod +x example1.sh''' | ||
− | '''chmod +x example1.sh''' | + | Type: '''./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''' | ||
− | '''./example1.sh''' | + | Then type: '''./example1.sh''' |
− | + | |- | |
− | '''Type:''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;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;"| | + | | 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. |
− | + | Type:'''list.txt''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | Type: | + | |
− | + | ||
− | '''list.txt''' | + | |
|- | |- | ||
Line 253: | Line 227: | ||
'''<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;"| We have our shebang line here. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have our '''shebang line''' here. |
|- | |- |
Revision as of 18:30, 23 October 2013
Title of script: Arithmetic comparison in BASH shell scripting
Author: Ashwini Patil
Keywords: video tutorial, Bash shell, -eq, -ne, -gt, -ge, -lt, -le
|
|
---|---|
Display Slide 1 | Welcome to the spoken tutorial on Arithmetic Comparison in BASH |
Display Slide 2 | In this tutorial, we will learn
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 for practice. |
On the editor.
example1.sh
|
I already have a working example of arithmetic operators. Let me switch to it now.
|
Open a file in any editor of your choice and type the code as shown.
| |
In this program, we will check whether a given file is empty or not. | |
Highlight
#!/bin/bash |
Let me explain the code now.
This is the shebang line. |
Highlight
|
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.
wc will print newline, word, and byte counts for each file. -w will print the word count. What will happen is -
|
Highlight
fi |
This is the if statement
-eq command checks whether word count is equal to 0. If the condition is true, we will print the message “File has zero words”. fi is the end of first if condition. |
Highlight
if [ $x -ne 0 ]; then
fi |
Here is another if condition.
Here, -ne command checks whether word count is not equal to 0. If the condition is true, we will print a message “File has so-and-so words”.
This is the end of 2nd if condition. |
Save your program file.
| |
Type:
touch list.txt |
Now first let's create a file list.txt
So type: touch list.txt |
echo “How are you” > list.txt
|
Now, let's add a line in the file
Type: echo “How are you” After the double quotes, > (greater than signs) list.txt |
On the terminal type:
chmod +x example1.sh Type: ./example1.sh |
Now let us make our script executable. Type: chmod +x example1.sh
Then type: ./example1.sh |
Type:
list.txt |
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 example2.sh in my editor. |
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 now. | |
This program will check whether the word count is
| |
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` |
-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, -lt command checks whether word count is less than 1.
|
Highlight
if [ $x -gt 1 ] ; then echo "$y has more than one character" |
The next if statement contains nested if statements.
|
Highlight
if [ $x -ge 1 ] && [ $x -le 100 ]; then echo "Number of characters ranges between 1 and 100"
|
There are multiple if statements within this if statement.
If both the conditions are satisfied, then it prints: Number of characters ranges between 1 and 100.
|
Highlight
if [ $x -gt 100 ] ; then echo "Number of characters is above 100" fi |
Then the next if statement will be evaluated.
If the condition is stasified, we print Number of characters is above 100.
|
Highlight fi | Here we end the 2nd if statement. |
On the terminal
Type chmod +x example2.sh Type: ./example2.sh |
Let us execute the program now.
Type: chmod +x example2.sh Type: ./example2.sh |
Type:
list.txt |
Type:
list.txt |
Highlight the output | list.txt has more than one character.
will be printed on the terminal. |
Add or remove characters to the list.txt file.
| |
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 10
Summary |
Summary
In this tutorial we learnt, -lt -gt -le -ge -eq |
Display Slide 11
Assignment |
Write a program to demonstrate the use of not equal to operator.
Hint: -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. |