You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: presentation/linux_bash_metacentrum_course.tex
+78-40Lines changed: 78 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -1808,9 +1808,9 @@ \subsection{Editors}
1808
1808
\begin{itemize}
1809
1809
\item Modes of \texttt{vim}:
1810
1810
\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{:}'').
1814
1814
\end{enumerate}
1815
1815
\item For more information see \url{http://www.vim.org/} and \url{http://vim.wikia.com/wiki/Vim_Tips_Wiki}
1816
1816
\item In Czech \url{http://www.nti.tul.cz/~satrapa/docs/vim/}
\item Every script begins with \texttt{\#!/bin/bash} (or alternative for another shells, Perl, \ldots)
@@ -1879,17 +1881,21 @@ \section{Scripting}
1879
1881
\end{bashcode}
1880
1882
\end{frame}
1881
1883
1884
+
\subsection{Reading variables}
1885
+
1882
1886
\begin{frame}[fragile]{Script reading two variables}
1883
1887
\begin{bashcode}
1884
1888
#!/bin/bash
1885
1889
# Arguments are read from command line as parameters of the script
1886
1890
# 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`."
1888
1892
# "$#" is available every time and contains number of parameters
1889
1893
# (variables) given to the script
1890
-
echo "Number of parameters is $#."
1894
+
echo "Number of parameters is $#."
1891
1895
# "$*" is available every time and contains all supplied parameters
1892
1896
echo "Those parameters were supplied: $*."
1897
+
# "$0" is available every time and contains script path
1898
+
echo "Path to the scrip is: \"$0\"."
1893
1899
echo
1894
1900
exit
1895
1901
\end{bashcode}
@@ -1900,7 +1906,9 @@ \section{Scripting}
1900
1906
chmod +x interactive1.sh
1901
1907
./interactive1.sh 89 # Or select any other two numbers
1902
1908
\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}
1904
1912
\end{frame}
1905
1913
1906
1914
\begin{frame}[fragile]{Variables will be interactively provided by the user}
@@ -1922,15 +1930,20 @@ \section{Scripting}
1922
1930
chmod +x interactive2.sh
1923
1931
./interactive1.sh # Values will be provided when script asks
1924
1932
\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}
1926
1939
\end{frame}
1927
1940
1928
1941
\begin{frame}[fragile]{Provide named parameters}
1929
1942
\begin{bashcode}
1930
1943
#!/bin/bash
1931
1944
# Script has only one parameter ($1) provided as its parameter
1932
1945
case "$1" in # evaluating provided parameter and behaving accordingly
1933
-
-d|--disk)
1946
+
-d|--disk) # "|" means alternatives - more possible inputs
1934
1947
echo "Your disk usage is:"
1935
1948
df -h
1936
1949
;;
@@ -1940,10 +1953,10 @@ \section{Scripting}
1940
1953
;;
1941
1954
# This should be every time last possibility - any other input
1942
1955
*) # 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
1947
1960
esac
1948
1961
exit
1949
1962
\end{bashcode}
@@ -1952,30 +1965,53 @@ \section{Scripting}
1952
1965
\begin{frame}{Notes to previous script}
1953
1966
\begin{itemize}
1954
1967
\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
1956
1969
\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
-
\itemHaving 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
1958
1971
\end{itemize}
1959
1972
\end{frame}
1960
1973
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
+
1961
1998
\begin{frame}[fragile]{Provide parameters, verify them and behave accordingly I}
0 commit comments