Bash Control Structures Overview: Functions, Variables, Boolean Logic, and If Statements

Slide Note
Embed
Share

This lecture covers essential topics in Bash scripting, including control structures such as functions, variable scope, boolean logic, and if statements. Understand how to declare functions, manage variable scope, use boolean operators for logic operations, and create conditional if statements for decision-making in Bash scripting.


Uploaded on Oct 03, 2024 | 0 Views


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. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lecture 5: Control Structures in Bash CSE 374: Intermediate Programming Concepts and Tools 1

  2. Administrivia HW1 Live - Due next Thursday 10/14 at 11:50pm CSE 374 AU 20 - KASEY CHAMPION 2

  3. Functions ~/hello_world.sh #!/bin/bash declaration function_name() { commands } semicolons not required at end of lines hello_world () { echo 'hello, world' } - hello_world single line version function_name() { commands; } semicolon required Returns Bash functions don t allow for return values Return value is status of last executed statement (0 for success, non-zero for fail) return and exit keywords both end function and send error code - call by name function_name Parameters Bash functions cannot take their own parameters, but can reference script level arguments using the $N syntax https://linuxize.com/post/bash-functions/ 3

  4. Variable Scope All variables are by default global, even if declared inside a function Local can be declared using the local keyword ~/variables_scope.sh #!/bin/bash var1='A' var2='B' Output: my_function () { local var1='C' var2='D' echo "Inside function: var1: $var1, var2: $var2" } Before executing function: var1: A, var2: B Inside function: var1: C, var2: D After executing function: var1: A, var2: D echo "Before executing function: var1: $var1, var2: $var2" my_function echo "After executing function: var1: $var1, var2: $var2" 4

  5. Boolean Logic in Bash Bash understands Boolean logic syntax && and || or ! not binary operators: -eq equals Ex: 1 -eq 1 is TRUE -ne not equals Ex: 1 -ne 2 is TRUE -lt less than Ex: 1 -lt 2 is TRUE -gt greater than Ex: 1 -gt 2 is FALSE -le less than or equal to Ex: 2 -le 2 is TRUE -ge greater than or equal to Ex: 2 -ge 1 is TRUE Square brackets encase tests: [ test ] you can use the typical symbols for comparison when between brackets, but syntax will be a bit different https://opensource.com/article/19/10/programming-bash-logical-operators-shell-expansions 5

  6. If Statements if [ $# -ne 2 ] then echo "$0: takes 2 arguments" 1>&2 exit 1 fi if [ test ]; then commands fi if [ -f .bash_profile ]; then echo You have a .bash_profile. else echo You do not have a .bash_profile fi https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php 6

  7. Common If Use Cases If file contains if grep q E myregex file.txt; then echo found it! fi -q option quiet suppresses the output from the loop If is gated on successful command execution (returns 0) If incorrect number of arguments passed if [ $# -ne 2 ]; then echo $0 requires 2 arguments >&2 exit 1 fi Checks if number of arguments is not equal to 2, if so prints an error message to stderr and exits with error code CSE 374 AU 20 - KASEY CHAMPION 7

  8. Loops counter=1 while [ test ] do commands while [ $# -gt 0 ] while [ $counter -le 10 ] do do echo $* echo $counter shift done ((counter++)) done done for value in {1..5} for variable in words; do commands done do echo $value done https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php 8

  9. Common loop use cases Iterate over files Iterate over arguments to script for file in $(ls) while [ $# -gt 0 ] do do if [-f $file ]; then echo $* echo $file shift fi done done Shift command moves through list of arguments Similar to .next in Java Scanner CSE 374 AU 20 - KASEY CHAMPION 9

  10. Exit Command Ends a script s execution immediately -Like return End scripts with a code to tell the computer whether the script was successful or had an error 0 = successful -exit without a number defaults to 0 exit exit 0 Non 0 = error exit 1 ctrl+C aborts execution CSE 374 AU 20 - KASEY CHAMPION 10

  11. Scripting demo: search CSE 374 AU 20 - KASEY CHAMPION 11

More Related Content