BASH/C2/String-and-File-attributes/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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