Mastering Branching and Testing in Linux
This insightful guide delves into the world of branching with 'if' statements, understanding exit status, testing expressions, working with string and integer expressions, using different versions of 'test' expressions, and combining expressions effectively for better script performance. Dive into the nuances of flow control and optimize your Linux commands with precision.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
The Linux Command Line Chapter 27 Flow Control: Branching with if Prepared by Dr. Reyes, New York City College of Technology
Branching with if if a statement that allows you to evaluate a condition and branch based on the results Syntax Example
Exit Status Exit Status integer value returned by a command when it terminates execution o Range from 0 - 255 o Value of 0 indicates success o Values 1 - 255 indicates error or failure o Man pages explain what each exit status means for a command $? parameter used to examine the exit status of the last command executed
Test and Expressions An if statement will test an expression You may use the keyword test or enclose the expression within brackets [ ] o test expression o [ expression ] When using [ expression ] you must leave a space after opening the first bracket and another space before closing the last bracket o [expression] may give unexpected results (notice the lack of spaces) A test will return zero if the expression evaluates to true, non-zero otherwise. o Strange, but to be consistent with the exit status explained before
Other Versions of test [[ expression ]] allows the use of regular expressions and patterns, still need spaces. (( arithmetic expression )) makes arithmetic tests easier, does not require spaces.
Combining Expressions Recommendation