Join Scott Simpson for an in-depth discussion in this video Testing truth conditions with the if keyword, part of Up and Running with Bash Scripting.
…An if statement executes code based on the truth value of a given expression.…In Bash, that takes the form of if, followed by a statement,…usually in single or test brackets or extended test or double square brackets.…Or it can be contained within double parentheses for…an integer comparison, or have no brackets at all.…Whatever is appropriate for the comparison you're doing.…That's followed up with a semicolon and the word then, or…you can drop the semicolon and put one on it's own line.…After then, goes whatever you want to execute if the expression is true.…
Optionally, you can specify an else condition, which…executes if the tested expression evaluates is false.…And you close the if statement with fi, if backwards.…If you need to do additional evaluation within an else block, you can…use the lf or elif statement with it's own then, since it's another evaluation.…Let's take a look at how you might use this in a script.…I'll open up my working file my.sh and I'll set a equal to five.…And then I'll use an if statement with integer comparison.…
- What is Bash?
- Managing output with grep, awk, and cut
- Understanding Bash script syntax
- Creating a basic Bash script
- Displaying text with "echo"
- Working with numbers, strings, and arrays
- Reading and writing text files
- Working with loops
- Using functions
- Getting user input during execution
- Ensuring a response
Skill Level Beginner
1. Working with the Command Line
2. Building Bash Scripts
3. Control Structures
4. Interacting with the User
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.