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
\begin{frame}{Special variables available in the script (selection)}
3132
-
\begin{itemize}
3133
-
\item\alert{\texttt{\$1}},~\ldots~(number from \texttt{1} up to number of parameters) --- individual positional parameters (see further for example)
3134
-
\item\alert{\texttt{\$0}} --- path of the starting script
3135
-
\item\alert{\texttt{\$\#}} --- number of command-line arguments
3136
-
\item\alert{\texttt{\$*}} --- all of the positional parameters, seen as a~single word, must be quoted (i.e. \texttt{"\$*"})
3137
-
\item\alert{\texttt{\$@}} --- same as \texttt{\$*}, but each parameter is a~quoted string --- the parameters are passed on 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{"\$@"})
3138
-
\item\alert{\texttt{\$\$}} --- process ID (PID) of the script itself
3139
-
\item\alert{\texttt{\$?}} --- Exit status of previous command, function, or the script itself
3140
-
\item\href{https://www.tldp.org/LDP/abs/html/internalvariables.html}{See more variables\ldots}
3141
-
\end{itemize}
3142
-
\end{frame}
3143
-
3144
3130
\subsection{Functions}
3145
3131
3146
3132
\begin{frame}[fragile]{Functions in BASH}{Pieces of code, which can be used repeatedly}
3147
3133
\begin{bashcode}
3148
-
# Declare new function
3134
+
# Declare new function within script
3149
3135
function MyNewFunction1 {
3150
3136
echo "Hello, $USER from $(groups) on $HOSTNAME!"
3151
3137
}
@@ -3165,11 +3151,27 @@ \subsection{Functions}
3165
3151
\end{bashcode}
3166
3152
\end{frame}
3167
3153
3154
+
\subsection{BASH variables}
3155
+
3156
+
\begin{frame}{Special variables available in the script (selection)}
3157
+
\begin{itemize}
3158
+
\item These variables can be used within script e.g. to parse arguments provided by the user
3159
+
\item\alert{\texttt{\$1}},~\ldots~(number from \texttt{1} up to number of parameters) --- individual positional parameters (see further for example)
3160
+
\item\alert{\texttt{\$0}} --- path of the starting script
3161
+
\item\alert{\texttt{\$\#}} --- number of command-line arguments
3162
+
\item\alert{\texttt{\$*}} --- all of the positional parameters, seen as a~single word, must be quoted (i.e. \texttt{"\$*"})
3163
+
\item\alert{\texttt{\$@}} --- same as \texttt{\$*}, but each parameter is a~quoted string --- the parameters are passed on 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{"\$@"})
3164
+
\item\alert{\texttt{\$\$}} --- process ID (PID) of the script itself
3165
+
\item\alert{\texttt{\$?}} --- Exit status of previous command, function, or the script itself
3166
+
\item\href{https://www.tldp.org/LDP/abs/html/internalvariables.html}{See more variables\ldots}
3167
+
\end{itemize}
3168
+
\end{frame}
3169
+
3168
3170
\subsection{Reading variables}
3169
3171
3170
3172
\begin{frame}{It is important to check user input\ldots}
\item By accident or purpose (attack), user can enter unexpected value
3179
3181
\begin{itemize}
3180
3182
\item In the \enquote{best} case, the script \enquote{just} crashes
3181
-
\item Script can behave unexpectedly, returning very weird results
3183
+
\item Script can behave unexpectedly, returning very weird results, damage data
3182
3184
\item Internal functions/commands can return error messages, which are hard to understand
3183
3185
\item Attacker can e.g. modify web content (\href{https://en.wikipedia.org/wiki/Cross-site_scripting}{XSS},~\ldots), obtain private data, root privileges,~\ldots
\item Single-bracket conditions --- file, string, or arithmetic conditions
3510
3539
\item Double-bracket syntax --- enhanced
3511
3540
\begin{itemize}
3512
3541
\item Allow usage of regular expressions and globing patterns
3513
3542
\item Word splitting is prevented --- \texttt{\$STRINGVAR} can contain spaces
3514
-
\item Expanding file names --- \texttt{if [[ -a *.sh ]]} (variant with only one bracket doesn't work when there are multiple sh files)
3515
-
\item Allows more detailed test, e.g. \texttt{if [[ \$num -eq 3 \&\&"\$STRINGVAR" == XXX ]] \ldots}
3543
+
\item Expanding file names --- \texttt{if [[} -\texttt{a *.sh ]]} (variant with only one bracket doesn't work when there are multiple sh files)
3544
+
\item Allows more detailed test, e.g. \texttt{if [[ \$num} -\texttt{eq 3 \&\&"\$STRINGVAR" == XXX ]] \ldots}
3516
3545
\end{itemize}
3517
3546
\end{itemize}
3518
3547
\item -\texttt{eq} --- Equal to
@@ -3531,8 +3560,8 @@ \subsection{Branching the code}
3531
3560
\item\texttt{\$STR1 == \$STR2} --- True if both strings are equal
3532
3561
\item\texttt{\$STR} --- True if string \texttt{\$STR} is assigned a~value and is not null
3533
3562
\item\texttt{\$STR1 != \$STR2} --- True if both strings are unequal
3534
-
\item -\texttt{a} --- Performs the \texttt{AND} function (\texttt{[ \ldots -a \ldots~]} or \texttt{[ \ldots~] \&\& [ \ldots~]})
3535
-
\item -\texttt{o} --- Performs the \texttt{OR} function (\texttt{[ \ldots -o \ldots~]} or \texttt{[ \ldots~] || [ \ldots~]})
3563
+
\item -\texttt{a} --- Performs the \texttt{AND} function (\texttt{[ \ldots} -\texttt{a \ldots~]} or \texttt{[ \ldots~] \&\& [ \ldots~]})
3564
+
\item -\texttt{o} --- Performs the \texttt{OR} function (\texttt{[ \ldots} -\texttt{o \ldots~]} or \texttt{[ \ldots~] || [ \ldots~]})
3536
3565
\item Do not confuse globing patterns and regular expressions when using \texttt{[[ \ldots~]]}
3537
3566
\begin{itemize}
3538
3567
\item Shell globing: \texttt{if [[ "\$STRINGVAR"\textbf{==} ?[sS]tring* ]]; then} --- \texttt{?} represents single character \texttt{[]} any character inside and \texttt{*} zero or more characters
@@ -3548,6 +3577,7 @@ \subsection{Branching the code}
3548
3577
\item RAxML binaries must be in \texttt{\$PATH}
3549
3578
\item See \texttt{raxml\_if.sh} for whole script
3550
3579
\end{itemize}
3580
+
\vfill
3551
3581
\begin{bashcode}
3552
3582
if grep -iq avx2 /proc/cpuinfo; then # Does the CPU support AVX2?
3553
3583
RAXML='raxmlHPC-AVX2' # Select appropriate binary
@@ -3567,6 +3597,7 @@ \subsection{Branching the code}
3567
3597
\item Same task as on previous slide, but instead of if-then branching it is using \texttt{case}
3568
3598
\item See \texttt{raxml\_case.sh} for whole script
3569
3599
\end{itemize}
3600
+
\vfill
3570
3601
\begin{bashcode}
3571
3602
# Determine which CPU is available and which binary use then
3572
3603
CPUFLAGS=$(grep -i flags /proc/cpuinfo | uniq)
@@ -3597,7 +3628,7 @@ \subsection{Loops}
3597
3628
for JPGF in *.jpg; do convert $JPGF -resize 100x100 thumbs-$JPGF; done
3598
3629
# More commands in a block
3599
3630
for JPGF in $(ls -1 *.jpg); do
3600
-
echo "Processing JPGF $JPGF"
3631
+
echo "Processing JPG $JPGF"
3601
3632
convert $JPGF -resize 100x100 thumbs-$JPGF
3602
3633
echo "File thumbs-$file created"
3603
3634
done
@@ -3614,8 +3645,7 @@ \subsection{Loops}
3614
3645
\begin{bashcode}
3615
3646
# while cycle is evaluating condition and if it is equal to 0 (TRUE)
3616
3647
# the cycle body is launched, repeatedly while the condition is met
3617
-
while condition
3618
-
do
3648
+
while condition; do
3619
3649
commands
3620
3650
done
3621
3651
# Like while cycle, but until condition is not equal to zero
@@ -3627,7 +3657,7 @@ \subsection{Loops}
3627
3657
if [condition]; then # If something happens
3628
3658
break; fi # End up the cycles and continue by following commands
3629
3659
while read TEXTLINE; do # Run cycles on text file
3630
-
commands...
3660
+
commands... # TEXTLINE contains in each cycle one line of the file
3631
3661
done < text_file_to_process.txt
3632
3662
while :; do echo "Press CTRL+C to exit..."; done # Infinite loop
3633
3663
for (( ; ; )) ; do echo "Press CTRL+C to exit..."; done # Infinite loop
0 commit comments