1 00:00:01 --> 00:00:04 Welcome to the spoken tutorial on Basics of Shell Scripting. 2 00:00:05 --> 00:00:08 In this tutorial, we will learn about 3 00:00:09 --> 00:00:10 * System variables 4 00:00:11 --> 00:00:12 * User defined variables and 5 00:00:13 --> 00:00:15 * Accepting user input via keyboard 6 00:00:16 --> 00:00:22 To follow this tutorial you should be familiar with Linux Operating System 7 00:00:23 --> 00:00:28 If not, for relevant tutorials please visit our website which is as shown 8 00:00:29 --> 00:00:31 For this tutorial I am using 9 00:00:32 --> 00:00:34 * Ubuntu Linux 12.04 Operating System and 10 00:00:35 --> 00:00:39 * GNU Bash version 4.1.10 11 00:00:40 --> 00:00:45 Please NoteGNU Bash version 4 or above is recommended for practice. 12 00:00:46 --> 00:00:48 Let us start with a introduction to variables 13 00:00:49 --> 00:00:54 * Bash variables provide temporary storage for information 14 00:00:55 --> 00:01:00 * These variables can be used within the lifespan of the program. 15 00:01:01 --> 00:01:06 * There are two types of variables System variables User defined variables 16 00:01:07 --> 00:01:13 System variables, These are created and maintained by Linux Bash Shell itself. 17 00:01:14 --> 00:01:16 They are defined by Capital letters. 18 00:01:17 --> 00:01:19 Commonly used system variables are 19 00:01:20 --> 00:01:20 * BASH_VERSION, 20 00:01:21 --> 00:01:22 * HOSTNAME, 21 00:01:23 --> 00:01:24 * HOME etc 22 00:01:25 --> 00:01:32 Let us open the terminal by pressing Ctrl Alt and T keys simultaneously on your keyboard. 23 00:01:33 --> 00:01:37 Now type set and press Enter. 24 00:01:38 --> 00:01:41 This will display all the system variables. 25 00:01:42 --> 00:01:52 Alternately, you can type env or printenv, to view all the system variables. 26 00:01:53 --> 00:01:54 Let me clear the prompt 27 00:01:55 --> 00:02:00 Now, type echo space within double quotes dollar sign HOSTNAME 28 00:02:01 --> 00:02:03 and Now press Enter. 29 00:02:04 --> 00:02:06 The hostname of the system will be displayed. 30 00:02:07 --> 00:02:10 Now let's find out the full path of homedirectory. 31 00:02:11 --> 00:02:17 Type echo space within double quotes dollar sign HOME(in Capital) 32 00:02:18 --> 00:02:20 Press Enter. 33 00:02:21 --> 00:02:25 The full path of user's home directory will be displayed. 34 00:02:26 --> 00:02:26 Now, type 35 00:02:27 --> 00:02:31 echo space within double quotes HOME (in capital) 36 00:02:32 --> 00:02:33 Press Enter. 37 00:02:34 --> 00:02:38 This will display only the HOME not the value of HOME variable. 38 00:02:39 --> 00:02:47 So it is neccessary to use dollar sign( '$') at the beginning of every variable, to display its value. 39 00:02:48 --> 00:02:50 Let us switch back to our slides 40 00:02:51 --> 00:02:52 User Defined Variables 41 00:02:53 --> 00:02:56 * These variables are created and maintained by users. 42 00:02:57 --> 00:03:04 * It is always a good idea to avoid uppercase for the names of user defined variables. 43 00:03:05 --> 00:03:11 * This makes it easy to differentiate between user defined and system variables. 44 00:03:12 --> 00:03:13 Switch back to our terminal 45 00:03:14 --> 00:03:19 Type username equal to sign sunita 46 00:03:20 --> 00:03:28 Please note that there should not be any blank space between username, equal to sign and sunita. 47 00:03:29 --> 00:03:29 Now press Enter. 48 00:03:30 --> 00:03:32 To display the value of variable username 49 00:03:33 --> 00:03:39 Type echo space within double quotes dollar sign username 50 00:03:40 --> 00:03:41 press Enter 51 00:03:42 --> 00:03:45 This will display sunita on your terminal. 52 00:03:46 --> 00:03:49 The value of a variable can be unset, 53 00:03:50 --> 00:03:51 Let us switch back to our slide 54 00:03:52 --> 00:03:58 unset, the value of variable can be unset by using the unset command 55 00:03:59 --> 00:04:02 The syntax for this is unset variablename 56 00:04:03 --> 00:04:07 Let's use the previous example where username is our variable. 57 00:04:08 --> 00:04:17 Switch to the Terminal. Now type unset space username press Enter 58 00:04:18 --> 00:04:27 Let us check Type echo space within double quotes dollar sign username press Enter. 59 00:04:28 --> 00:04:29 Nothing will be displayed on the terminal. 60 00:04:30 --> 00:04:35 This means that the value of variable username has been removed. 61 00:04:36 --> 00:04:38 Now switch back to our slide 62 00:04:39 --> 00:04:41 Global and local variables. 63 00:04:42 --> 00:04:48 * In Shell script, user defined variables can be declared globally or locally. 64 00:04:49 --> 00:04:51 * By default, all variables are global. 65 00:04:52 --> 00:04:58 * This means, their values remains the same inside and outside the function. 66 00:04:59 --> 00:05:03 Let us learn how to declare variables globally and locally. 67 00:05:04 --> 00:05:06 Switch to the terminal and type 68 00:05:07 --> 00:05:15 gedit space g_(underscore)variable.sh space & (ampersand sign) 69 00:05:16 --> 00:05:22 gedit is the text editor g_(underscore) variable.sh is our file name 70 00:05:23 --> 00:05:27 and & (ampersand) is use to free up the prompt. 71 00:05:28 --> 00:05:29 press Enter. 72 00:05:30 --> 00:05:34 Type the code as shown here, in your g_(underscore)variable.sh file. 73 00:05:35 --> 00:05:37 Let me explain the code now. 74 00:05:38 --> 00:05:43 The first line, with the hash and exclamation symbol, is a shebang or a bang line. 75 00:05:44 --> 00:05:50 username=sunita is the userdefined variable and it is declared globally. 76 00:05:51 --> 00:05:54 echo will display the string outside function: And 77 00:05:55 --> 00:05:59 dollar username will print the value of the variable username. 78 00:06:00 --> 00:06:03 This is how we defined a function in BASH script. 79 00:06:04 --> 00:06:08 We will discuss about functions in detail, in later tutorials. 80 00:06:09 --> 00:06:11 This is the body of the function. 81 00:06:12 --> 00:06:18 Here another message inside function will be displayed, along with the value of username. 82 00:06:19 --> 00:06:20 Here, we call the function 83 00:06:21 --> 00:06:22 This is our code. Now let's execute it. 84 00:06:23 --> 00:06:25 Come back to our Terminal. 85 00:06:26 --> 00:06:27 Let me clear the prompt 86 00:06:28 --> 00:06:30 First we need to make our file executable. 87 00:06:31 --> 00:06:38 Type chmod space plus x space g_(underscore)variable.sh press Enter 88 00:06:39 --> 00:06:44 Now type dot slash g_(Underscore)variable.sh 89 00:06:45 --> 00:06:46 Press Enter 90 00:06:47 --> 00:06:47 Observe the output. 91 00:06:48 --> 00:06:52 Outside the function, username takes the value sunita. 92 00:06:53 --> 00:06:58 Inside the function also, username takes the same value sunita. 93 00:06:59 --> 00:07:03 This is because username was declared globally outside the function. 94 00:07:04 --> 00:07:08 Next, let us learn how to declare a variable locally. 95 00:07:09 --> 00:07:17 Type gedit space l_(Underscore)variable.sh space & (ampersand sign) 96 00:07:18 --> 00:07:19 Press Enter. 97 00:07:20 --> 00:07:24 Type the code as shown here, in your l_(underscore)variable.sh file. 98 00:07:25 --> 00:07:27 Let me explain the code. 99 00:07:28 --> 00:07:35 The code is the same as before, except for an extra line of code inside the function. 100 00:07:36 --> 00:07:40 Inside the function block, we have a line,local space username equals to jack 101 00:07:41 --> 00:07:47 This assigns a new value for the variable username locally. 102 00:07:48 --> 00:07:49 Now switch to the Terminal. 103 00:07:50 --> 00:07:51 Let's make file executable 104 00:07:52 --> 00:07:59 By Typing chmod space plus x space l_variable.sh 105 00:08:00 --> 00:08:01 Press Enter. 106 00:08:02 --> 00:08:06 Type dot slash l_variable.sh 107 00:08:07 --> 00:08:07 Press Enter. 108 00:08:08 --> 00:08:09 The output is displayed 109 00:08:10 --> 00:08:14 Outside the function, username takes the value sunita. 110 00:08:15 --> 00:08:19 Whereas inside the function, username takes the value jack. 111 00:08:20 --> 00:08:25 This is because username is assigned this value locally, within the function. 112 00:08:26 --> 00:08:30 Now let us quickly see how to get user input via keyboard. 113 00:08:31 --> 00:08:35 The read command is used to accept input from the keyboard. 114 00:08:36 --> 00:08:40 It can also be used to assign an input value to a user defined variable. 115 00:08:41 --> 00:08:43 The syntax of read command is 116 00:08:44 --> 00:08:49 read space hyphen p space within double quotes PROMPT 117 00:08:50 --> 00:08:54 Please note that PROMPT is just a string, that waits for user input. 118 00:08:55 --> 00:08:57 You may replace it with your own string. 119 00:08:58 --> 00:08:59 Now Switch to the terminal 120 00:09:00 --> 00:09:07 Typegedit space read.sh space & (ampersand sign) 121 00:09:08 --> 00:09:08 Press Enter. 122 00:09:09 --> 00:09:13 Type the code as shown, in your read.sh file. 123 00:09:14 --> 00:09:15 Let me explain the code. 124 00:09:16 --> 00:09:20 In this example, input is given from the keyboard by the user. 125 00:09:21 --> 00:09:22 This is the bang line. 126 00:09:23 --> 00:09:30 Here -p displays the prompt, without a newline and takes input from the keyboard. 127 00:09:31 --> 00:09:35 The user input will be stored in the variable username. 128 00:09:36 --> 00:09:37 echo command displays the message 129 00:09:38 --> 00:09:42 Hello and the name entered by the user, via the keyboard. 130 00:09:43 --> 00:09:44 So, let us execute the programme. 131 00:09:45 --> 00:09:48 Come back to our terminal 132 00:09:49 --> 00:09:54 Type chmod space plus x space read.sh 133 00:09:55 --> 00:09:55 press Enter. 134 00:09:56 --> 00:10:00 Typedot slash read.sh press Enter. 135 00:10:01 --> 00:10:03 Here it is displayed Enter username: 136 00:10:04 --> 00:10:07 I will type ashwini press Enter. 137 00:10:08 --> 00:10:12 The message Hello ashwini is displayed. 138 00:10:13 --> 00:10:19 ashwini was assigned as an input value to the user defined variable username. 139 00:10:20 --> 00:10:22 Let us go back to our slide and summarise. 140 00:10:23 --> 00:10:25 In this tutorial we learnt, 141 00:10:26 --> 00:10:26 * System variables 142 00:10:27 --> 00:10:28 * User defined variables 143 00:10:29 --> 00:10:32 * Accepting user input via keyboard 144 00:10:33 --> 00:10:33 As an assignment 145 00:10:34 --> 00:10:37 Write a simple Bash program to get the following system variables 146 00:10:38 --> 00:10:40 * pwd and * logname 147 00:10:41 --> 00:10:42 Write a simple Bash program 148 00:10:43 --> 00:10:45 * To ask username from user 149 00:10:46 --> 00:10:50 * To exit the program, if user does not enter anything, within 10 seconds 150 00:10:51 --> 00:10:55 * {Hint: read -(Hyphen)t 10 -(Hyphen)p} 151 00:10:56 --> 00:10:58 Watch the video available at the link shown below. 152 00:10:59 --> 00:11:01 It summarises the Spoken Tutorial project 153 00:11:02 --> 00:11:06 If you do not have good bandwidth, you can download and watch it 154 00:11:07 --> 00:11:15 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test 155 00:11:16 --> 00:11:22 For more details, please write to contact@spoken-tutorial.org 156 00:11:23 --> 00:11:26 Spoken Tutorial Project is a part of the Talk to a Teacher project 157 00:11:27 --> 00:11:33 It is supported by the National Mission on Education through ICT, MHRD, Government of India 158 00:11:34 --> 00:11:39 More information on this Mission is available at the link shown below http://spoken-tutorial.org\NMEICT-Intro 159 00:11:40 --> 00:11:43 The script has been contributed by FOSSEE and Spoken Tutorial Teams. 160 00:11:44 --> 00:11:49 This is Ashwini Patil from IIT Bombay.Signing off.Thank you for joining.