# [Terminal] hellothere.sh
1. Basics
echo $SHELL
: The shell?
vim shelltest.sh
: Open shelltest.sh in Vim
#! /bin/bash
: Place in front of bash script to tell Linux which Shell to run
ls -l
: long format list
chmod u+x shelltest.sh
: user to have executable rights on shell.sh man wc
+ arrows
+ q
: manual of word-count script
wc --help
: quick reference of word-count script ${1,,}
: Parameter Expansion - Ignores lower and upper-cases when comparing to values
2. Variables
FIRST_NAME=tony
: set a variable
echo FIRST_NAME
: echo the variable’
\'
: escape the single quote with backtick
2.1 Fixed Variables
#!/bin/bash
=Tony
FIRST_NAME=JustDevs
LAST_NAME echo Hello $FIRST_NAME $LAST_NAME
+x hellothere.sh # [Terminal]
cbmod u/hellothere.sh # [Terminal] .
2.2 Interactive Variables
# [Terminal] hellothere_interactive.sh
#/bin/bash
is your first name?
echo What
read FIRST_NAMEis your last name?
echo What
read LAST_NAME
echo Hello $FIRST_NAME $LAST_NAME
+x hellothere_interactive.sh # [Terminal]
cbmod u/hellothere_interactive.sh # [Terminal] .
2.3 Positional Arguments
# [Terminal] vim hellothere_posarg.sh
#!/bin/bash
1 $2 echo Hello $
+x hellothere_posargs.sh # [Terminal]
cbmod u/hellothere_posargs.sh Tony JustDevs # [Terminal] .
3. Output redirection
3.1 Piping |
- The output of the previous command
ls -l /usr/bin
is forwarded to the command after the|
. - Add
grep bash
to filter for bash.
-l /usr/bin | grep bash # [Terminal] ls
3.2 Override >
E.g. Logging something from a script to a log-file: 1. Catch output from echo command
2. Override of a text file
! > output_override_to_text.txt
echo Hello World cat output_override_to_text.txt
3.3 Append >>
Append from script to a log-file: 1. Catch output from echo command
2. Append output into a text file
! > output_append_to_text.txt
echo Hello World
cat output_override_to_text.txt! > output_append_to_text.txt
echo Good day matey cat output_override_to_text.txt
4. Input direction
<
: from a file<<
: from multiple lines of text<<<
: from single string of text
4.1 <
from a line
Use word-count wc
command to for number of words in text 1. Command to receive Input 2. Input direction of text
-w < output_append_to_text.txt # input direction
wc | wc -w # output direction cat output_append_to_text.txt
4.2 <<
from multiple lines
Supply multiple lines of words 1. Command to receive Input 2. Input direction to Command 3. KeywordStart 4. Actual text 5. KeywordEnd
<< EOF
cat is some text
this with multiple
lines EOF
4.3 <<<
from single line
-w <<< "a sentence line with 6 words" wc
5. Test Operators
5.1 Equality
Tests whether an express exists with 0
(No issues) or 1
(Error) 1. Write expression 2. Print exit-code of last executed command
= hello ]
[ hello # exits 0
echo $?
1 = 0 ]
[ # exits 1
echo $?
1 -eq 1 ] # equate numericals
[ echo $?
5.2 If / Elif / Else
${1,,}
Parameter Expansion: Ignores lower and upper-cases when comparing to values
#/bin/bash
if [ ${1,,} = tonydevs ]; then
"Oh, you're the boss here. Welcome!"
echo elif [ ${1,,} = help]; then
"Just enter your username, duh!"
echo else
"I don't know who you are. But you're not the boss of me!"
echo
fi~
5.3 Case statements
Better than if / elif /else: - Checking for multiple values - is easier to read
#[Terminal] vim case_stmts.sh
#!/bin/bash
1,,} in
case ${| administrator)
tony "Gday, you're the boss here!"
echo ;;
help)
"Just enter your username!"
echo ;;
*)
"Hello there, you're not the boss of me. Enter a valid username!"
echo esac
//www.youtube.com/watch?v=tK9Oc6AEnR4 https:
6. Arrays
Store multiple variables in a list called Arrays
6.1 Indexing
=(one two three four five)
MY_FIRST_LIST# print only first element [TERMINAL]
echo $MY_FIRST_LIST @]} # prints everything
echo ${MY_FIRST_LIST[1]} # prints second element echo ${MY_FIRST_LIST[
6.2 Indexing
item
: each element in loop
${MY_FIRST_LIST[@]}
: all items in list
do echo -n
: do echo and ignore all new line characters
$item
: represents each single item in array
|
: output direction
wc -c
: count characters
done
: finish loop
for item in ${MY_FIRST_LIST[@]}; do echo -n $item | wc -c; done
7. Functions
7.1 Function only
- Create shell
- Define function
- Catch output for
up
andsince
with their different flags - Print everything between the two
EOF
s keywords - Call the variables generated
- Close function
# [Terminal] vim first_function.sh
#!/bin/bash
showuptime(){=$(uptime -p | cut -c4-)
up=$(uptime -s)
since<< EOF
cat ------
for ${up}
This machine has been up
It has been running since ${since}------
EOF
} showuptime
7.2 Not Declaring Local Variables (Wrong!)
If variables inside a function are not declared local
, they may override variables of the same name in the global
variables in the global environment/
#!/bin/bash
="global up" # add global variable 1
up="global since" # add global variable 2
since
echo $up
echo $since
showuptime(){=$(uptime -p | cut -c4-)
up=$(uptime -s)
since<< EOF
cat ------
for ${up}
This machine has been up
It has been running since ${since}------
EOF
}
showuptimeis: $up
echo up is: $since echo since
7.3 Declaring Local Variables in a Function
Define variables inside functions as local variables so they’re only available to the functions and not to the entire script.
#!/bin/bash
="global up"
up="global since"
since
echo $up
echo $since
showuptime(){=$(uptime -p | cut -c4-) # add local prefix to declare local variable 1
local up=$(uptime -s) # add local prefix to declare local variable 2
local since<< EOF
cat ------
for ${up}
This machine has been up
It has been running since ${since}------
EOF
}
showuptimeis: $up
echo up is: $since echo since
7.4 Position Arguments
Just like shell scripts, shell functions can also have positional arguments
#!/bin/bash
showname(){1 $2
echo hello $
} showname Tony JustDevs
8. Exit Codes
#!/bin/bash
showname(){1
echo hello $if [ ${1,,} = tony ]; then
return 0
else
return 1
fi
}1
showname() $if [ $? = 1 ]; then
= "A strange has called the function!"
echo fi
9. awk
Filter contents to and fro: 1. files or 2. output of a command
9.1 Filter a Text File
> onetwothree.txt #[Terminal]
echo one two three '{print $1}' onetwothree.txt awk
9.2 Filter a CSV File
vim csv_test.csv #[csv_test.csv]
one,two,three -F, '{print $1 $2}'
awk
9.3 Piping into awk
"Just get this world: Hello" | awk '{print $5}'
echo "Just get this world: Hello" | awk -F: '{print $2}' | cut -c2 echo
10. sed
Replace values in text files with Regular Expressions
Example: sed 's/word1/word2/g' sedtest.txt
sed
: replace values command
s
: means subtsitute
g
: globally, across the whole text file -i.ORIGINAL
: keeps original file appends .ORIGINAL
to file name
# [terminal]
vim sed_test.txt
# [sed_test.txt]
The fly flies like no fly flies. is an insect that has wings and a fly likes to eat leftovers
A fly
# Just prints into terminal
's/fly/grasshopper/g' sedtest.txt
sed
# replace og file with command + creates new file .txt.ORIGINAL of og content
-i.ORIGINAL 's/fly/grasshopper/g' sedtest.txt
sed