Lesson10 - Presentation
Lesson10 - Presentation
part 2
Objectives
Learn how to create arrays
Learn about string manipulation
Learn about herdoc syntax
Learn about random numbers
Formatting Text in bash
2
Bash String Manipulation
https://www.tldp.org/LDP/abs/html/string-manipulation.html
${var:start:length} – get a substring
${var/match/replace} – replace the first occurrence of match with
replace
${var//match/replace} – replace all instances of match with
replace
3
Math in Bash
Surround your math equation in $(()) to calcualte values in bash,
this will be an arithmetic expression
4
Random numbers
To generate a random number in bash you can use $RANDOM , it is a
variable that will return a random number
To specify a range you have to do some arithmetic operations
$(($RANDOM%10+1)) a random number between 1 and 10, use
modulus division by your range size, and had your minimum value
$(($RANDOM%11+10)) a random number between 10 and 20
5
Arrays in bash
Recall arrays are lists of values
To declare and array from a list use:
declare -a arrayName=(item1 item2 item3)
arrayName[0]=item1
arrayName[1]=item2
6
Arrays in bash (cont.)
To access elements of an array you can use the following syntax
${arrayName[0]} = first element
${arrayName[1]} = second elements
To access all elements we can use:
${arrayName[@]} = array expansion into list
Note in this case if this is quoted "${arrayName[@]}" then each
array element will be quoted when creating a list
"${array[@]}" # “One Item” “Two Item” - 2 Items (Usually the desired result)
${array[@]} # One Item Two Item – 4 Items
7
Arrays in bash (cont.)
To access all elemnts we could also use:
${arrayName[*]} = array expansion into list
Note in this case if this is quoted "${arrayName[*]}" then the
list is combined into a single element separated by the first
character in the IFS variable (usually space)
value="welcome"
cat <<< $value
9
Heredoc Syntax
Heredoc syntax allows us to send multiple lines of input to a
command similar to redirecting from a file but without creating a
file
to use heredoc syntax we use <<{end of input indicator}, ex. <<EOF
10
Heredoc Syntax (cont.)
By default variable are expanded
file=file.txt
sftp [email protected] <<EOF
put $file
dir
EOF
11
Heredoc Syntax (cont.)
Sometimes you may want to send the actual variable, to do that
quote the end of input indicator with single quotes at the opening
tag only
12
Bash Formatting
Bash supports a lot of text formatting, including bold, underlining,
inverting background and foreground and changing colours of both
the background and foreground
These features are dependent on your terminal.
For example, some terminals only support 8 colours others
support 256.
Some support text blinking while most do not.
It’s also based on commands allowing the terminal to interpret
the output
Some commands also support viewing the formatting like less -R 13
Bash Formatting (cont.)
The first thing you have to do is use a bash escape character, \033
seems to be the most supported and the one I would recommend
using
Next, you need to enter your code for the desired effect.
Reference this guide
https://misc.flogisoft.com/bash/tip_colors_and_formatting
echo -e "\033[32m"
16