0% found this document useful (0 votes)
15 views

Lecture 05

The document discusses Bash scripting control structures including functions, variables, logical operators, if/else statements, loops, and exit commands. It provides examples of declaring and calling functions, setting local vs global variables, using comparison operators and Boolean logic, common if/loop use cases, and exiting a script with return codes.

Uploaded by

dire
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 05

The document discusses Bash scripting control structures including functions, variables, logical operators, if/else statements, loops, and exit commands. It provides examples of declaring and calling functions, setting local vs global variables, using comparison operators and Boolean logic, common if/loop use cases, and exiting a script with return codes.

Uploaded by

dire
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lecture 5: Control Structures CSE 374: Intermediate

Programming Concepts and


in Bash Tools

1
Administrivia
HW1 Live - Due next Thursday 10/14 at 11:50pm

CSE 374 AU 20 - KASEY CHAMPION 2


Functions ~/hello_world.sh
#!/bin/bash

declaration
function_name() { hello_world () {
echo 'hello, world'
commands
}
}
- semicolons not required at end of lines
hello_world

single line version Returns


function_name() { commands; } Bash functions don’t allow for return values
Return value is status of last executed statement (0 for success, non-
- semicolon required zero for fail)
return and exit keywords both end function and send error code

call by name Parameters


Bash functions cannot take their own parameters, but can reference
function_name script level arguments using the $N syntax

https://linuxize.com/post/bash-functions/ 3
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'

my_function () { Output:
local var1='C'
Before executing function: var1: A, var2: B
var2='D'
Inside function: var1: C, var2: D
echo "Inside function: var1: $var1, var2: $var2"
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
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
If Statements if [ $# -ne 2 ]
then
if [ test ]; then echo "$0: takes 2 arguments" 1>&2
commands exit 1
fi 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
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


Loops
while [ test ] counter=1 while [ $# -gt 0 ]

do while [ $counter -le 10 ] do


echo $*
commands do
echo $counter shift
done
((counter++)) done

done

for variable in words; do for value in {1..5}


commands do
done echo $value
done

https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php 8
Common loop use cases
▪Iterate over files ▪Iterate over arguments to script
s + d i re ctories
for file in $(ls) <- All file 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


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


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

You might also like