Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Read, Loops and While" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James demonstrates the read command, which takes input from the keyboard and assigns it to a variable. After that, James covers loops that run a set of commands repeatedly. Then James expands upon the loop to show the while loop, execute a set of commands repeatedly until some condition occurs.


Transcript from the "Read, Loops and While" Lesson

>> James Halliday: Okay, so hopefully people got a bit of a chance to experiment with writing a little script, whatever commands you wanna put in here. And you can put more commands if you want as well, so you could print the date. I don't know. Do whatever you want. Few extra things I want to add based on some of the feedback I got that aren't in the notes yet, but hopefully I will remember to add them, is that if you want to do things for your script, like maybe prompt for input, there a command called read that you can use to read a line from standard end and set the result into an environment variable.

So you do read LINE, maybe. That's a good name for a line of input. If you type that in then you type the message, hit Enter, and it's gonna save that value into an environment variable called LINE. So this is a way then, in your scripts, you can do things like prompt for input.

So for example, if you had a script that did, let me just change the script up a bit. So there's an option to echo called -n that doesn't print the newline. So you can do things like have a question, or have a little caret or something with -n, and then it's gonna print it without putting the newline at the end.

So if you do that, you could ask, what is your name? And then read NAME, and then say echo hello $NAME. If you use an exclamation mark, that would make sure that you quote it properly, or with double quotes in this case, because exclamation marks are a little bit tricky there.

They actually expand out to be something from the history
>> James Halliday: So now if I run the script, it's gonna prompt me for my name. There we go. And it prints out my name. So this is a way. Sometimes, you wanna read from standard in, although it's less common.

But sometimes, it make sense. A lot of the basic utilities don't generally prompt for input interactively. They mostly deal with argument, but for your own scripts, you can do them however you like. Right, so that's just the read command. The main time when I use the read command is actually in conjunction with some other commands that I'd like to talk about.

They're also not in the notes, but it's very useful for scripting. I just wanna quickly cover a few things, like loops and stuff like that, which I use a whole lot. Actually, I use them all the time, but [LAUGH] I don't know how common they are for other people who use Bash.

So, why don't I just cover it? So we've already seen the watch commands. There's something similar to it, if you just remember. So there's a command called true. This gets into something called exit codes, which I'm gonna talk about a bit later. But suffice it to say for now, there's a command called true that is truth-y.

So if we do something like while true, we can print the date. There's also a command called sleep that will just sit there for however many seconds you specify. And so this command, every second, will print out the date. There's also another kind of loop where if you have command that will split things by white space, you can loop over all of the values.

So we can use the brace expansion for sequences to do something fun there. So if we [INAUDIBLE] do for x in {0..10}; echo $x. In this case, for is going to define a variable called whatever we specified right here, and then we can give it a list of things to loop over.

And then, for every one of those things, we can run this command. And you could put as many commands as you want in here, separated by semicolons or on their own line, and then at the end, you put done, and that tells the loop to be over. So that just, ahh!

Sorry. I did this wrong. You have to put do immediately after for. And then it will print. These will become more useful once we talk about exit codes and signals a bit later.
>> James Halliday: For now, it's just kind of this nifty little one liner you can use. The while true thing is really handy if you have buggy scripts, like a server or something that crashes sometimes, but it's not that important, so you just want it to restart itself.

So you could do, I do this all the time, I do while true; do node server.js; done. If it crashes, whatever, it just restarts. Pretty handy, little trick to do.
>> James Halliday: Another really useful thing to use loops for, especially while loops, is if you have a command like ls -1, that's gonna give us every file in a directory, you can pipe into a while loop, which is kind of a little bit strange to think about, but it'll hopefully make sense in a second.

So we can combine all of the the things that we've already seen to do something really cool. So if you remember, the read command will read a line from standard in. So we can read each line of standard in into an environment variable called line, and maybe we'll do the wc -l command to print out the number of lines for every file, and maybe we'll also print out the name of the file.

Every file should be coming in on standard in. And then we wanna do -n, so that it doesn't print out A new line, and we also wanna put a space in there. And there's another little thing, because my ls happens to be configured so that it does, puts those starts and things at the end if it's an executable.

So I'm actually gonna do /bin/ls to get around to my alias. You probably won't have to do this, but in my case, I do. A little bit lost in the weeds, but this is a really neat thing that you can do. Hang on. And then you have to put done at the end.

There we go. Wc -l also prints out the file after. So, I don't like that. I'm gonna get rid of that. I could pipe into sed and cut off the end, but I'll pipe into awk and print out the first column. There we go. So you don't have to know exactly what this means, but it's just something that you can do, if you know a little bit about each of these tools.

We can print our own file formats and things. And I'll paste that into chat as well, just for some kind of examples to chew on, if nothing else.
>> James Halliday: Again, relatively advance use case, but it's kind of, hopefully, will give you an idea for how you can sort of string together a lot of these concepts in a coherent way.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now