Skip to content

Commit 38b9d49

Browse files
committed
Updates
1 parent 16596b9 commit 38b9d49

File tree

6 files changed

+120
-70
lines changed

6 files changed

+120
-70
lines changed

presentation/linux_bash_metacentrum_course.tex

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,9 +1808,9 @@ \subsection{Editors}
18081808
\begin{itemize}
18091809
\item Modes of \texttt{vim}:
18101810
\begin{enumerate}
1811-
\item ``Normal'' -- nothing is displayed in bottom left corner, every key has some meaning (\texttt{dd} to cut current line, \texttt{r} to replace character below cursor,\texttt{v} for selection of text, \texttt{y} to copy, \texttt{x} to cut, \texttt{p} to paste, \texttt{i} or \texttt{Insert} key to enter insert mode, \texttt{:} to enter command mode, number to get to line of particular line number, \texttt{u} to undo last change(s), \ldots)
1812-
\item Insert -- in bottom left corner ``-{-} INSERT {-}-'' is displayed -- the most familiar mode -- normal typing etc., exit to normal mode by \texttt{ESC} key
1813-
\item Command -- in bottom left corner ``\texttt{:}'' is displayed -- awaits commands, e.g. \texttt{w} to write file, \texttt{q} to quit, \texttt{q!} to quit and discard changes, \%s/... to search and replace as in \texttt{sed}, \texttt{syntax on/off} to turn syntax highlight on/off, \texttt{/} to search, \ldots Exit to command mode by \texttt{Backspace} key (delete ``\texttt{:}'').
1811+
\item ``Normal'' -- nothing is displayed in bottom left corner, every key has some meaning (\texttt{dd} to cut current line, \texttt{r} to replace character below cursor, \texttt{v} for selection of text, \texttt{y} to copy, \texttt{x} to cut, \texttt{p} to paste, \texttt{i} or \texttt{Insert} key to enter insert mode, \texttt{:} to enter command mode, number to get to line of particular line number, \texttt{u} to undo last change(s), \ldots)
1812+
\item Insert -- in bottom left corner \texttt{-{-} INSERT {-}-} is displayed -- the most familiar mode -- normal typing etc., exit to normal mode by \texttt{ESC} key
1813+
\item Command -- in bottom left corner \texttt{:} is displayed -- awaits commands, e.g. \texttt{w} to write file, \texttt{q} to quit, \texttt{q!} to quit and discard changes, \%s/... to search and replace as in \texttt{sed}, \texttt{syntax on/off} to turn syntax highlight on/off, \texttt{/} to search, \ldots Exit to command mode by \texttt{Backspace} key (delete ``\texttt{:}'').
18141814
\end{enumerate}
18151815
\item For more information see \url{http://www.vim.org/} and \url{http://vim.wikia.com/wiki/Vim_Tips_Wiki}
18161816
\item In Czech \url{http://www.nti.tul.cz/~satrapa/docs/vim/}
@@ -1824,7 +1824,7 @@ \subsection{Regular expressions}
18241824
\begin{itemize}
18251825
\item \alert{.} -- any single character
18261826
\item \alert{*} -- any number of characters/occurrences of pattern (including 0)
1827-
\item \alert{[\ldots]} -- any one character in the brackets
1827+
\item \alert{[\ldots]} -- any character in the brackets
18281828
\item \alert{[\textasciicircum\ldots]} -- reverse case -- all characters except newline and those listed in brackets
18291829
\item \alert{\textasciicircum} -- first character of reg exp -- beginning of the line
18301830
\item \alert{\$} -- last character of reg exp -- end of the line
@@ -1861,6 +1861,8 @@ \subsection{Regular expressions}
18611861
18621862
\section{Scripting}
18631863
1864+
\subsection{Basic skeleton}
1865+
18641866
\begin{frame}[fragile]{Basic script}
18651867
\begin{itemize}
18661868
\item Every script begins with \texttt{\#!/bin/bash} (or alternative for another shells, Perl, \ldots)
@@ -1879,17 +1881,21 @@ \section{Scripting}
18791881
\end{bashcode}
18801882
\end{frame}
18811883
1884+
\subsection{Reading variables}
1885+
18821886
\begin{frame}[fragile]{Script reading two variables}
18831887
\begin{bashcode}
18841888
#!/bin/bash
18851889
# Arguments are read from command line as parameters of the script
18861890
# Order has to be kept (well, not in this case, but generally yes)
1887-
echo "Sum of two numbers $1 and $2 is `expr $1 + $2` ."
1891+
echo "Sum of two numbers $1 and $2 is `expr $1 + $2`."
18881892
# "$#" is available every time and contains number of parameters
18891893
# (variables) given to the script
1890-
echo "Number of parameters is $# ."
1894+
echo "Number of parameters is $#."
18911895
# "$*" is available every time and contains all supplied parameters
18921896
echo "Those parameters were supplied: $*."
1897+
# "$0" is available every time and contains script path
1898+
echo "Path to the scrip is: \"$0\"."
18931899
echo
18941900
exit
18951901
\end{bashcode}
@@ -1900,7 +1906,9 @@ \section{Scripting}
19001906
chmod +x interactive1.sh
19011907
./interactive1.sh 8 9 # Or select any other two numbers
19021908
\end{bashcode}
1903-
There is no checking of input values, nothing advanced, \ldots
1909+
\begin{itemize}
1910+
\item There is no checking of input values, nothing advanced, \ldots
1911+
\end{itemize}
19041912
\end{frame}
19051913
19061914
\begin{frame}[fragile]{Variables will be interactively provided by the user}
@@ -1922,15 +1930,20 @@ \section{Scripting}
19221930
chmod +x interactive2.sh
19231931
./interactive1.sh # Values will be provided when script asks
19241932
\end{bashcode}
1925-
There is no checking of input values, nothing advanced, \ldots
1933+
\begin{itemize}
1934+
\item There is no checking of input values, nothing advanced, \ldots
1935+
\end{itemize}
1936+
\begin{bashcode}
1937+
$(expr $1 + $2) # Alternative - $(...) is same as `...`
1938+
\end{bashcode}
19261939
\end{frame}
19271940
19281941
\begin{frame}[fragile]{Provide named parameters}
19291942
\begin{bashcode}
19301943
#!/bin/bash
19311944
# Script has only one parameter ($1) provided as its parameter
19321945
case "$1" in # evaluating provided parameter and behaving accordingly
1933-
-d|--disk)
1946+
-d|--disk) # "|" means alternatives - more possible inputs
19341947
echo "Your disk usage is:"
19351948
df -h
19361949
;;
@@ -1940,10 +1953,10 @@ \section{Scripting}
19401953
;;
19411954
# This should be every time last possibility - any other input
19421955
*) # User is then notified he entered nonsense and gets some help
1943-
echo "Wrong option!
1944-
Usage: -d or --disk for available disk space or
1945-
-u or --uptime for computer uptime"
1946-
;;
1956+
echo "Wrong option!"
1957+
echo "Usage: -d or --disk for available disk space or"
1958+
echo "-u or --uptime for computer uptime"
1959+
exit 1;; # In this case, exit with error code 1
19471960
esac
19481961
exit
19491962
\end{bashcode}
@@ -1952,30 +1965,53 @@ \section{Scripting}
19521965
\begin{frame}{Notes to previous script}
19531966
\begin{itemize}
19541967
\item First make \texttt{interactive3.sh} executable and launch it via e.g. \texttt{./interactive3.sh -d} or \texttt{./interactive3.sh -{-}uptime} or so
1955-
\item Function \texttt{case} has basic checking of input available -- as last parameter use ``\alert{*)}'' -- any other input except those defined will produce some warning message or so
1968+
\item Function \texttt{case} has basic checking of input available -- as last parameter use \texttt{*)} -- any other input except those defined above will produce some warning message, error or so
19561969
\item In same way can be added more parameters (by multiple use of \texttt{case}), but order of parameters must be kept and all parameters are compulsory
1957-
\item Having variable number of parameters, possibility to use only some of them and variable order is more complicated and it usually requires \texttt{case} in \texttt{while} loop and reading variable into an array
1970+
\item \texttt{case} can evaluate simple regular expressions, e.g. \texttt{--[Uu]ptime)}, \texttt{-d*}, \ldots
19581971
\end{itemize}
19591972
\end{frame}
19601973
1974+
\begin{frame}[fragile]{Functions in BASH}
1975+
\begin{itemize}
1976+
\item Pieces of code, which can be used repeatedly
1977+
\end{itemize}
1978+
\begin{bashcode}
1979+
# Declare new function
1980+
function MyNewFunction1 {
1981+
echo "Hello, $USER on $HOSTNAME!"
1982+
}
1983+
# Use it in a script as any other command:
1984+
...
1985+
MyNewFunction1
1986+
...
1987+
# Use with variables (same as in previous cases)
1988+
function MyNewFunction2 {
1989+
echo "The sum is `expr $1 + $2`."
1990+
}
1991+
# Use it in the script
1992+
...
1993+
MyNewFunction2 5 8 # For example
1994+
...
1995+
\end{bashcode}
1996+
\end{frame}
1997+
19611998
\begin{frame}[fragile]{Provide parameters, verify them and behave accordingly I}
19621999
\begin{bashcode}
19632000
#!/bin/bash
1964-
NUMBER='^[0-9]+$'
1965-
if [ "$#" -ne "3" ]; then
1966-
echo "Error! Requiring 3 parameters! Received $#.
1967-
Usage number1 -plus/-minus/-product/-quotient number2
1968-
Use -plus for sum, -minus for difference, -product
1969-
for multiplication or -quotient for quotient."
1970-
exit 1
2001+
NUMBER='^[0-9]+$' # From beginning (^) to the end ($) only number(s)
2002+
function usagehelp { # Function to print help
2003+
echo "Usage: number1 plus/minus/product/quotient number2"
2004+
echo "Use plus for sum, minus for difference, product"
2005+
echo "for multiplication or quotient for quotient."
2006+
exit 1
2007+
}
2008+
if [ "$#" -ne "3" ]; then # Do we have 3 parameters provided?
2009+
echo "Error! Requiring 3 parameters! Received $# ($*)."
2010+
usagehelp # The function to print help
19712011
fi
1972-
if [[ ! $1 =~ $NUMBER ]]; then
2012+
if [[ ! $1 =~ $NUMBER ]]; then # Is parameter 1 number?
19732013
echo "Parameter 1 is not an integer!"
1974-
exit 1
1975-
fi
1976-
if [[ ! $3 =~ $NUMBER ]]; then
1977-
echo "Parameter 3 is not an integer!"
1978-
exit 1
2014+
usagehelp # The function to print help
19792015
fi
19802016
\end{bashcode}
19812017
Continues on next slide\ldots
@@ -1984,16 +2020,18 @@ \section{Scripting}
19842020
\begin{frame}[fragile]{Provide parameters, verify them and behave accordingly II}
19852021
Remaining part from previous slide\ldots
19862022
\begin{bashcode}
1987-
case $2 in
1988-
-plus) expr $1 '+' $3;;
1989-
-minus) expr $1 '-' $3;;
1990-
-product) expr $1 '*' $3;;
1991-
-quotient) expr $1 '/' $3;;
1992-
*) echo "Wrong option!
1993-
Usage number1 -plus/-minus/-product/-quotient number2
1994-
Use -plus for sum, -minus for difference,
1995-
-product for multiplication or -quotient
1996-
for quotient.";;
2023+
if [[ ! $3 =~ $NUMBER ]]; then # Is parameter 3 number?
2024+
echo "Parameter 3 is not an integer!"
2025+
usagehelp # The function to print help
2026+
fi
2027+
case "$2" in
2028+
plus) expr $1 '+' $3;;
2029+
minus) expr $1 '-' $3;;
2030+
product) expr $1 '*' $3;;
2031+
quotient) expr $1 '/' $3;;
2032+
*) echo "Wrong option!"
2033+
usagehelp # The function to print help
2034+
;;
19972035
esac
19982036
exit
19992037
\end{bashcode}
@@ -2009,9 +2047,9 @@ \section{Scripting}
20092047
fi
20102048
# Two branches - when condition is met and when not
20112049
if expression; then
2012-
commands1
2050+
commands1 # expression is TRUE
20132051
else
2014-
commands2
2052+
commands2 # expression is FALSE - all other cases
20152053
fi
20162054
# Join together two (or more) if branches
20172055
if expression1; then

scripts/interactive1.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
# Arguments are read from command line as parameters of the script
44
# Order has to be kept (well, not in this case, but generally yes)
55

6-
echo "Sum of two numbers $1 and $2 is `expr $1 + $2` ."
6+
echo "Sum of two numbers $1 and $2 is `expr $1 + $2`."
77

8-
# "$#" is available every time and contains number of parameters
9-
# (variables) given to the script
8+
# "$#" is available every time and contains number of parameters (variables) given to the script
109

11-
echo "Number of parameters is $# ."
10+
echo "Number of parameters is $#."
1211

1312
# "$*" is available every time and contains all supplied parameters
1413

1514
echo "Those parameters were supplied: $*."
1615

16+
# "$0" is available every time and contains script path
17+
18+
echo "Path to the scrip is: \"$0\"."
19+
1720
echo
1821

1922
exit

scripts/interactive2.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ echo "Please, input second value to sum and press Enter"
1010

1111
read V2
1212

13-
echo "Sum of two numbers $V1 and $V2 is `expr $V1 + $V2` ."
13+
echo "Sum of two numbers $V1 and $V2 is `expr $V1 + $V2`."
14+
15+
# $(expr $1 + $2) is an alternative - $(...) is same as `...`
1416

1517
echo
1618

scripts/interactive3.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# "case" is evaluating provided parameter and behaving accordingly
55

66
case "$1" in
7+
# "|" means alternatives - more possible inputs
78
-d|--disk)
89
echo "Your disk usage is:"
910
df -h
@@ -14,11 +15,13 @@ case "$1" in
1415
;;
1516
# This should be every time last possibility - any other input
1617
# User is then notified he entered nonsense and gets some help
17-
*)
18-
echo "Wrong option!
19-
Usage: -d or --disk for available disk space or
20-
-u or --uptime for computer uptime"
21-
;;
18+
*) # Any other input
19+
echo "Wrong option!"
20+
echo "Usage: -d or --disk for available disk space or"
21+
echo "-u or --uptime for computer uptime"
22+
# In this case, exit with error code 1
23+
exit 1
24+
;;
2225
esac
2326

2427
exit

scripts/interactive4.sh

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,41 @@
55
# at least one time (+) up to the end of the line ($)
66
NUMBER='^[0-9]+$'
77

8+
# Function to print help - we will use it twice
9+
function usagehelp {
10+
echo "Usage: number1 plus/minus/product/quotient number2"
11+
echo "Use plus for sum, minus for difference, product"
12+
echo "for multiplication or quotient for quotient."
13+
exit 1
14+
}
15+
16+
# Do we have 3 parameters provided?
817
if [ "$#" -ne "3" ]; then
9-
echo "Error! Requiring 3 parameters! Received $#.
10-
Usage: number1 -plus/-minus/-product/-quotient number2
11-
Use -plus for sum, -minus for difference, -product
12-
for multiplication or -quotient for quotient."
13-
exit 1
18+
echo "Error! Requiring 3 parameters! Received $# ($*)."
19+
usagehelp # The function to print help
1420
fi
1521

1622
# "=~" means we are testing if $1 fits to regular expression in $NUMBER
17-
23+
# Is parameter 1 number?
1824
if [[ ! $1 =~ $NUMBER ]]; then
1925
echo "Parameter 1 is not an integer!"
20-
exit 1
26+
usagehelp # The function to print help
2127
fi
2228

29+
# Is parameter 3 number?
2330
if [[ ! $3 =~ $NUMBER ]]; then
2431
echo "Parameter 3 is not an integer!"
25-
exit 1
32+
usagehelp # The function to print help
2633
fi
2734

28-
case $2 in
29-
-plus) expr $1 '+' $3;;
30-
-minus) expr $1 '-' $3;;
31-
-product) expr $1 '*' $3;;
32-
-quotient) expr $1 '/' $3;;
33-
*) echo "Wrong option!
34-
Usage number1 -plus/-minus/-product/-quotient number2
35-
Use -plus for sum, -minus for difference,
36-
-product for multiplication or -quotient
37-
for quotient.";;
35+
case "$2" in
36+
plus) expr $1 '+' $3;;
37+
minus) expr $1 '-' $3;;
38+
product) expr $1 '*' $3;;
39+
quotient) expr $1 '/' $3;;
40+
*) echo "Wrong option!"
41+
usagehelp # The function to print help
42+
;;
3843
esac
3944

4045
exit

scripts/noninteractive.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

3-
# Simple non-interactive script - no communication with user
4-
# only list of commands
3+
# Simple non-interactive script - no communication with user only list of commands
54

65
echo "Hi, $USER, today is `date` and your PATH is $PATH."
76

0 commit comments

Comments
 (0)