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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by 2 users not shown)
Line 15: Line 15:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Dear friends, Welcome to the spoken tutorial on  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Dear friends, Welcome to the spoken tutorial on  
  
'''String and File Attributes''' '''comparison in Bash.'''
+
'''String and File Attributes comparison in Bash.'''
  
 
|-
 
|-
Line 21: Line 21:
 
| 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
  
* '''String comparison'''
+
* '''String comparison''' and
* '''File attributes comparison'''<br/> with few examples
+
* '''File attributes comparison'''
 
+
  
 +
We will do this using a few examples.
  
 
|-
 
|-
 
| 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''' '''
+
| 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  
* '''Ubuntu Linux''' 12.04 '''Operating System '''and  
+
* '''GNU BASH''' version 4.1.10
* '''GNU BASH''' version 4.2.24<br/>
+
  
  
Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial.
+
Please note,''' GNU Bash''' version 4 or above, is recommended to practise this tutorial.
  
 
|-
 
|-
Line 40: Line 39:
  
  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction.  There are two ways to compare a '''string''' in '''Bash'''.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are two ways to compare a '''string''' in '''Bash'''.
+
1) First: using == (equal to equal to) operator
  
1) Using == operator
+
To compare two '''equal strings'''.
  
To compare two equal strings.
+
2) Second: using != (not equal to) operator
  
2) Using != operator
+
To compare two '''not equal strings'''.
 
+
To compare two not equal strings.
+
  
  
Line 56: Line 54:
 
|-
 
|-
 
| 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;"| I have a simple program here that checks the user ID.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have a simple program here, that checks the user ID.
  
  
Open a file in your editor and save it as '''strcompare.sh'''
+
Open a file in your editor and save it as '''strcompare dot sh'''
  
  
Now, type the code as shown here, in your '''strcompare.sh '''file
+
Now, type the code as shown here, in your '''strcompare dot sh '''file
  
 
|-
 
|-
Line 68: Line 66:
  
 
'''<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;"| This is shebang line.
+
| 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 '''shebang line.'''
  
 
|-
 
|-
Line 78: Line 76:
  
 
'''<nowiki>if [ "$(whoami)" != 'root' ]; then </nowiki>'''
 
'''<nowiki>if [ "$(whoami)" != 'root' ]; then </nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''if '''statement checks the output of the variable '''whoami '''against the '''string “root”'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''if '''statement checks the output of the variable '''whoami ''' against the '''string “root”'''.
  
  
We have used '''not-equal to '''operator here to compare strings.
+
We have used '''not-equal to '''operator here to compare '''strings'''.
  
 
|-
 
|-
Line 87: Line 85:
  
 
'''echo "You have no permission to run $0 as non-root user." '''
 
'''echo "You have no permission to run $0 as non-root user." '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the current user is not the '''root user,''' then
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the current user is not the '''root user,''' then it will '''echo''' this statement -
 
+
it will '''echo''' this statement -
+
  
'''“You have no permission to run strcompare.sh as non-root user.”'''
+
'''“You have no permission to run strcompare dot sh as non-root user.”'''
  
  
Here '''$0''' is the '''zero'th '''argument which is a '''filename '''itself.
+
Here '''$0 (dollar zero)''' is the '''zeroeth '''argument which is a '''filename '''itself.
  
 
|-
 
|-
Line 115: Line 111:
 
And here with '''fi, '''we end the '''if statement.'''
 
And here with '''fi, '''we end the '''if statement.'''
  
|-
 
| 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;"| Save your file and exit the editor.
 
  
 
|-
 
|-
Line 131: Line 124:
 
An erroneous command returns a '''non-zero''' '''value.'''
 
An erroneous command returns a '''non-zero''' '''value.'''
  
It can be interpreted as an error code.
+
It can be interpreted as an '''error code'''.
  
We can customize the return value of the '''exit statement '''by stating the '''return value'''.
+
We can customize the return value of the '''exit statement '''.
  
 
|-
 
|-
Line 146: Line 139:
  
  
| 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 by pressing
+
| 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 by pressing '''Ctrl+Alt+T '''keys simultaneously on your keyboard.
 
+
'''Ctrl+Alt+T '''keys simultaneously on your keyboard.
+
  
 
|-
 
|-
Line 154: Line 145:
  
 
'''whoami'''
 
'''whoami'''
| 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 check the current user of the system.
+
| 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 check the current user of the system.
  
 
Type '''whoami '''and press '''Enter.'''
 
Type '''whoami '''and press '''Enter.'''
  
  
This will output the name of the current user'''.'''
+
This will output the name of the current user.
  
 
|-
 
|-
Line 169: Line 160:
 
Type '''chmod +x strcompare.sh'''
 
Type '''chmod +x strcompare.sh'''
  
Press '''Enter'''
 
  
 
|-
 
|-
Line 175: Line 165:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type '''./strcompare.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type '''./strcompare.sh'''
  
Press '''Enter'''
 
  
 
|-
 
|-
Line 183: Line 172:
 
| 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:
 
| 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:
  
'''You have no permission to run ./strcompare.sh as non-root user.'''
+
'''You have no permission to run dot slash strcompare dot sh as non-root user.'''
  
 
|-
 
|-
Line 193: Line 182:
 
| 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 execute the same '''program''' as '''root user.'''
 
| 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 execute the same '''program''' as '''root user.'''
  
Type: '''sudo ./strcompare.sh '''and press '''Enter'''
+
Type: '''sudo dot slash strcompare.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:1pt solid #000000;padding:0.097cm;"| It will prompt for a password.  Give your password here.
  
 
|-
 
|-
Line 199: Line 192:
  
 
'''The output'''
 
'''The output'''
| 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:
+
| 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: '''Welcome root!. '''
 
+
'''Welcome root!. '''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| FILE ATTRIBUTES
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| FILE ATTRIBUTES
| 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 learn about the '''file attributes '''comparison.
+
| 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 learn about the '''file attributes '''comparison.
  
 
|-
 
|-
Line 213: Line 204:
 
|-
 
|-
 
| 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;"| In this program we will check whether a given file exists or not.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this program, we will check whether a given file exists or not.
  
 
|-
 
|-
Line 230: Line 221:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then '''-f''' command checks whether the file exists or not.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then '''-(hyphen) f''' command checks whether the file exists or not.
  
 
And whether it is a normal file.
 
And whether it is a normal file.
Line 249: Line 240:
  
 
'''fi'''
 
'''fi'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the condition is '''false,''' it will '''echo "File does not exist" '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Else, it will '''echo "File does not exist" '''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch to the '''terminal''' and let us execute our file.
 
+
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch to the '''terminal''' and execute the '''program'''.
+
  
 
|-
 
|-
| 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: '''chmod +x fileattrib.sh'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''chmod  plus x fileattrib dot sh'''
  
'''chmod +x fileattrib.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 first make the file executable.
 
 
Type '''chmod +x fileattrib.sh'''
 
 
and press '''Enter.'''
 
  
 
|-
 
|-
| 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 '''./fileattrib.sh'''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type: '''dot slash fileattrib dot sh'''
'''./fileattrib.sh'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''./fileattrib.sh'''
+
 
+
and press '''Enter.'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight output
 
+
| 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:
'''Output'''
+
| 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:
+
  
 
'''File exists and is a normal file'''
 
'''File exists and is a normal file'''
Line 292: Line 269:
  
 
'''gedit empty.sh &'''
 
'''gedit empty.sh &'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Before executing our program, I will create an empty file.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Before executing our program, I will create an empty file named as '''empty dot sh'''.
  
Type '''gedit empty.sh &'''
+
Type '''gedit empty dot sh ampersand sign'''
  
and save this file using Ctrl+S.
+
Click on '''Save''' and close the file.
  
 
|-
 
|-
Line 312: Line 289:
  
 
'''then'''
 
'''then'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Let us replace the '''- (hyphen) f''' attrib with '''- (hyphen) s''' attribute.
 
+
Let us replace the '''-f''' attribute with '''-s'''.
+
  
 
|-
 
|-
Line 324: Line 299:
  
 
'''file1= “/home/ttt/empty.sh”'''
 
'''file1= “/home/ttt/empty.sh”'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And also replace the filename with''' empty.sh .'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Replace the filename here as well.  Type ''' empty.sh '''
  
 
|-
 
|-
Line 342: Line 317:
  
 
'''echo “File is empty”'''
 
'''echo “File is empty”'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the first echo statement with:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, replace the first''' echo statement''' with:
  
 
'''echo “File exists and is not empty”'''
 
'''echo “File exists and is not empty”'''
  
And the second echo statement with:
+
And the second '''echo statement''' with:
  
 
'''echo “File is empty”'''
 
'''echo “File is empty”'''
 
 
  
  
Line 361: Line 334:
  
 
'''./fileattrib.sh'''
 
'''./fileattrib.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now on the terminal, let's execute the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the terminal.  Let me clear the prompt.
  
Type '''./fileattrib.sh'''
+
Let's execute.
 +
 
 +
Type '''dot slash fileattrib dot sh'''
  
 
and press '''Enter.'''
 
and press '''Enter.'''
Line 375: Line 350:
 
|-
 
|-
 
| 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;"| Now let us see another file attribute which will check the '''write premission''' of any file.
+
| 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 see another file attribute, which will check the '''write premission''' of any file.
 
+
 
+
  
  
Line 392: Line 365:
  
 
'''then'''
 
'''then'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In our program, let us replace the '''-s''' command with '''-w''' command.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our program. Let us replace the '''- (hyphen) s''' attribute with '''- (hyphen) w'''.
  
 
|-
 
|-
Line 411: Line 384:
  
 
'''echo “User don't have write permission to this file”'''
 
'''echo “User don't have write permission to this file”'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the first echo statement with:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the first '''echo statement''' with:
  
'''echo “User has write permission to this file”'''
+
'''“User has write permission to this file”'''
  
And second echo statement with:
+
And second''' echo statement''' with:
  
'''echo “User doesn't have write permission to this file”'''
+
'''“User doesn't have write permission to this file”'''
 +
 
 +
Click on '''Save'''.
  
 
|-
 
|-
Line 423: Line 398:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will use a different file for this example.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will use a different file for this example.
  
I will select a file which is don't have '''read''' or '''write permission'''.
+
I will select a file, which is not a readable file or which does not have '''write permission'''.
  
 
|-
 
|-
Line 431: Line 406:
  
 
'''Save the file'''
 
'''Save the file'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me change the filepath to
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me change the '''filepath''' to
  
'''“/etc/mysql/debian.cnf”'''
+
'''“slash etc slash mysql slash debian dot cnf”'''
  
and save the changes.
+
Click on '''Save'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch to the terminal'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch to the terminal'''
| 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 our program now.
+
| 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 our program.
  
|-
+
Press the '''up-arrow key'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal'''
+
 
+
'''Type:'''
+
 
+
'''./fileattrib.sh'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the terminal and type:
+
 
+
'''./fileattrib.sh'''
+
  
 
|-
 
|-
Line 455: Line 422:
  
 
'''Output'''
 
'''Output'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see that the output as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see that the output is displayed as:
  
 
'''User doesn't have write permission to this file.'''
 
'''User doesn't have write permission to this file.'''
Line 461: Line 428:
 
|-
 
|-
 
| 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 us see another example based on file attributes.
+
| 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 see another example based on file attributes.
  
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 476: Line 443:
  
  
I have already typed the code. So, let's go through it now.
+
Let's go through the code.
  
 
|-
 
|-
Line 487: Line 454:
  
 
The two files have already been created and are empty.
 
The two files have already been created and are empty.
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''if'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we check whether '''file1''' is newer than '''file2.'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the echo statements as per the narration
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the '''condition''' is '''true''', we print '''file1 is newer than  file2.'''
 +
 +
Else, '''file2 is newer than  file1.'''
 +
 +
|-
 +
| 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;"| Here is another '''if statement.'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''if'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we check whether '''file1''' is older than '''file2.'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the echo statements as per the narration
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the '''condition''' is '''true''', we print '''file1 is older than  file2.'''
 +
 +
Else, we print '''file2 is older than  file1.'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|On the '''terminal'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our '''terminal.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open''' empty1.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open''' empty1.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First, we will edit '''empty1.sh'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First, we will edit '''empty1.sh''' file
  
 
|-
 
|-
Line 500: Line 495:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will just add an '''echo statement''' in it.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will just add an '''echo statement''' in it.
  
Type: '''echo “Hiii”'''
+
Type: '''echo within doubel quotes Hiii after the double quotes greater than sign empty one dot sh'''
 +
 
 +
Press '''Enter'''.
  
 
|-
 
|-
Line 508: Line 505:
  
 
'''chmod +x fileattrib2.sh'''
 
'''chmod +x fileattrib2.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.
+
| 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 executeable.
  
Switch to the temrinal and type -
+
Type -
  
'''chmod +x fileattrib2.sh'''
+
'''chmod plus x fileattrib2 dot sh'''
  
 
|-
 
|-
Line 520: Line 517:
  
 
'''./fileattrib2.sh'''
 
'''./fileattrib2.sh'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type '''./fileattrib2.sh'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type '''dot slash fileattrib2 dot sh'''
 
+
  
  
Line 546: Line 542:
  
 
Here also I will add an '''echo statement.'''
 
Here also I will add an '''echo statement.'''
 +
 +
  
 
|-
 
|-
| 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 echo within double quotes ''' "How are you" ''' after the quotes '''empty2.sh'''.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type echo within double quotes ''' "How are you" ''' after the quotes greater than sign >'''empty2.sh'''.
 +
Let me clear the prompt.
 +
 
 +
 
 +
 
 +
|-
 +
| 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;"| Now let us execute our script again. Press the up-arrow key. Go to '''./fileattrib2.sh'''
 +
Press Enter
  
'''./fileattrib2.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute come back to our terminal and type '''./fileattrib2.sh'''
 
  
 
|-
 
|-
Line 557: Line 562:
  
 
'''Output'''
 
'''Output'''
| 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:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now the output is displayed as:
  
 
'''file2 is newer than file1'''
 
'''file2 is newer than file1'''
  
 
'''file1 is older than file2'''
 
'''file1 is older than file2'''
 +
 +
|-
 +
| 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 brings us to the end of this tutorial.
 +
  
 
|-
 
|-

Latest revision as of 12:18, 13 November 2013

Title of script: String and File Attributes comparison in Bash

Author: Ashwini Patil

Keywords: video tutorial, ==, !=, string compare, file attributes.


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on

String and File Attributes comparison in Bash.

Display Slide 2 In this tutorial, we will learn
  • String comparison and
  • File attributes comparison

We will do this using a few 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 practise this tutorial.

Display Slide 4


Let us start with an introduction. There are two ways to compare a string in Bash.

1) First: using == (equal to equal to) operator

To compare two equal strings.

2) Second: using != (not equal to) operator

To compare two not equal strings.


Let us look at an example.

I have a simple program here, that checks the user ID.


Open a file in your editor and save it as strcompare dot sh


Now, type the code as shown here, in your strcompare dot sh file

Highlight

#!/bin/bash

Let me explain the code. This is shebang line.
whoami whoami command gives the username of the current user.
Highlight

if [ "$(whoami)" != 'root' ]; then

The if statement checks the output of the variable whoami against the string “root”.


We have used not-equal to operator here to compare strings.

Highlight

echo "You have no permission to run $0 as non-root user."

If the current user is not the root user, then it will echo this statement -

“You have no permission to run strcompare dot sh as non-root user.”


Here $0 (dollar zero) is the zeroeth argument which is a filename itself.

Highlight

else

echo "Welcome root!. "

If the user is the root user, it will echo -

“Welcome root!”

Highlight

exit 0;

Then we have the exit statement for the program.


And here with fi, we end the if statement.


Let us switch back to our slides to know more about exit statement.
Display Slide Every program returns an exit status.

A successful command returns a 0 (zero).

An erroneous command returns a non-zero value.

It can be interpreted as an error code.

We can customize the return value of the exit statement .

Now, let's execute the program.
Press Ctrl+Alt+T

on the Terminal:


Open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard.
Type:

whoami

Now first, let's check the current user of the system.

Type whoami and press Enter.


This will output the name of the current user.

Type:

chmod +x strcompare.sh

Now let's make our file executable.

Type chmod +x strcompare.sh


Type ./strcompare.sh Then type ./strcompare.sh


Highlight

The output

The output is displayed as:

You have no permission to run dot slash strcompare dot sh as non-root user.

On the terminal

Type: sudo su

Type: ./strcompare.sh

Now let's execute the same program as root user.

Type: sudo dot slash strcompare.sh

It will prompt for a password. Give your password here.
Highlight

The output

The output is displayed as: Welcome root!.
FILE ATTRIBUTES Now, let's learn about the file attributes comparison.
I already have a working example of the code.
In this program, we will check whether a given file exists or not.
Highlight

file1=“/home/ttt/fileattrib.sh”

file1 is the variable in which we save the path of the file.
Highlight

if [ -f $file1 ];

then


Then -(hyphen) f command checks whether the file exists or not.

And whether it is a normal file.

Highlight

echo "File exists and is a normal file"


If the condition is true, it will echo "File exists and is a normal file"
else

echo "File does not exist"

fi

Else, it will echo "File does not exist"
On the terminal Let us switch to the terminal and let us execute our file.
Type: chmod +x fileattrib.sh Type chmod plus x fileattrib dot sh


Type ./fileattrib.sh Type: dot slash fileattrib dot sh
Highlight output The output is displayed as:

File exists and is a normal file

Now we will check whether the file is empty or not.
Type on the terminal

gedit empty.sh &

Before executing our program, I will create an empty file named as empty dot sh.

Type gedit empty dot sh ampersand sign

Click on Save and close the file.

Replace

if [ -f $file1 ];

then


with


if [ -s $file1 ];

then

Let us replace the - (hyphen) f attrib with - (hyphen) s attribute.
Replace

file1= “/home/ttt/fileattrib.sh”

with

file1= “/home/ttt/empty.sh”

Replace the filename here as well. Type empty.sh
Replace

echo "File exists and is a normal

with

echo "File exists and is not empty"

AND

"File does not exist"

with

echo “File is empty”

Now, replace the first echo statement with:

echo “File exists and is not empty”

And the second echo statement with:

echo “File is empty”


On the terminal Click on Save.
Type:

./fileattrib.sh

Come back to the terminal. Let me clear the prompt.

Let's execute.

Type dot slash fileattrib dot sh

and press Enter.

Highlight

Output

The output is File is empty.
Now, let us see another file attribute, which will check the write premission of any file.


Replace

if [ -s $file1 ];

then

with

if [ -w $file1 ];

then

Come back to our program. Let us replace the - (hyphen) s attribute with - (hyphen) w.
Replace


file exists and is not empty"

with

echo “User has write permission to this file”

AND

echo "file is empty"

with

echo “User don't have write permission to this file”

Now replace the first echo statement with:

“User has write permission to this file”

And second echo statement with:

“User doesn't have write permission to this file”

Click on Save.

I will use a different file for this example.

I will select a file, which is not a readable file or which does not have write permission.

Replace the filepath with

“/etc/mysql/debian.cnf”

Save the file

Let me change the filepath to

“slash etc slash mysql slash debian dot cnf”

Click on Save.

Switch to the terminal Let us execute our program.

Press the up-arrow key.

Highlight

Output

We see that the output is displayed as:

User doesn't have write permission to this file.

Now, let us see another example based on file attributes.

In this example, we will check whether file1 is newer than file2.

Switch to fileattrib2.sh

Point cursor to the filename

fileattrib2.sh

Let us see the program.

Note that the filename is fileattrib2.sh


Let's go through the code.

Highlight

file1= “/home/ttt/empty1.sh”

file2= “/home/ttt/empty2.sh”

Here we have two variables file1 and file2.

The two files have already been created and are empty.

Highlight if Here we check whether file1 is newer than file2.
Highlight the echo statements as per the narration If the condition is true, we print file1 is newer than file2.

Else, file2 is newer than file1.

Here is another if statement.
Highlight if Here we check whether file1 is older than file2.
Highlight the echo statements as per the narration If the condition is true, we print file1 is older than file2.

Else, we print file2 is older than file1.

On the terminal Come back to our terminal.
Open empty1.sh First, we will edit empty1.sh file
Type:

#!/bin/bash

echo “Hiii”

I will just add an echo statement in it.

Type: echo within doubel quotes Hiii after the double quotes greater than sign empty one dot sh

Press Enter.

On the terminal

Type:

chmod +x fileattrib2.sh

Now, let us make our script executeable.

Type -

chmod plus x fileattrib2 dot sh

On the terminal

Type:

./fileattrib2.sh

Now type dot slash fileattrib2 dot sh


Highlight

Output

We see the output as:

file1 is newer than file2

file2 is older than file1

Open empty2.sh

Type:

#!/bin/bash

echo “Hello”

Now let's edit empty2.sh

Here also I will add an echo statement.


Type echo within double quotes "How are you" after the quotes empty2.sh. Type echo within double quotes "How are you" after the quotes greater than sign >empty2.sh.

Let me clear the prompt.


Now let us execute our script again. Press the up-arrow key. Go to ./fileattrib2.sh

Press Enter


Highlight

Output

Now the output is displayed as:

file2 is newer than file1

file1 is older than file2

This brings us to the end of this tutorial.


Display Slide 8

Summary

Let us summarise.

In this tutorial we learnt,

String comparison

file attributes

==

!=

-f

-s

-w

-nt

-ot

Display Slide 9

Assignment

As an assignment

Explore some more attributes.

Ex: -r

-x

-o

Display Slide 10

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 11

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 12

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 teams.

This is Ashwini Patil from IIT Bombay

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey