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

Lesson10 - Presentation

Linux presentation

Uploaded by

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

Lesson10 - Presentation

Linux presentation

Uploaded by

extra superb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Bash Scripting ...

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

echo $((4+2)) #6 addition


echo $((4-2)) #2 subtraction
echo $((4*2)) #8 multiplication
echo $((4/2)) #2 division
echo $((4%2)) #0 modulo
echo $((4**2)) #16 exponents

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=(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)

array=(“One Item” “Two Item”)


"${array[*]}" # “One Item Two Item” - 1 Items
${array[*]} # One Item Two Item – 4 Items

To access the size of the array you can use the #


8
Ex: ${#arrayName[@]}
Bash input redirection from variables
Sometimes you want to redirect standard input from a variable, to
do that use <<<

value="welcome"
cat <<< $value

The output of cat will be welcome

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

sftp [email protected] <<EOF


put file.txt
dir
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

ssh [email protected] <<'EOF'


echo $USER
EOF

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[1m Test"

The output would be Test, it will be bolded


14
Bash Formatting (cont.)
Once you have changed the formatting it does not revert back
automatically
To revert it use 0m

echo -e "\033[1m Test \033[0m"

Changing the colour is just a matter of using a different number

echo -e "\033[32m"

The above will change your terminal colour to green


15
Let's try some more examples

16

You might also like