O'Reilly logo

CompTIA Linux+ Certification Guide by Philip Inshanally

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing scripts using if statements

We can use if statements in scripting, in order to test a condition. The basic syntax of an if statement is as follows:

if [some condition]; then                execute somethingfiorif [[some condition]]; then                execute somethingfi

We can create a simple if script, using the preceding code as guidance. Sometimes, we may need to use double square brackets, which offer enhancements over the older, single-bracket style. Let's create a script named myif.sh, using vi or nano, as follows:

[philip@localhost Documents]$ vi myif.sh[philip@localhost Documents]$ cat myif.sh#!/bin/bashecho "Welcome to our if statement script"if [[ $1 == 4 ]]; then                echo "You're very smart"fiecho "See you soon!"[philip@localhost Documents]$ chmod +x ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required