Top Banner
BSBT6110_WK4_DAY2 Tzu L. Phang 2016-09-22
39

BSBT6110 WK4 DAY2 - University of Colorado Denver

Nov 05, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BSBT6110 WK4 DAY2 - University of Colorado Denver

BSBT6110_WK4_DAY2

Tzu L. Phang

2016-09-22

Page 2: BSBT6110 WK4 DAY2 - University of Colorado Denver

Define or declare variable’s attributes

Figure 1:

Page 3: BSBT6110 WK4 DAY2 - University of Colorado Denver

Build in variables

I echo $PWDI echo $HOMEI echo $HOSTNAMEI echo $BASH_VERSIONI echo $SECONDSI echo $0 (return the name of the script)I echo $PWD

To explore more Built-in variable

Page 4: BSBT6110 WK4 DAY2 - University of Colorado Denver

Script: Command Substitute

Create “sub.sh” using nano

#! /bin/bash# This is a basic bash script

d=$(pwd)

echo $d

Page 5: BSBT6110 WK4 DAY2 - University of Colorado Denver

Script: Command Substitute . . . let’s do more . . .

Add onto “sub.sh” using nano

#! /bin/bash# This is a basic bash script

d=$(pwd)

echo $d

a=$(ping -c 1 example.com | grep ‘bytes from’ | cut -d = -f4)

echo “The ping was $a”

Page 6: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with Numbers

Figure 2:

Page 7: BSBT6110 WK4 DAY2 - University of Colorado Denver

Arithmetic Operation

Figure 3:

Page 8: BSBT6110 WK4 DAY2 - University of Colorado Denver

Script: number operation examples

Create “arithmetic.sh” in nano

#! /bin/bash# This is a basic bash script

d=2e=$(( d+2 )) # Use (( )) to perform arithmetic echo $e(( e++ )); echo $e(( e+=5 )); echo $e(( e+=3 )); echo $e(( e/=3 )); echo $e(( e-=5 )): echo $e

Page 9: BSBT6110 WK4 DAY2 - University of Colorado Denver

Bash only work on integer

f=$(( 1/3 )) # return 0

echo $f# Need special command to show decimalsg=$(echo 1/3 | bc -l)

echo $g

Page 10: BSBT6110 WK4 DAY2 - University of Colorado Denver

Camparison Values

Figure 4:

Page 11: BSBT6110 WK4 DAY2 - University of Colorado Denver

Comparison Operation

Figure 5:

Page 12: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: Comparison

Create “compare.sh” using nano

#! /bin/bash# This is a basic bash script

[[ “cat” == “cat” ]]echo $? # Return “0” for success

[[ “cat” == “dog” ]]echo $? # Return “1” for fail

[[ 20 > 100 ]]echo $? # Return “success”; why???[[ 20 -gt 100 ]]echo $? # Now it is correct.

Page 13: BSBT6110 WK4 DAY2 - University of Colorado Denver

Number Comparison Operation

Figure 6:

Page 14: BSBT6110 WK4 DAY2 - University of Colorado Denver

Logic Operation

Figure 7:

Page 15: BSBT6110 WK4 DAY2 - University of Colorado Denver

String null value

Figure 8:

Page 16: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: string null value

Create “null.sh” in nano

#! /bin/bash# This is a basic bash script

a=“” # An empty stringb=“cat” # Non empty string[[ -z $a && -n $b ]] # ask if “a” is null and (&&) “b” is not nullecho ?

Page 17: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with String

Create “string.sh” using nano

#! /bin/bash# This is a basic bash script

a=“hello”b=“world”c=$a$becho $c

echo ${#c} # brace { } expansion to count how manycharacters

d=${c:3} # brace { } expansion to substring; starting at 3rdletterecho $d

Linux is zero based count language

Page 18: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with stirng . . . cont . . .

echo ${c:3:4} # start at 3rd letter and extract 4echo ${c: -4} # from the end to the beginning (space beforenegative)echo ${c: -4:3} # first 3 letters of the last 4 letters

fruit=“apple banana banana cherry”echo ${fruit/banana/durian}# replace the first occurance of “banana” with “durian”echo ${fruit//banana/durian} # now replace all occurancesecho ${fruit/#apple/durian} # only replace if first occuranceecho ${fruit/%cherry/durian} # only replace if last occuranceecho ${fruit/*c/durian} # regular expression

Page 19: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with array

a=() # this is how to define an empty arrayb=(“apple” “banana” “cherry”)# an array with 3 element (no need “,”)echo ${b[2]} # To retrieve, use zero based notationb[5]=“kiwi” # set array by index numberb+=(“mango”) # to add to the next positionecho ${b[@]} # represent the whole arrayecho ${b[@]: -1} # retrieve the last element

Page 20: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with association array

declare -A myarraymyarray[color]=blue # “color” is the key for “blue”myarray[“office building”]=“HQ West”

echo ${myarray[“office building”]} is ${myarray[color]}

Page 21: BSBT6110 WK4 DAY2 - University of Colorado Denver

Control Structure: if statement

Figure 9:

Page 22: BSBT6110 WK4 DAY2 - University of Colorado Denver

if statement . . . cont . . .

Figure 10:

Page 23: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: if statement

Create “if.sh” using nano

a=5if[ $a -gt 4 ]; thenecho $a is greater than 4!

elseecho $a is not greater than 4!

fi

Page 24: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exerise: Regular Expression in if statement

Create “if_regexp.sh” in nano

a=“This is my string”if[[ $a=~[0-9]+ ]]; thenecho “There is numbers in the string: $a”

elseecho “This is no number in the string: $a”

fi

Page 25: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: while loop

Create “while.sh” in nano

i=0while[ $i -le 10 ]; doecho i:$i((i+=1))

done

Page 26: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: until loop

Create “until.sh” in nano

j=0until[ $j -ge 10 ]; doecho j:$j((j+=1))

done

Page 27: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: for loop basic

Create “for_basic.sh” in nano

for i in 1 2 3doecho $i

done

Page 28: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: for loop with brace { }

Create “for_brace.sh” in nano

for i in {1..100}doecho $i

done

Page 29: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: for loop C style

Create “for_C.sh” in nano

for((i=1; i<=10; i++))doecho $i

done

Page 30: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: for loop with array

Create “for_array.sh” in nano

arr=(“apple” “banana” “cherry”)for i in ${arr[@]}doecho $i

done

Page 31: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: for loop with **association array*

Create “for_assoc.sh” in nano

declare -A arrarr[“name”]=“Scott”arr[“id”]=“1234”for i in “$!arr[@]}”doecho “$i: ${arr{$i]}”

done

Page 32: BSBT6110 WK4 DAY2 - University of Colorado Denver

Exercise: case selection

Create “case.sh” in nano

a=“dog”case $a in

cat) echo “Feline”;;dog|puppy) echo “Canine”;;*) echo “No match!”;;

esac

Page 33: BSBT6110 WK4 DAY2 - University of Colorado Denver

Using function

Create “function.sh” in nano

function greet {echo “Hi there!”

}

echo “And now, a greeting”greet

Page 34: BSBT6110 WK4 DAY2 - University of Colorado Denver

Using function argument

Create “argument.sh” in nano

function greet {echo “Hi $1”

}

echo “And now, a greeting”greet Scott

Page 35: BSBT6110 WK4 DAY2 - University of Colorado Denver

Passing 2 argument into function

Change “argument.sh” in nano

function greet {echo “Hi $1! What a nice $2”

}

echo “And now, a greeting!”greet Scott Morninggreet Everybody Evening

Page 36: BSBT6110 WK4 DAY2 - University of Colorado Denver

Passing many argument into function

Create “many_argument.sh” in nano

function numberthings {i=1 for f in $@; doecho $i: $f((i+=1))

done}

numberthings $(ls)numberthins pine birch maple spruce

Page 37: BSBT6110 WK4 DAY2 - University of Colorado Denver

Working with arguments

Page 38: BSBT6110 WK4 DAY2 - University of Colorado Denver

Getting input during execution

Page 39: BSBT6110 WK4 DAY2 - University of Colorado Denver