bsbt6110 wk4 day2 - university of colorado denver
TRANSCRIPT
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
Script: Command Substitute
Create “sub.sh” using nano
#! /bin/bash# This is a basic bash script
d=$(pwd)
echo $d
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”
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
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
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.
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 ?
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
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
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
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]}
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
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
Exercise: for loop with array
Create “for_array.sh” in nano
arr=(“apple” “banana” “cherry”)for i in ${arr[@]}doecho $i
done
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
Exercise: case selection
Create “case.sh” in nano
a=“dog”case $a in
cat) echo “Feline”;;dog|puppy) echo “Canine”;;*) echo “No match!”;;
esac
Using function
Create “function.sh” in nano
function greet {echo “Hi there!”
}
echo “And now, a greeting”greet
Using function argument
Create “argument.sh” in nano
function greet {echo “Hi $1”
}
echo “And now, a greeting”greet Scott
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
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