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

From Script | Spoken-Tutorial
Jump to: navigation, search
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