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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script:''' '''BASH comparison''' '''Author:''' Ashwini Patil '''Keywords: video tutorial, ==, !=, string compare, file attributes.''' {| style="border-spacing:0;…')
 
Line 1: Line 1:
'''Title of script:''' '''BASH comparison'''
+
'''Title of script:''' '''String and File Attributes''' '''comparison in Bash'''
  
 
'''Author:''' Ashwini Patil
 
'''Author:''' Ashwini Patil
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  
  
'''BASH comparison.'''
+
'''String and File Attributes''' '''comparison in Bash.'''
  
 
|-
 
|-

Revision as of 10:54, 24 October 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
  • File attributes comparison
    with few examples


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


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

Display Slide 4


There are two ways to compare a string in Bash.

1) Using == operator

To compare two equal strings.

2) Using != 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.sh


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

Highlight

#!/bin/bash

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.sh as non-root user.”


Here $0 is the zero'th 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.

Save your file and exit the editor.
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 by stating the return value.

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

Press Enter

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

Press Enter

Highlight

The output

The output is displayed as:

You have no permission to run ./strcompare.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 ./strcompare.sh and press Enter

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

If the condition is false, it will echo "File does not exist"
On the terminal


Let us switch to the terminal and execute the program.
Type:

chmod +x fileattrib.sh

Let us first make the file executable.

Type chmod +x fileattrib.sh

and press Enter.

Type:

./fileattrib.sh

Type ./fileattrib.sh

and press Enter.

Highlight

Output

The output is:

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.

Type gedit empty.sh &

and save this file using Ctrl+S.

Replace

if [ -f $file1 ];

then


with


if [ -s $file1 ];

then

Come back to our program.

Let us replace the -f attribute with -s.

Replace

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

with

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

And also replace the filename with 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

Now on the terminal, let's execute the program.

Type ./fileattrib.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

In our program, let us replace the -s command with -w command.
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:

echo “User has write permission to this file”

And second echo statement with:

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

I will use a different file for this example.

I will select a file which is don't have read or write permission.

Replace the filepath with

“/etc/mysql/debian.cnf”

Save the file

Let me change the filepath to

“/etc/mysql/debian.cnf”

and save the changes.

Switch to the terminal Let us execute our program now.
On the terminal

Type:

./fileattrib.sh

Come back to the terminal and type:

./fileattrib.sh

Highlight

Output

We see that the output as:

User doesn't have write permission to this file.

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


I have already typed the code. So, let's go through it now.

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.

Open empty1.sh First, we will edit empty1.sh
Type:

#!/bin/bash

echo “Hiii”

I will just add an echo statement in it.

Type: echo “Hiii”

On the terminal

Type:

chmod +x fileattrib2.sh

Let us execute the program.

Switch to the temrinal and type -

chmod +x fileattrib2.sh

On the terminal

Type:

./fileattrib2.sh

Now type ./fileattrib2.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.

On the terminal type:

./fileattrib2.sh

Let's execute come back to our terminal and type ./fileattrib2.sh
Highlight

Output

The output is displayed as:

file2 is newer than file1

file1 is older than file2

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