Mastering decision-making in scripts is essential for building dynamic and responsive applications. This chapter covers conditional statements, logical operators, and the test command to help manage flow control effectively in scripting languages.
if statement in most scripting languages?if condition { }if (condition)if condition:if {condition}else if condition differ from an if statement?if condition failsifelif statement?elif condition {}elif (condition)elif condition:else if condition:if condition is true and there is no else block?else block will be executed automaticallyelif used?if condition failselse block is not sufficientelse statement?else block is executedif block is executed insteadelif statements can be used in a script?elseifelifelse statement in a script?ifif conditionendif}fi&& represent?!&&||not|| return true?falsetruetruefalse&& operator?if (a && b)if a && bif (a || b)if (a && b == true)false && true in a conditional expression?truefalseundefinednullif (x != 0)if (x ! 0)if (!(x == 0))if (!x == 0)!(a && b) evaluate to?a or b is false, the result is falsetruea and b are true|| operator be used in conditional statements?!(x == 1)?x != 1x == 1x < 1x > 1&& and || in a single conditional expression?&& operation takes precedence|| operation takes precedencetest command [ ] in scripting languages?test command to check if a file exists?[ -f file.txt ][ file.txt ]test -e file.txttest [ file.txt ][ and [[ in scripting?[ is used for logical operations, and [[ is used for numeric operations[ is a command, and [[ is an internal keyword for advanced tests[[ is only used for string comparisontest command [ ]?[ ] in a script?[ -z $var ][ ! -z $var ][ -n $var ][ $var ][ -f myfile.txt ]?[[ ]] for string comparison?==!==~=>[[ -d directory ]] test for?[[ ]]?[[ $var == "text" ]][[ $var = "text" ]][ $var == "text" ][ $var = "text" ][[ 5 -gt 3 ]] is used in a script?| QNo | Answer (Option with Text) |
|---|---|
| 1 | b) if (condition) |
| 2 | b) It runs only if the previous if condition fails |
| 3 | c) elif condition: |
| 4 | b) The script skips the block entirely |
| 5 | b) When multiple conditions need to be checked in a sequence |
| 6 | b) The output of the else block is executed |
| 7 | c) As many as needed |
| 8 | a) else |
| 9 | a) To handle any condition not covered by if |
| 10 | c) fi |
| 11 | b) AND |
| 12 | a) ! |
| 13 | c) When at least one condition is true |
| 14 | a) if (a && b) |
| 15 | b) false |
| 16 | a) if (x != 0) |
| 17 | b) It negates the outcome of the AND operation |
| 18 | b) When at least one of the conditions needs to be true |
| 19 | a) x != 1 |
| 20 | b) The && operation takes precedence |
| 21 | b) To check conditions in scripts |
| 22 | a) [ -f file.txt ] |
| 23 | b) [ is a command, and [[ is an internal keyword for advanced tests |
| 24 | d) All of the above |
| 25 | b) [ ! -z $var ] |
| 26 | a) Returns true if the file exists |
| 27 | d) => |
| 28 | a) Whether a directory exists |
| 29 | a) [[ $var == "text" ]] |
| 30 | a) True |