Learn to create a while loop at the Linux command line.
Linux bash shell script while keyword with examples (3:56)
Videos can also be accessed from the YouTube Playlist.
Our ninety-sixth word, or command (keyword) to memorize is
while from our category
while allows you to create a
Recall from videos (tutorials) #87 to #95, we're using a script to
demonstrate workflow, and now we'll introduce
while at the command line and then
we'll script it.
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s).
while command (keyword) has
no traditional options and no arguments as it has its
own syntax. Also,
while is technically
a keyword, not a command, and we'll cover 3 related keywords here:
Unlike most commands, help is not available with double-dash
while is a shell built-in
covered in the
So why is
while an important command (keyword)?
Well, for menus to work we need to stay within a script and be offered
choices to follow the branches and back.
And now you know how to do that.
Okay, the best way to embed this in your memory is by typing in your own terminal window.
Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.
Here we go. Let's review those 3 key words I mentioned in this group.
As you can see, they all are keywords.
Next, trying a counting example with a
while then a
test block with an expression from
our video (tutorial) on
test, and then
while $count is less than or
equal to 8, do a print of the count each time, then add 1 to the count
and head up to do it again.
Next, let's see
while code using
nano and this is in our script
that we've been working on.
(Below is the screen from within
while is here, above the menu but
below the functions we addressed in video (tutorial) #95, and while
true, do this menu code below, and then within those menu items it
points to the functions above.
(Please note: the code above is meant to illustrate the concept of a multi-level conditional statement. It will not run without other code in the script. Go to the last video #100 to see the code for the whole script if you'd like to try it on your own.)
Ctrl-x to leave
y to confirm saving.)
And last, let's run it. Give it a try.
Our first section from the earlier videos shows up first, if you recall. (The section below is after the pause and clear screen.)
Then our menu, with the four choices and the fifth leaves the
while loop, and exits, right? And
then here we'll select that fifth option to quit and then come back
later to see these other functions.
Okay now you know how to use
And you know the syntax for commands, options and arguments.
One last tip about the
(keyword). So the takeaway is this:
do a block of code, otherwise
Okay, thanks for visiting today. I hope this was a helpful
introduction to the
while command (keyword).
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Don't miss out on new YouTube Channel content. Subscribe here.
functionstatement, hit Back.
casekeyword offers an easy alternative when creating menus, click Next.