/ factorpad.com / tech / linux-essentials / while-command.html
An ad-free and cookie-free website.
Intermediate
Learn to create a while loop at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux bash Shell Script while Keyword with Examples (3:56)
Our ninety-sixth word, or command (keyword) to memorize is
while
from our category
Workflow.
while
allows you to create a
program loop.
-options | description |
---|---|
|
None for the while keyword |
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)
.
The 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:
while
,
do
and
done
.
Unlike most commands, help is not available with double-dash
--help
, as
while
is a shell built-in
covered in the bash
manual page.
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
loop,
while
then a
test
block with an expression from
our video (tutorial) on test
, and then
saying 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 nano
.)
The 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.)
(Hit Ctrl-x
to leave
nano
and
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 while
.
And you know the syntax for commands, options and arguments.
One last tip about the while
command
(keyword). So the takeaway is this:
while
true,
do
a block of code, otherwise
done
.
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 free opportunities to learn. Subscribe at YouTube, follow @factorpad on Twitter and join the no-spam email list.
function
statement, hit Back.case
keyword offers
an easy alternative when creating menus, click Next./ factorpad.com / tech / linux-essentials / while-command.html
A newly-updated free resource. Connect and refer a friend today.