/ factorpad.com / tech / linux-essentials / if-command.html
An ad-free and cookie-free website.
Intermediate
Learn to build a branch in a bash script at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux if then else bash Script with Examples (3:55)
Our ninety-fourth word, or command to memorize is
if
from our category
Workflow.
if
(plus other keywords) allows
you to build a branch in a script.
-options | description |
---|---|
|
None for the if keyword |
Recall from videos (tutorials) #87 to #93, we're using a script to
demonstrate workflow, now we'll introduce
if
at the command line and then
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 if
command (condition/keyword) has
no traditional options and no arguments as it has its
own syntax, and our structure here has been command-based, but
if
is
a technically a bash
keyword, not a
command and I'll explain it with 4 other related keywords:
then
,
else
,
elif
and
fi
.
Unlike most commands, help is not available with double-dash
--help
, as the
if
construct is a shell built-in
covered in the
bash
man
page.
So why is if
an important command (keyword)?
Well, we need to tell bash
to follow
conditional logic.
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 the 5 words in this group that I mentioned earlier.
And as you can see, they all are keywords.
Next, try an if
on one line, with
integers and incorporate the brackets from that video (tutorial) we just
did on test
. And we have to assume
an input variable here, and let's use
myinput=1
.
And then the logic goes, if myinput=1
,
then input was 1, and your exit status was 0 for true, and it ends there
with fi
. (At the command line you can
use the ;
to separate the steps. It
always ends with fi
and will get
there if the condition is true
and it will go to the second step, or if false, it will skip the
second step and go straight to the third, or
fi
.)
Next, let's change myinput
to 2, and
then re-run this line here.
And it skipped to the fi
to end
(and had no output) because the exit status was not true (not zero).
And then, since this line is getting very long, let's view it in our script and pause to review this menu and logic for the branches here, and then we'll try it out.
(Below is the screen from within nano
.)
(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, run it with
~/bin/funscript
and here's our
progress so far.
(The section below is after the pause and clear screen.)
Okay now you know how to use if
.
And you know the syntax for commands, options and arguments.
One last tip about the if
command
(keyword). So at the end there, I left with the #5 to quit because I
built a loop, and the first few choices sit in that loop, and I'll
explain that next.
Okay, thanks for visiting today. I hope this was a helpful
introduction to the if
command
(keyword).
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
See other scripts and programs built in other languages at our YouTube Channel and throughout this website.
printf
command,
hit Back.function
, click Next./ factorpad.com / tech / linux-essentials / if-command.html
A newly-updated free resource. Connect and refer a friend today.