1 00:00:00,000 --> 00:00:01,949 [No audio] 2 00:00:01,949 --> 00:00:05,789 Do While scripts. Do while scripts are 3 00:00:05,789 --> 00:00:09,299 very similar to for loop scripts. 4 00:00:09,299 --> 00:00:11,279 [No audio] 5 00:00:11,279 --> 00:00:13,979 The do while script or statement 6 00:00:13,979 --> 00:00:17,249 continually executes a block of 7 00:00:17,249 --> 00:00:19,679 statements while a particular condition 8 00:00:19,679 --> 00:00:23,219 is true or met. For example, if you are 9 00:00:23,219 --> 00:00:25,049 running a script and you create it in a 10 00:00:25,049 --> 00:00:29,249 loop, it will run until a certain 11 00:00:29,549 --> 00:00:32,609 variable is met. For example, if you 12 00:00:32,639 --> 00:00:35,099 have, if you want to run the script 13 00:00:35,129 --> 00:00:41,099 until August 2015, to 2pm, then you 14 00:00:41,099 --> 00:00:44,609 will define that in do while script. And 15 00:00:44,639 --> 00:00:47,369 a lot of times, the scripts or the 16 00:00:47,399 --> 00:00:50,459 daemons, the process that never dies, that 17 00:00:50,459 --> 00:00:52,049 keep running and running and running, 18 00:00:52,289 --> 00:00:54,659 they always have do while scripts 19 00:00:54,659 --> 00:00:57,959 because they are running continuously 20 00:00:58,229 --> 00:01:05,069 until a certain condition is met. So for 21 00:01:05,069 --> 00:01:07,979 example, run a script until 2pm. As soon 22 00:01:07,979 --> 00:01:11,039 as the 2pm hits, you are done, exit out. 23 00:01:12,029 --> 00:01:16,676 While, condition, do, command1, command2, 24 00:01:16,676 --> 00:01:18,749 command3. And once all these 25 00:01:18,749 --> 00:01:21,299 commands are done, and the condition is 26 00:01:21,299 --> 00:01:25,739 done, then you are done. You are done 27 00:01:25,769 --> 00:01:28,859 with your script. So let's look at our 28 00:01:28,859 --> 00:01:33,648 Linux machine, and let's try out a do while script. 29 00:01:33,648 --> 00:01:36,935 [No audio] 30 00:01:36,935 --> 00:01:39,029 Let's clear the screen, where we 31 00:01:39,029 --> 00:01:41,249 are, we are in myscripts directory, which 32 00:01:41,249 --> 00:01:45,089 is this directory we want to be in, and 33 00:01:45,389 --> 00:01:47,129 this time we're going to create a new 34 00:01:47,129 --> 00:01:49,049 script, and we are going to call it 35 00:01:49,439 --> 00:01:52,534 script-dowhile. 36 00:01:53,534 --> 00:01:56,339 Again, it's just the 37 00:01:56,339 --> 00:01:57,389 name I'm picking, you could pick 38 00:01:57,389 --> 00:02:00,689 anything you want. Hit enter, you are in 39 00:02:00,689 --> 00:02:03,959 vi mode, you hit insert, and you start 40 00:02:03,959 --> 00:02:05,579 typing. What's the first thing that you 41 00:02:05,579 --> 00:02:11,399 type, you type hash, bang, /bin/bash, which 42 00:02:11,399 --> 00:02:15,119 defines your shell environment. And if 43 00:02:15,119 --> 00:02:17,399 you wanted to put a comment, you could 44 00:02:17,399 --> 00:02:26,832 say, This script is created by Imran Afzal 45 00:02:26,832 --> 00:02:32,669 on 2018. Yeah, that's fine. And if you 46 00:02:32,669 --> 00:02:34,349 want to put a description of the script, 47 00:02:34,349 --> 00:02:36,269 you could do that. But let's get into 48 00:02:36,269 --> 00:02:38,789 the script. I have already written down 49 00:02:38,789 --> 00:02:41,099 a script, and I'm just gonna go in and 50 00:02:41,099 --> 00:02:43,019 copy it, there are two scripts in my 51 00:02:43,019 --> 00:02:45,599 handout, I'm going to take the second 52 00:02:45,599 --> 00:02:47,129 one, I want you to do the first one 53 00:02:47,129 --> 00:02:49,349 yourself, I'm just going to copy it, and 54 00:02:49,349 --> 00:02:52,559 I'm going to paste it real quick. Let's 55 00:02:52,559 --> 00:02:55,079 see what the script does. The script is 56 00:02:55,079 --> 00:02:57,359 doing, of course, the first one is 57 00:02:57,389 --> 00:03:00,089 defining the shell which we already 58 00:03:00,089 --> 00:03:01,949 have. So we're going to take it out by 59 00:03:02,009 --> 00:03:05,969 hitting dd, and then it's telling us 60 00:03:05,969 --> 00:03:09,149 that the count is zero, the number is 61 00:03:09,149 --> 00:03:13,619 10. While the count is less than 10, do 62 00:03:14,219 --> 00:03:16,829 echo number of seconds left to stop this 63 00:03:16,829 --> 00:03:19,589 process. And the process is the variable 64 00:03:20,009 --> 00:03:21,299 which is $1. 65 00:03:21,299 --> 00:03:28,980 [No audio] 66 00:03:28,980 --> 00:03:32,670 And echo, it's just an empty space, empty line, 67 00:03:32,820 --> 00:03:37,800 sleep, sleep is a command that will wait 68 00:03:37,800 --> 00:03:39,810 for the number of seconds that is 69 00:03:39,810 --> 00:03:44,070 defined after the sleep command, then it 70 00:03:44,070 --> 00:03:46,770 has a few number of expressions. These 71 00:03:46,770 --> 00:03:48,240 are the expressions you could get it 72 00:03:48,240 --> 00:03:51,330 anywhere online, I have that expression 73 00:03:51,330 --> 00:03:54,390 for a while that I tried out many times 74 00:03:54,390 --> 00:03:55,830 in my work environment, and works 75 00:03:55,830 --> 00:03:58,110 perfectly. And if you want to use it in 76 00:03:58,110 --> 00:04:00,120 your work environment you are more than 77 00:04:00,120 --> 00:04:02,430 welcome to do. So the next one is the 78 00:04:02,460 --> 00:04:04,980 echo, and then it says echo $1, 79 00:04:04,980 --> 00:04:09,690 process is stopped!!!!, and echo again, it is 80 00:04:09,690 --> 00:04:12,900 telling it that it's giving an empty 81 00:04:12,900 --> 00:04:15,960 line. So let's run the script and let's 82 00:04:15,960 --> 00:04:20,220 see what it does. Give it an executable 83 00:04:20,220 --> 00:04:25,200 permissions. It says 10 seconds left to 84 00:04:25,200 --> 00:04:33,990 stop this process, 9, 8, 7, 6, 5, 4, 3, 2, 1, and done, 85 00:04:33,990 --> 00:04:37,830 process is stopped!!!! You see that? So 86 00:04:37,830 --> 00:04:42,480 these do while scripts are created for a 87 00:04:42,480 --> 00:04:49,500 certain value or certain time to be met. 88 00:04:49,890 --> 00:04:53,430 And once that variable is met, they will 89 00:04:53,460 --> 00:04:57,450 stop the process and exit out of it. I 90 00:04:57,450 --> 00:04:59,580 want you to create a new script in 91 00:04:59,580 --> 00:05:02,130 you're in environment, and this is the 92 00:05:02,130 --> 00:05:04,380 script. Again, this is also in the 93 00:05:04,380 --> 00:05:06,900 handouts. You could copy and paste this 94 00:05:06,900 --> 00:05:09,150 script, create a new script, and run it. 95 00:05:09,630 --> 00:05:13,380 You could also be my guest to go online 96 00:05:13,650 --> 00:05:16,800 and just type do while script examples 97 00:05:16,800 --> 00:05:19,500 there are tons and tons of do while 98 00:05:19,680 --> 00:05:22,920 scripts that you could try and get, get 99 00:05:22,920 --> 00:05:28,560 a low exposure to do while scripts. I'm 100 00:05:28,560 --> 00:05:30,600 sure by the time you will run at least 101 00:05:30,600 --> 00:05:32,940 five or six scripts you know exactly 102 00:05:33,210 --> 00:05:35,946 what this statement do. 103 00:05:35,946 --> 00:05:40,139 [No audio]