Difference between revisions of "BASH/C2/String-and-File-attributes/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Title of script:''' '''String and File Attributes''' '''comparison in Bash'''
 
 
'''Author:''' Ashwini Patil
 
 
'''Keywords: video tutorial, ==, !=, string compare, file attributes.'''
 
 
 
 
 
{| border=1  
 
{| border=1  
!Time  
+
|'''Time'''
!Narration  
+
|'''Narration'''
  
 
|-
 
|-
 
| 00:01
 
| 00:01
|  Dear friends, Welcome to the spoken tutorial on '''String and File Attributes comparison in Bash.'''
+
|  Dear friends, welcome to the '''spoken tutorial''' on '''String and File Attributes comparison in Bash.'''
  
 
|-
 
|-
 
| 00:10
 
| 00:10
|   In this tutorial, we will learn
+
| In this tutorial, we will learn:
  
 
|-
 
|-
 
| 00:13
 
| 00:13
| * '''String comparison''' and '''File attributes comparison'''
+
|'''String''' comparison and '''File attributes''' comparison.
  
 
|-
 
|-
Line 29: Line 21:
 
|-
 
|-
 
| 00:22
 
| 00:22
|  For this tutorial, I am using  
+
|  For this tutorial, I am using:
  
 
|-
 
|-
 
| 00:25
 
| 00:25
| * '''Ubuntu Linux''' 12.04 Operating System and  
+
|'''Ubuntu Linux''' '''12.04''' Operating System and  
  
 
|-
 
|-
 
| 00:30
 
| 00:30
| * '''GNU BASH''' version 4.1.10
+
|'''GNU BASH''' version '''4.1.10'''
  
 
|-
 
|-
 
| 00:34
 
| 00:34
| Please note,''' GNU Bash''' version '''4''' or above, is recommended to practise this tutorial.
+
| Please note,''' GNU Bash''' version '''4''' or above is recommended to practice this tutorial.
  
 
|-
 
|-
Line 57: Line 49:
 
|-
 
|-
 
| 00:53
 
| 00:53
| To compare two '''equal strings'''.
+
| to compare two '''equal strings'''.
  
 
|-
 
|-
 
| 00:56
 
| 00:56
| 2) Second: != (not equal to) operator.
+
| 2) Second: != (not equal to) operator
  
 
|-
 
|-
 
| 00:59
 
| 00:59
| To compare two '''not equal strings'''.
+
| to compare two '''not equal strings'''.
 
+
  
 
|-
 
|-
Line 74: Line 65:
 
|-
 
|-
 
|  01:06
 
|  01:06
|  I have a simple program here, that checks the user ID.
+
|  I have a simple program here that checks the '''user ID'''.
 
+
  
 
|-
 
|-
 
| 01:11
 
| 01:11
|  Open a file in your editor and save it as '''strcompare dot sh'''
+
|  Open a file in your '''editor''' and '''save''' it as '''strcompare dot sh'''.
  
 
|-
 
|-
 
|  01:19
 
|  01:19
| Now, type the code as shown here, in your '''strcompare dot sh''' file.
+
| Now type the code as shown here, in your '''strcompare dot sh''' file.
  
 
|-
 
|-
 
| 01:26   
 
| 01:26   
|  Let me explain the code.   
+
|  Let me explain the '''code'''.   
  
 
|-
 
|-
Line 99: Line 89:
 
|-
 
|-
 
|  01:36
 
|  01:36
|  The '''if''' statement checks the output of the variable '''whoami ''' against the '''string “root”'''.
+
|  The '''if''' statement checks the '''output''' of the variable '''whoami ''' against the '''string “root”'''.
 
+
  
 
|-
 
|-
 
|  01:44
 
|  01:44
| We have used '''not-equal to''' operator here to compare the '''strings'''.
+
| We have used '''not-equal to''' operator here to compare the strings.
  
 
|-
 
|-
Line 112: Line 101:
 
|-
 
|-
 
| 01:57
 
| 01:57
| '''“You have no permission to run strcompare dot sh as non-root user.”'''
+
| '''“You have no permission to run 'strcompare dot sh' as non-root user.”'''
 
+
  
 
|-
 
|-
 
| 02:05
 
| 02:05
| Here '''$0 (dollar zero)''' is the '''zeroeth''' argument which is a filename itself.
+
| Here, '''$0 (dollar zero)''' is the zeroth '''argument''' which is a 'file-name' itself.
  
 
|-
 
|-
 
|  02:13
 
|  02:13
|  If the user is the '''root user''', it will '''echo -''' '''“Welcome root!”'''
+
|  If the user is the '''root user''', it will '''echo -''' “Welcome root!”.
  
 
|-
 
|-
 
| 02:18
 
| 02:18
 
|  Then we have the '''exit''' statement for the program.
 
|  Then we have the '''exit''' statement for the program.
 
  
 
|-
 
|-
 
| 02:23
 
| 02:23
| And here with '''fi ''' we end the '''if statement.'''
+
| And here with '''"fi" ''' we end the '''if statement.'''
 
+
  
 
|-
 
|-
 
|  02:28  
 
|  02:28  
|  Let us switch back to our slides to know more about the '''exit statement.'''
+
|  Let us switch back to our '''slides''' to know more about the '''exit statement.'''
  
 
|-
 
|-
Line 143: Line 129:
 
|-
 
|-
 
| 02:38  
 
| 02:38  
| A successful command returns a '''0 (zero)'''.
+
| A successful command returns a '''0 '''(zero).
  
 
|-
 
|-
 
| 02:42
 
| 02:42
| An erroneous command returns a '''non-zero''' '''value.'''
+
| An erroneous command returns a non-zero '''value.'''
  
 
|-
 
|-
Line 158: Line 144:
  
 
|-
 
|-
|   02:56
+
|02:56
|   Now, let's execute the program.
+
|Now, let's '''execute''' the program.
  
 
|-
 
|-
|   02:58  
+
|02:58  
| Open the terminal by pressing '''Ctrl+Alt''' and '''T''' keys simultaneously on your keyboard.
+
|Open the '''terminal window''' by pressing '''Ctrl+Alt''' and '''T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 171: Line 157:
 
|-
 
|-
 
|  03:12
 
|  03:12
| Type '''whoami'''
+
| Type: '''whoami'''.
  
 
|-
 
|-
Line 179: Line 165:
 
|-
 
|-
 
|  03:17
 
|  03:17
| This will output the name of the current user.
+
| This will '''output''' the name of the current user.
  
 
|-
 
|-
Line 187: Line 173:
 
|-
 
|-
 
| 03:25
 
| 03:25
| Type '''chmod +x strcompare dot sh'''
+
| Type: '''chmod +x strcompare dot sh'''
 
+
  
 
|-
 
|-
 
| 03:32
 
| 03:32
| Type '''dot slash strcompare dot sh'''
+
| Type: '''dot slash strcompare dot sh'''
 
+
  
 
|-
 
|-
 
| 03:37
 
| 03:37
|  The output is displayed as:
+
|  The '''output''' is displayed as:
  
 
|-
 
|-
Line 205: Line 189:
 
|-
 
|-
 
| 03:47  
 
| 03:47  
|  Now let's execute the same '''program''' as '''root user.'''
+
|  Now let's execute the same program as '''root user.'''
  
 
|-
 
|-
Line 213: Line 197:
 
|-
 
|-
 
|  03:58
 
|  03:58
|  It will prompt for an password.   
+
|  It will prompt for a password.   
  
 
|-
 
|-
Line 237: Line 221:
 
|-
 
|-
 
| 04:23   
 
| 04:23   
|  '''file1''' is the variable in which we save the path of the file.  
+
|  '''file1''' is the variable in which we '''save''' the '''path''' of the file.  
  
 
|-
 
|-
 
|  04:29
 
|  04:29
|  '''-(hyphen) f''' command checks whether the file exists or not.
+
|  '''-(hyphen) f''' command checks whether the file exists or not
  
 
|-
 
|-
 
|  04:33
 
|  04:33
| And whether it is a normal file.
+
| and whether it is a normal file.
  
 
|-
 
|-
 
|  04:37
 
|  04:37
|  If the condition is '''true,'''  it will '''echo "File exists and is a normal file" '''
+
|  If the condition is '''True,'''  it will '''echo "File exists and is a normal file" '''.
  
 
|-
 
|-
 
|  04:44
 
|  04:44
|  Else, it will '''echo "File does not exis" '''
+
|  Else, it will '''echo "File does not exist" '''.
  
 
|-
 
|-
 
|  04:48
 
|  04:48
| Switch back to the '''terminal''' let us execute our file.
+
| Switch back to the '''terminal'''. Let us '''execute''' our file.
  
 
|-
 
|-
 
|  04:53  
 
|  04:53  
|  Type '''chmod  plus x fileattrib dot sh'''
+
|  Type: '''chmod  plus x fileattrib dot sh'''
 
+
  
 
|-
 
|-
Line 271: Line 254:
 
|  05:05  
 
|  05:05  
 
|  The output is displayed as:
 
|  The output is displayed as:
 
  
 
|-
 
|-
 
|  05:07
 
|  05:07
| '''File exists and is a normal file'''.
+
| '''"File exists and is a normal file"'''.
 
+
  
 
|-
 
|-
Line 288: Line 269:
 
|-
 
|-
 
| 05:24
 
| 05:24
| Type '''gedit empty dot sh ampersand sign'''
+
| Type: '''gedit empty dot sh ampersand''' sign.
  
 
|-
 
|-
Line 296: Line 277:
 
|-
 
|-
 
| 05:35   
 
| 05:35   
| |Let us replace  '''- (hyphen) f''' attrib with '''- (hyphen) s''' attribute.
+
| |Let us replace  '''- (hyphen) f''' attribute with '''- (hyphen) s''' attribute.
  
 
|-
 
|-
Line 304: Line 285:
 
|-
 
|-
 
|  05:45  
 
|  05:45  
| Type ''' empty dot sh '''
+
| Type ''' empty dot sh '''.
  
 
|-
 
|-
|   05:47
+
|05:47
|   Now, replace the first '''echo statement''' with:
+
|Now, replace the first '''echo statement''' with:
  
 
|-
 
|-
Line 316: Line 297:
 
|-
 
|-
 
| 05:54
 
| 05:54
| And the second '''echo statement''' with:
+
| and the second '''echo statement''' with:
  
 
|-
 
|-
 
| 05:57
 
| 05:57
| ''' “File is empty”'''
+
| ''' “File is empty”'''.
 
+
  
 
|-
 
|-
Line 329: Line 309:
 
|-
 
|-
 
| 06:01  
 
| 06:01  
|  Come back to the terminal.   
+
|  Come back to the '''terminal'''.   
  
 
|-
 
|-
 
| 06:03  
 
| 06:03  
| Let me clear the prompt.
+
| Let me clear the '''prompt'''.
  
 
|-
 
|-
 
| 06:06
 
| 06:06
| Let's execute.
+
| Let's '''execute'''.
  
 
|-
 
|-
 
| 06:08  
 
| 06:08  
| Type '''dot slash fileattrib dot sh''' press '''Enter.'''
+
| Type: '''dot slash fileattrib dot sh''' press '''Enter.'''
  
 
|-
 
|-
 
| 06:13
 
| 06:13
| The output is '''File is empty.'''
+
| The output is '''"File is empty".'''
  
 
|-
 
|-
 
|  06:17
 
|  06:17
|   Now, let us see another file attribute, which will check the '''write premission''' of any file.
+
|Now, let us see another file attribute which will check the '''write permission''' of any file.
 
+
  
 
|-
 
|-
Line 363: Line 342:
 
|  06:32
 
|  06:32
 
|  Now replace the first '''echo statement''' with:
 
|  Now replace the first '''echo statement''' with:
 
  
 
|-
 
|-
 
|  06:36
 
|  06:36
| '''“User has write permission to this file”'''
+
| '''“User has write permission to this file”'''.
  
 
|-
 
|-
Line 375: Line 353:
 
|-
 
|-
 
|  06:43
 
|  06:43
| '''“User doesn't have write permission to this file”'''
+
| '''“User doesn't have write permission to this file”'''.
  
 
|-
 
|-
Line 387: Line 365:
 
|-
 
|-
 
|  06:53
 
|  06:53
| I will select a file, which is not a readable file or which does not have '''write permission'''.
+
| I will select a file which is not a readable file or which does not have '''write permission'''.
  
 
|-
 
|-
 
|  07:01
 
|  07:01
|   Let me change the '''filepath''' to
+
| Let me change the '''file path''' to
  
 
|-
 
|-
Line 403: Line 381:
 
|-
 
|-
 
| 07:12
 
| 07:12
| Let us execute our program.
+
| Let us '''execute''' our program.
  
 
|-
 
|-
Line 415: Line 393:
 
|-
 
|-
 
| 07:21   
 
| 07:21   
| '''User doesn't have write permission to this file.'''
+
| '''"User doesn't have write permission to this file".'''
  
 
|-
 
|-
 
| 07:26   
 
| 07:26   
|  Now, let us see another example based on file attributes.
+
|  Now, let us see another example based on '''file attribute'''s.
  
 
|-
 
|-
 
| 07:31   
 
| 07:31   
| In this example, we will check whether '''file1''' is newer than '''file2.'''
+
| In this example, we will check whether 'file1' is newer than 'file2'.
  
 
|-
 
|-
Line 431: Line 409:
 
|-
 
|-
 
|  07:40
 
|  07:40
| Note that our filename is '''fileattrib2 dot sh'''
+
| Note that our file-name is '''fileattrib2 dot sh'''.
  
 
|-
 
|-
 
|  07:46
 
|  07:46
| Let's go through the code.
+
| Let's go through the '''code'''.
  
 
|-
 
|-
 
|  07:48
 
|  07:48
|  Here we have two variables '''file1''' and '''file2.'''
+
|  Here we have two variables '''file1''' and '''file2'''.
  
 
|-
 
|-
Line 447: Line 425:
 
|-
 
|-
 
|  07:58  
 
|  07:58  
|  Here we check whether '''file1'''  is newer than '''file2.'''
+
|  Here, we check whether '''file1'''  is newer than '''file2.'''
  
 
|-
 
|-
 
| 08:04  
 
| 08:04  
|  If the '''condition''' is '''true''', we print '''file1 is newer than  file2.'''
+
|  If the '''condition''' is '''True''', we print '''"file1 is newer than  file2".'''
  
 
|-
 
|-
 
| 08:09  
 
| 08:09  
| '''Else''', '''file2 is newer than  file1.'''
+
| Else, '''"file2 is newer than  file1".'''
  
 
|-
 
|-
Line 467: Line 445:
 
|-
 
|-
 
| 08:21  
 
| 08:21  
| If the '''condition''' is '''true''', we print '''file1 is older than  file2.'''
+
| If the '''condition''' is '''True''', we print '''"file1 is older than  file2".'''
  
 
|-
 
|-
 
| 08:27  
 
| 08:27  
| Else, we print '''file2 is older than  file1.'''
+
| Else, we print '''"file2 is older than  file1".'''
  
 
|-
 
|-
Line 479: Line 457:
 
|-
 
|-
 
|  08:35
 
|  08:35
| First, we will edit '''empty1 dot sh''' file
+
| First, we will edit '''empty1 dot sh''' file.
  
 
|-
 
|-
Line 487: Line 465:
 
|-
 
|-
 
| 08:42  
 
| 08:42  
| Type: '''echo within doubel quotes Hiii after the double quotes greater than sign empty one dot sh'''. Press '''Enter'''.
+
| Type: '''echo within double quotes hiii after the double quotes greater than sign empty one dot sh'''. Press '''Enter'''.
  
 
|-
 
|-
 
|  08:53   
 
|  08:53   
|  Now, let us make our script executeable.
+
|  Now, let us make our '''script''' executable.
  
 
|-
 
|-
 
| 08:57   
 
| 08:57   
| Type '''chmod plus x fileattrib2 dot sh'''
+
| Type: '''chmod plus x fileattrib2 dot sh'''.
  
 
|-
 
|-
 
|  09:03
 
|  09:03
|  Now type '''dot slash fileattrib2 dot sh'''
+
|  Now, type: '''dot slash fileattrib2 dot sh'''.
 
+
  
 
|-
 
|-
Line 512: Line 489:
 
|-
 
|-
 
|  09:15
 
|  09:15
| '''file2 is older than file1'''
+
| '''file2 is older than file1'''.
  
 
|-
 
|-
Line 521: Line 498:
 
| 09:23
 
| 09:23
 
| Here also I will add an '''echo statement.'''
 
| Here also I will add an '''echo statement.'''
 
  
 
|-
 
|-
 
|  09:27
 
|  09:27
|  Type echo within double quotes '''How are you''' after the quotes (greater than sign) >'''empty2 dot sh'''.  
+
|  Type: echo within double quotes '''How are you''' after the quotes greater than sign (>)'''empty2 dot sh'''.  
  
 
|-
 
|-
 
|  09:38
 
|  09:38
| Let me clear the prompt.
+
| Let me clear the '''prompt'''.
  
 
|-
 
|-
 
| 09:41
 
| 09:41
|  Now let us execute our script again.  
+
|  Now let us '''execute''' our '''script''' again.  
  
 
|-
 
|-
 
| 09:45
 
| 09:45
| Press the up-arrow key.  
+
| Press the '''up-arrow''' key.  
  
 
|-
 
|-
 
| 09:47
 
| 09:47
 
| Go to '''dot slash fileattrib2 dot sh''' Press '''Enter.'''
 
| Go to '''dot slash fileattrib2 dot sh''' Press '''Enter.'''
 
  
 
|-
 
|-
 
|  09:53  
 
|  09:53  
|  Now the output is displayed as:
+
|  Now the '''output''' is displayed as:
  
 
|-
 
|-
 
|  09:55  
 
|  09:55  
| '''file2 is newer than file1'''
+
| '''"file2 is newer than file1"'''
  
 
|-
 
|-
 
|  09:59  
 
|  09:59  
| And '''file1 is older than file2'''
+
| And '''"file1 is older than file2"'''.
  
 
|-
 
|-
 
|  10:03
 
|  10:03
 
|  This brings us to the end of this tutorial.
 
|  This brings us to the end of this tutorial.
 
  
 
|-
 
|-
 
|  10:06
 
|  10:06
|  Let us summarise.
+
|  Let us summarize.
  
 
|-
 
|-
 
| 10:08
 
| 10:08
| In this tutorial we learnt,
+
| In this tutorial, we learned:
  
 
|-
 
|-
 
| 10:11
 
| 10:11
| '''String comparison'''
+
|'''String''' comparison'''file attributes'''
 
+
|-
+
| 10:12
+
|'''file attributes'''
+
  
 
|-
 
|-
Line 583: Line 553:
 
|-
 
|-
 
| 10:16
 
| 10:16
|'''!= (not equal to)'''
+
|'''!= (not equal to)'''* '''-f''' (hyphen f)  
 
+
|-
+
| 10:17
+
|'''-f (hyphen f) '''
+
  
 
|-
 
|-
 
| 10:18
 
| 10:18
|'''-s'''(hyphen s)
+
|'''-s''' (hyphen s)* '''-w''' (hyphen w)
 
+
|-
+
| 10:19
+
|'''-w'''(hyphen w)
+
  
 
|-
 
|-
 
| 10:21
 
| 10:21
|'''-nt'''(hyphen nt)
+
|'''-nt''' (hyphen nt) and '''-ot '''(hyphen ot) '''attribute'''s.
 
+
|-
+
| 10:22
+
|and '''-ot '''(hyphen ot)attributes.
+
  
 
|-
 
|-
 
|  10:25
 
|  10:25
|   As an assignment
+
| As an assignment-explore some more attributes.
 
+
|-
+
| 10:26
+
| Explore some more attributes.
+
  
 
|-
 
|-
 
| 10:29
 
| 10:29
| Ex: '''-r '''
+
| Ex: '''-r ''', '''-x''' and '''-o'''.
 
+
'''-x'''
+
 
+
and '''-o'''
+
  
 
|-
 
|-
 
|  10:33
 
|  10:33
|  Watch the video available at the link shown below
+
|  Watch the video available at the link shown below.
  
 
|-
 
|-
 
| 10:36
 
| 10:36
| It summarises the Spoken Tutorial project  
+
| It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
 
|  10:40
 
|  10:40
| If you do not have good bandwidth, you can download and watch it
+
| If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
 
|  10:45
 
|  10:45
|  The Spoken Tutorial Project Team
+
|  The Spoken Tutorial Project team:
  
 
|-
 
|-
 
| 10:47
 
| 10:47
| Conducts workshops using spoken tutorials  
+
| Conducts workshops using spoken tutorials.
  
 
|-
 
|-
 
|  10:51
 
|  10:51
| Gives certificates to those who pass an online test  
+
| Gives certificates to those who pass an online test.
  
 
|-
 
|-
Line 650: Line 600:
  
 
|-
 
|-
| 11:02
+
|11:02
|   Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|'''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project.
  
 
|-
 
|-
 
| 11:06
 
| 11:06
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
| It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 
|-
 
|-
 
|  11:14
 
|  11:14
| More information on this Mission is available at the link shown below .
+
| More information on this mission is available at the link shown below .
  
 
|-
 
|-
Line 667: Line 617:
 
|-
 
|-
 
| 11:25
 
| 11:25
| This is Ashwini Patil from IIT Bombay signning off.
+
| This is Ashwini Patil from IIT Bombay, signing off.
  
 
|-
 
|-

Latest revision as of 16:19, 23 March 2017

Time Narration
00:01 Dear friends, welcome to the spoken tutorial on String and File Attributes comparison in Bash.
00:10 In this tutorial, we will learn:
00:13 String comparison and File attributes comparison.
00:18 We will do this using a few examples.
00:22 For this tutorial, I am using:
00:25 Ubuntu Linux 12.04 Operating System and
00:30 GNU BASH version 4.1.10
00:34 Please note, GNU Bash version 4 or above is recommended to practice this tutorial.
00:42 Let us start with an introduction.
00:45 There are two ways to compare a string in Bash.
00:49 1) First: using == (equal to equal to) operator
00:53 to compare two equal strings.
00:56 2) Second: != (not equal to) operator
00:59 to compare two not equal strings.
01:03 Let us look at an example.
01:06 I have a simple program here that checks the user ID.
01:11 Open a file in your editor and save it as strcompare dot sh.
01:19 Now type the code as shown here, in your strcompare dot sh file.
01:26 Let me explain the code.
01:28 This is shebang line.
01:31 whoami command gives the username of the current user.
01:36 The if statement checks the output of the variable whoami against the string “root”.
01:44 We have used not-equal to operator here to compare the strings.
01:50 If the current user is not the root user, then it will echo this statement -
01:57 “You have no permission to run 'strcompare dot sh' as non-root user.”
02:05 Here, $0 (dollar zero) is the zeroth argument which is a 'file-name' itself.
02:13 If the user is the root user, it will echo - “Welcome root!”.
02:18 Then we have the exit statement for the program.
02:23 And here with "fi" we end the if statement.
02:28 Let us switch back to our slides to know more about the exit statement.
02:34 Every program returns an exit status.
02:38 A successful command returns a 0 (zero).
02:42 An erroneous command returns a non-zero value.
02:47 It can be interpreted as an error code.
02:51 We can customize return value of the exit statement .
02:56 Now, let's execute the program.
02:58 Open the terminal window by pressing Ctrl+Alt and T keys simultaneously on your keyboard.
03:08 First, let's check the current user of the system.
03:12 Type: whoami.
03:15 Press Enter.
03:17 This will output the name of the current user.
03:21 Now let's make our script executable.
03:25 Type: chmod +x strcompare dot sh
03:32 Type: dot slash strcompare dot sh
03:37 The output is displayed as:
03:39 You have no permission to run dot slash strcompare dot sh as non-root user.
03:47 Now let's execute the same program as root user.
03:52 Type: sudo dot slash strcompare dot sh
03:58 It will prompt for a password.
04:01 Give your password here.
04:04 The output is displayed as: Welcome root!.
04:08 Now, let's learn about the file attributes comparison.
04:13 I already have a working example of the code.
04:17 In this program, we will check whether a given file exists or not.
04:23 file1 is the variable in which we save the path of the file.
04:29 -(hyphen) f command checks whether the file exists or not
04:33 and whether it is a normal file.
04:37 If the condition is True, it will echo "File exists and is a normal file" .
04:44 Else, it will echo "File does not exist" .
04:48 Switch back to the terminal. Let us execute our file.
04:53 Type: chmod plus x fileattrib dot sh
05:00 Type: dot slash fileattrib dot sh
05:05 The output is displayed as:
05:07 "File exists and is a normal file".
05:11 Now we will check whether the file is empty or not.
05:16 Before executing our program, I will create an empty file named as empty dot sh.
05:24 Type: gedit empty dot sh ampersand sign.
05:31 Click on Save, close the file.
05:35 Let us replace - (hyphen) f attribute with - (hyphen) s attribute.
05:41 Replace the filename here as well.
05:45 Type empty dot sh .
05:47 Now, replace the first echo statement with:
05:51 “File exists and is not empty”
05:54 and the second echo statement with:
05:57 “File is empty”.
05:59 Click on Save.
06:01 Come back to the terminal.
06:03 Let me clear the prompt.
06:06 Let's execute.
06:08 Type: dot slash fileattrib dot sh press Enter.
06:13 The output is "File is empty".
06:17 Now, let us see another file attribute which will check the write permission of any file.
06:24 Come back to our program.
06:26 Let us replace the - (hyphen) s attribute with - (hyphen) w.
06:32 Now replace the first echo statement with:
06:36 “User has write permission to this file”.
06:40 And the second echo statement with:
06:43 “User doesn't have write permission to this file”.
06:47 Click on Save.
06:49 I will use a different file for this example.
06:53 I will select a file which is not a readable file or which does not have write permission.
07:01 Let me change the file path to
07:04 “slash etc slash mysql slash debian dot cnf”
07:10 Click on Save.
07:12 Let us execute our program.
07:15 Press the up-arrow key. Press Enter.
07:19 We see that the output is displayed as:
07:21 "User doesn't have write permission to this file".
07:26 Now, let us see another example based on file attributes.
07:31 In this example, we will check whether 'file1' is newer than 'file2'.
07:38 Let us see the program.
07:40 Note that our file-name is fileattrib2 dot sh.
07:46 Let's go through the code.
07:48 Here we have two variables file1 and file2.
07:53 The two files have already been created and are empty.
07:58 Here, we check whether file1 is newer than file2.
08:04 If the condition is True, we print "file1 is newer than file2".
08:09 Else, "file2 is newer than file1".
08:14 This is another if statement.
08:16 Here we check whether file1 is older than file2.
08:21 If the condition is True, we print "file1 is older than file2".
08:27 Else, we print "file2 is older than file1".
08:32 Come back to our terminal.
08:35 First, we will edit empty1 dot sh file.
08:39 I will just add an echo statement in it.
08:42 Type: echo within double quotes hiii after the double quotes greater than sign empty one dot sh. Press Enter.
08:53 Now, let us make our script executable.
08:57 Type: chmod plus x fileattrib2 dot sh.
09:03 Now, type: dot slash fileattrib2 dot sh.
09:09 We see the output as:
09:11 file1 is newer than file2
09:15 file2 is older than file1.
09:19 Now let's edit empty2 dot sh file.
09:23 Here also I will add an echo statement.
09:27 Type: echo within double quotes How are you after the quotes greater than sign (>)empty2 dot sh.
09:38 Let me clear the prompt.
09:41 Now let us execute our script again.
09:45 Press the up-arrow key.
09:47 Go to dot slash fileattrib2 dot sh Press Enter.
09:53 Now the output is displayed as:
09:55 "file2 is newer than file1"
09:59 And "file1 is older than file2".
10:03 This brings us to the end of this tutorial.
10:06 Let us summarize.
10:08 In this tutorial, we learned:
10:11 String comparisonfile attributes
10:14 ==(equal to equal to)
10:16 != (not equal to)* -f (hyphen f)
10:18 -s (hyphen s)* -w (hyphen w)
10:21 -nt (hyphen nt) and -ot (hyphen ot) attributes.
10:25 As an assignment-explore some more attributes.
10:29 Ex: -r , -x and -o.
10:33 Watch the video available at the link shown below.
10:36 It summarizes the Spoken Tutorial project.
10:40 If you do not have good bandwidth, you can download and watch it.
10:45 The Spoken Tutorial Project team:
10:47 Conducts workshops using spoken tutorials.
10:51 Gives certificates to those who pass an online test.
10:55 For more details, please write to contact@spoken-tutorial.org
11:02 Spoken Tutorial project is a part of the Talk to a Teacher project.
11:06 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
11:14 More information on this mission is available at the link shown below .
11:19 The script has been contributed by Fossee and spoken-tutorial team.
11:25 This is Ashwini Patil from IIT Bombay, signing off.
11:29 Thank You for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Ranjana, Sandhya.np14