Skip to content

Commit dbc456a

Browse files
committed
Small updates of scripting chapter.
1 parent cfb2534 commit dbc456a

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

presentation/linux_bash_metacentrum_course.tex

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,21 +3425,38 @@ \subsection{Basic skeleton}
34253425
\item Every script begins with \texttt{\#!/bin/bash} (or alternative for another shells, Perl,~\ldots)
34263426
\item Add any commands you like\ldots
34273427
\item Every script should end with \texttt{exit} (but it is not necessary)
3428-
\item After writing the script, add execution permission (\texttt{chmod +x noninteractive.sh})
3429-
\item Launch with \texttt{./noninteractive.sh}
3428+
\item After writing the script, add execution permission (\texttt{chmod +x noninteractive.sh}), launch with \texttt{./noninteractive.sh}
34303429
\item The most simple script:
34313430
\end{itemize}
34323431
\vfill
34333432
\begin{bashcode}
34343433
#!/bin/bash
3435-
# Simple non-interactive script - no communication with user
3436-
# only list of commands - prints user name, date and $PATH
3434+
# Simple non-interactive script - no communication with user,
3435+
# only list of commands - prints user name, date, $PATH and free space
34373436
echo "Hi, ${USER}, today is $(date) and your PATH is ${PATH}."
3437+
echo "Free space on your disk:"
3438+
df -h
34383439
echo
34393440
exit
34403441
\end{bashcode}
34413442
\end{frame}
34423443
3444+
\subsection{BASH variables}
3445+
3446+
\begin{frame}{Special BASH internal variables available in the script (selection)}
3447+
\begin{itemize}
3448+
\item These variables can be used within script e.g. to parse arguments provided by the user
3449+
\item \alert{\texttt{\$1}},~\ldots~(number from \texttt{1} up to number of parameters) --- individual positional parameters (see further examples)
3450+
\item \alert{\texttt{\$0}} --- path of the starting script
3451+
\item \alert{\texttt{\$\#}} --- number of command-line arguments
3452+
\item \alert{\texttt{\$*}} --- all of the positional parameters, seen as a~single word, must be quoted (i.e. \texttt{"\$*"})
3453+
\item \alert{\texttt{\$@}} --- same as \texttt{\$*}, but each parameter is a~quoted string --- the parameters are passed intact, without interpretation or expansion, each parameter in the argument list is seen as a~separate word, should be quoted (i.e. something like \texttt{"\$@"})
3454+
\item \alert{\texttt{\$\$}} --- process ID (PID) of the script itself
3455+
\item \alert{\texttt{\$?}} --- Exit status of previous command, function, or the script itself
3456+
\item \href{https://tldp.org/LDP/abs/html/internalvariables.html}{See more variables\ldots}
3457+
\end{itemize}
3458+
\end{frame}
3459+
34433460
\subsection{Functions}
34443461
34453462
\begin{frame}[fragile]{Functions in BASH}{Pieces of code, which can be used repeatedly}
@@ -3463,22 +3480,6 @@ \subsection{Functions}
34633480
\end{bashcode}
34643481
\end{frame}
34653482
3466-
\subsection{BASH variables}
3467-
3468-
\begin{frame}{Special BASH internal variables available in the script (selection)}
3469-
\begin{itemize}
3470-
\item These variables can be used within script e.g. to parse arguments provided by the user
3471-
\item \alert{\texttt{\$1}},~\ldots~(number from \texttt{1} up to number of parameters) --- individual positional parameters (see further examples)
3472-
\item \alert{\texttt{\$0}} --- path of the starting script
3473-
\item \alert{\texttt{\$\#}} --- number of command-line arguments
3474-
\item \alert{\texttt{\$*}} --- all of the positional parameters, seen as a~single word, must be quoted (i.e. \texttt{"\$*"})
3475-
\item \alert{\texttt{\$@}} --- same as \texttt{\$*}, but each parameter is a~quoted string --- the parameters are passed intact, without interpretation or expansion, each parameter in the argument list is seen as a~separate word, should be quoted (i.e. something like \texttt{"\$@"})
3476-
\item \alert{\texttt{\$\$}} --- process ID (PID) of the script itself
3477-
\item \alert{\texttt{\$?}} --- Exit status of previous command, function, or the script itself
3478-
\item \href{https://tldp.org/LDP/abs/html/internalvariables.html}{See more variables\ldots}
3479-
\end{itemize}
3480-
\end{frame}
3481-
34823483
\subsection{Reading variables}
34833484
34843485
\begin{frame}{It is important to check user input\ldots}
@@ -3902,7 +3903,7 @@ \subsection{Branching the code}
39023903
commands2
39033904
else
39043905
commands3
3905-
fi
3906+
fi # Branching can be long, hard to read and susceptible to errors
39063907
\end{bashcode}
39073908
\end{frame}
39083909
@@ -3923,29 +3924,32 @@ \subsection{Branching the code}
39233924
\item Double-bracket syntax --- enhanced (now preferred)
39243925
\begin{itemize}
39253926
\item Allow usage of regular expressions and globing patterns
3926-
\item Word splitting is prevented --- \texttt{\textbraceleft \$STRINGVAR} can contain spaces
3927+
\item Word splitting is prevented --- \texttt{\$\textbraceleft STRINGVAR\textbraceright} can contain spaces etc.
39273928
\item Expanding file names --- \texttt{if [[ -a *.sh ]]} (variant with only one bracket doesn't work when there are multiple sh files)
39283929
\item More detailed test, e.g. \texttt{if [[ \$num -eq 3 \&\& "\$\textbraceleft STRINGVAR\textbraceright " == XXX ]]\ldots}
39293930
\end{itemize}
39303931
\end{itemize}
3931-
\item \texttt{-eq} --- Equal to (like \texttt{==})
3932-
\item \texttt{-lt} --- Less than
3933-
\item \texttt{-gt} --- Greater than
3934-
\item \texttt{-ge} --- Greater than or equal to
3935-
\item \texttt{-le} --- Less than or equal to
3936-
\item \texttt{-f "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is a~regular file (not link or so)
3937-
\item \texttt{-r "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is readable
3938-
\item \texttt{-w "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is writable
3939-
\item \texttt{-x "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is executable
3940-
\item \texttt{-d "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is a~directory
3941-
\item \texttt{-s "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and has a~size greater than zero
3942-
\item \texttt{-n "\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is not a~null (empty) string
3943-
\item \texttt{-z "\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is a~null string
3944-
\item \texttt{"\$\textbraceleft STR1\textbraceright " == "\$\textbraceleft STR2\textbraceright "} --- True if both strings are equal
3945-
\item \texttt{"\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is assigned a~value and is not null
3946-
\item \texttt{"\$\textbraceleft STR1\textbraceright " != "\$\textbraceleft STR2\textbraceright "} --- True if both strings are unequal
3947-
\item \texttt{-a} --- Performs the \texttt{AND} function (\texttt{[~\ldots~-a~\ldots~]} or \texttt{[~\ldots~] \&\& [~\ldots~]})
3948-
\item \texttt{-o} --- Performs the \texttt{OR} function (\texttt{[~\ldots~-o~\ldots~]} or \texttt{[~\ldots~] || [~\ldots~]})
3932+
\item Common tests
3933+
\begin{itemize}
3934+
\item \texttt{-eq} --- Equal to (like \texttt{==})
3935+
\item \texttt{-lt} --- Less than
3936+
\item \texttt{-gt} --- Greater than
3937+
\item \texttt{-ge} --- Greater than or equal to
3938+
\item \texttt{-le} --- Less than or equal to
3939+
\item \texttt{-f "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is a~regular file (not link or so)
3940+
\item \texttt{-r "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is readable
3941+
\item \texttt{-w "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is writable
3942+
\item \texttt{-x "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is executable
3943+
\item \texttt{-d "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and is a~directory
3944+
\item \texttt{-s "\$\textbraceleft FILE\textbraceright "} --- True if \texttt{\$FILE} exists and has a~size greater than zero
3945+
\item \texttt{-n "\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is not a~null (empty) string
3946+
\item \texttt{-z "\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is a~null string
3947+
\item \texttt{"\$\textbraceleft STR1\textbraceright " == "\$\textbraceleft STR2\textbraceright "} --- True if both strings are equal
3948+
\item \texttt{"\$\textbraceleft STR\textbraceright "} --- True if string \texttt{\$STR} is assigned a~value and is not null
3949+
\item \texttt{"\$\textbraceleft STR1\textbraceright " != "\$\textbraceleft STR2\textbraceright "} --- True if both strings are unequal
3950+
\item \texttt{-a} --- Performs the \texttt{AND} function (\texttt{[~\ldots~-a~\ldots~]} or \texttt{[~\ldots~] \&\& [~\ldots~]})
3951+
\item \texttt{-o} --- Performs the \texttt{OR} function (\texttt{[~\ldots~-o~\ldots~]} or \texttt{[~\ldots~] || [~\ldots~]})
3952+
\end{itemize}
39493953
\item \texttt{if} statement can test whatever returning \texttt{TRUE}/\texttt{FALSE}, commonly something like \texttt{if grep -q XXX; then \ldots} (see also further)
39503954
\item Do not confuse globing patterns and regular expressions when using \texttt{[[~\ldots~]]}
39513955
\begin{itemize}

scripts_data/noninteractive.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/bin/bash
22

3-
# Simple non-interactive script - no communication with user only list of commands - prints user name, date and $PATH
3+
# Simple non-interactive script - no communication with user, only list of commands - prints user name, date, $PATH and free space
44

55
echo "Hi, ${USER}, today is $(date) and your PATH is ${PATH}."
66

7+
echo "Free space on your disk:"
8+
df -h
9+
710
echo
811

912
exit

0 commit comments

Comments
 (0)