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
\item Script can behave unexpectedly, returning very weird results, damage data
3362
3362
\item Internal functions/commands can return error messages, which are hard to understand
3363
3363
\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
3364
+
\item Applies also to scientific data --- wrong input use to have unexpected outcomes\ldots
3364
3365
\end{itemize}
3365
3366
\item Programmer should always check if user input is correct, filter it
# the beginning of the line to overwrite the number in next step
3644
3671
sleep 1s # Wait 1 second - just for fun ;-)
3645
3672
# Do the task - append input to the output - note usage of variables
3646
-
cat "$INPUTFILE" >> "$OUTPUTFILE" # Do the task - append inp to output
3673
+
cat "$INPUTFILE" >> "$OUTPUTFILE" # Do the task - append into to output
3647
3674
done
3648
3675
echo -ne "\n" # Reset cursor to new line
3649
3676
echo "Done!"
3650
3677
exit
3651
3678
\end{bashcode}
3652
-
\vfill
3653
-
\begin{itemize}
3654
-
\item Script \texttt{interactive5.sh} contains complete example
3655
-
\end{itemize}
3656
3679
\end{frame}
3657
3680
3658
3681
\begin{frame}[fragile]{Multiple switches in classical UNIX form (no positional) V}
3682
+
\begin{itemize}
3683
+
\item Script \texttt{interactive5.sh} contains complete example
3684
+
\item This is the classical way how to use UNIX switches used in most of commands
3685
+
\item\texttt{while} loop encapsulating \texttt{case} ensures we evaluate all provided parameters regardless their number or order
3686
+
\begin{itemize}
3687
+
\item Be prepared that user can use the arguments in any combination and orded (e.g. calling \texttt{-h} together with any other switch) --- avoid in code doing several things at once
{ echo "Error! Making backup of $1 failed!"; exit 1; }
3694
3731
echo "Done!"
3695
3732
exit
3696
3733
\end{bashcode}
3697
3734
\vfill
3698
-
\hrule
3735
+
\begin{itemize}
3736
+
\item Common way for simple scripts --- do something with single input file
3737
+
\item Note lines 4 and 5 above --- common way to report success as well as handle failure
3738
+
\end{itemize}
3699
3739
\vfill
3700
3740
\begin{bashcode}
3741
+
# Make it executable
3742
+
chmod +x interactive6.sh
3701
3743
# Use the script with some text file...
3702
3744
./interactive6.sh long_text.txt
3703
3745
\end{bashcode}
@@ -3729,14 +3771,17 @@ \subsection{Branching the code}
3729
3771
3730
3772
\begin{frame}[allowframebreaks]{Evaluation of conditions}
3731
3773
\begin{itemize}
3732
-
\item\enquote{\texttt{[} \ldots~\texttt{]}} (always keep space around it --- inside) is function to evaluate expressions (alternatively use command \texttt{test})
3774
+
\item Basic method to branch code --- do something according to certain condition
3775
+
\item Very versatile, usually there are more options how to write desired conditioning
3776
+
\item Avoid long chaining of conditions using \texttt{elif} statement (previous slide) --- susceptible to mistakes, hard to debug
3777
+
\item\enquote{\texttt{[~\ldots~]}} (always keep space around it --- inside) is function to evaluate expressions (alternatively use command \texttt{test})
\item Single-bracket conditions --- file, string, or arithmetic conditions
3741
3786
\item Double-bracket syntax --- enhanced
3742
3787
\begin{itemize}
@@ -3746,28 +3791,29 @@ \subsection{Branching the code}
3746
3791
\item Allows more detailed test, e.g. \texttt{if [[ \$num -eq 3 \&\&"\$STRINGVAR" == XXX ]] \ldots}
3747
3792
\end{itemize}
3748
3793
\end{itemize}
3749
-
\item-\texttt{eq} --- Equal to
3750
-
\item-\texttt{lt} --- Less than
3751
-
\item-\texttt{gt} --- Greater than
3752
-
\item-\texttt{ge} --- Greater than or equal to
3753
-
\item-\texttt{le} --- Less than or equal to
3754
-
\item-\texttt{f \$FILE} --- True if \texttt{\$FILE} exists and is a~regular file
3755
-
\item-\texttt{r \$FILE} --- True if \texttt{\$FILE} exists and is readable
3756
-
\item-\texttt{w \$FILE} --- True if \texttt{\$FILE} exists and is writable
3757
-
\item-\texttt{x \$FILE} --- True if \texttt{\$FILE} exists and is executable
3758
-
\item-\texttt{d \$FILE} --- True if \texttt{\$FILE} exists and is a~directory
3759
-
\item-\texttt{s \$FILE} --- True if \texttt{\$FILE} exists and has a~size greater than zero
3760
-
\item-\texttt{n \$STR} --- True if string \texttt{\$STR} is not a~null (empty) string
3761
-
\item-\texttt{z \$STR} --- True if string \texttt{\$STR} is a~null string
3794
+
\item\texttt{-eq} --- Equal to (like \texttt{==})
3795
+
\item\texttt{-lt} --- Less than
3796
+
\item\texttt{-gt} --- Greater than
3797
+
\item\texttt{-ge} --- Greater than or equal to
3798
+
\item\texttt{-le} --- Less than or equal to
3799
+
\item\texttt{-f \$FILE} --- True if \texttt{\$FILE} exists and is a~regular file (not link or so)
3800
+
\item\texttt{-r \$FILE} --- True if \texttt{\$FILE} exists and is readable
3801
+
\item\texttt{-w \$FILE} --- True if \texttt{\$FILE} exists and is writable
3802
+
\item\texttt{-x \$FILE} --- True if \texttt{\$FILE} exists and is executable
3803
+
\item\texttt{-d \$FILE} --- True if \texttt{\$FILE} exists and is a~directory
3804
+
\item\texttt{-s \$FILE} --- True if \texttt{\$FILE} exists and has a~size greater than zero
3805
+
\item\texttt{-n \$STR} --- True if string \texttt{\$STR} is not a~null (empty) string
3806
+
\item\texttt{-z \$STR} --- True if string \texttt{\$STR} is a~null string
3762
3807
\item\texttt{\$STR1 == \$STR2} --- True if both strings are equal
3763
3808
\item\texttt{\$STR} --- True if string \texttt{\$STR} is assigned a~value and is not null
3764
3809
\item\texttt{\$STR1 != \$STR2} --- True if both strings are unequal
3765
-
\item-\texttt{a} --- Performs the \texttt{AND} function (\texttt{[\ldots -a \ldots~]} or \texttt{[\ldots~] \&\& [\ldots~]})
3766
-
\item-\texttt{o} --- Performs the \texttt{OR} function (\texttt{[\ldots -o \ldots~]} or \texttt{[\ldots~] || [\ldots~]})
3767
-
\item Do not confuse globing patterns and regular expressions when using \texttt{[[\ldots~]]}
3810
+
\item\texttt{-a} --- Performs the \texttt{AND} function (\texttt{[~\ldots~-a~\ldots~]} or \texttt{[~\ldots~] \&\& [~\ldots~]})
3811
+
\item\texttt{-o} --- Performs the \texttt{OR} function (\texttt{[~\ldots~-o~\ldots~]} or \texttt{[~\ldots~] || [~\ldots~]})
3812
+
\item Do not confuse globing patterns and regular expressions when using \texttt{[[~\ldots~]]}
3768
3813
\begin{itemize}
3769
-
\item Shell globing: \texttt{if [[ "\$STRINGVAR"\textbf{==} ?[sS]tring* ]]; then} --- \texttt{?} represents single character \texttt{[]} any character inside and \texttt{*} zero or more characters
3770
-
\item Regular expressions: \texttt{if [[ "\$STRINGVAR"\textbf{=$\sim$} .[sS]tring.* ]]; then} --- \texttt{.} represents single character (\texttt{?} would be zero or one occurrence of preceding expression), \texttt{[]} any character inside and \texttt{.*} zero or more occurrences of any single characters
3814
+
\item\textbf{Shell globing:} \texttt{if [[ "\$STRINGVAR"\alert{==} ?[sS]tring* ]]; then} --- \texttt{?} represents single character \texttt{[]} any character inside and \texttt{*} zero or more characters
3815
+
\item\textbf{Regular expressions:} \texttt{if [[ "\$STRINGVAR"\alert{=$\sim$} .[sS]tring.* ]]; then} --- \texttt{.} represents single character (\texttt{?} would be zero or one occurrence of preceding expression), \texttt{[]} any character inside and \texttt{.*} zero or more occurrences of any single characters
3816
+
\item Same expression is interpreted in different ways
3771
3817
\end{itemize}
3772
3818
\end{itemize}
3773
3819
\end{frame}
@@ -3819,26 +3865,42 @@ \subsection{Branching the code}
3819
3865
3820
3866
\subsection{Loops}
3821
3867
3822
-
\begin{frame}[fragile]{For cycles I}
3868
+
\begin{frame}[fragile]{For loops I}
3869
+
\begin{itemize}
3870
+
\item\texttt{for} loops are available in practically every programming language
3871
+
\item BASH allows plenty of variants how to declare repetitions
3872
+
\item In \texttt{for} loop we know in advance number of repeats --- numerical sequence, list of files,~\ldots
3873
+
\item Common way how to do same operation with multiple files
3874
+
\end{itemize}
3823
3875
\begin{bashcode}
3824
3876
# Ways how to declare number of repetitions
3877
+
# Variable $I contains in every repeat number from 1 to 10 - number of
3878
+
# respective repeat
3825
3879
for I in $(seq 1 10); do echo $I; done # "seq" is outdated
3826
3880
for I in 12345678910; do echo $I; done
3827
3881
for I in {1..10}; do echo $I; done
3828
3882
for (( I=1; I<=10; I++ )); do echo $I; done
3829
-
# One line for cycle for resizing of images (another option, as above)
3883
+
# One line for cycle for resizing of multiple images
3884
+
# Variable $JPGF contains in every repeat one-by-one name of input file
3885
+
# processed in the respective turn
3830
3886
for JPGF in *.jpg; do convert $JPGF -resize 100x100 thumbs-$JPGF; done
3831
-
# More commands in a block
3832
-
for JPGF in $(ls -1 *.jpg); do
3833
-
echo "Processing JPG $JPGF"
3834
-
convert $JPGF -resize 100x100 thumbs-$JPGF
3835
-
echo "File thumbs-$file created"
3836
-
done
3837
3887
\end{bashcode}
3838
3888
\end{frame}
3839
3889
3840
-
\begin{frame}[fragile]{For cycles II}
3890
+
\begin{frame}[fragile]{For loops II}
3891
+
\begin{itemize}
3892
+
\item Basic way to process more files
3893
+
\end{itemize}
3841
3894
\begin{bashcode}
3895
+
# More commands in a block
3896
+
for JPGF in *.jpg; do
3897
+
echo "Processing JPG $JPGF"
3898
+
file $JPGF # Get information about currently processed file
3899
+
convert $JPGF -resize 100x100 thumbs-$JPGF
3900
+
echo "File thumbs-$JPGF created"
3901
+
done
3902
+
# Passing through each loop can be influenced using conditions and
3903
+
# subsequent skipping of rest of the loop
3842
3904
for ...; do # Start cycles as you need
3843
3905
command1 # command1 will be executed in any case
3844
3906
if (condition); then # Set some condition to skip command2
@@ -3848,7 +3910,7 @@ \subsection{Loops}
3848
3910
\end{bashcode}
3849
3911
\end{frame}
3850
3912
3851
-
\begin{frame}[fragile]{While and until cycles I}
3913
+
\begin{frame}[fragile]{While and until loops I}
3852
3914
\begin{bashcode}
3853
3915
# while cycle is evaluating condition and if it is equal to 0 (TRUE)
3854
3916
# the cycle body is launched, repeatedly while the condition is met
@@ -3859,20 +3921,34 @@ \subsection{Loops}
3859
3921
until condition; do
3860
3922
commands
3861
3923
done
3924
+
# Compare differences between while (more common) and until loops
3925
+
I=0 # Assign initial value
3926
+
# Repeat while I is lower or equal 10; in every step increment I by 1
3927
+
while [ $I -le 10 ]; do echo "Value: $I"; I=$(($I + 1)); done
3928
+
I=20 # Assign initial value
3929
+
# Repeat until I is lower then 10; in every step decrement I by 1
3930
+
until [ $I -lt 10 ]; do echo "Value: $I"; I=$(($I - 1)); done
3931
+
\end{bashcode}
3932
+
\end{frame}
3933
+
3934
+
\begin{frame}[fragile]{While and until loops II}
3935
+
\begin{bashcode}
3936
+
# 'continue' skips to another look turn, 'break' terminates running
3937
+
# of whole loop (no further turns) and skips to following commands
3862
3938
while ...; do # Start cycles as you need
3863
3939
commands...
3864
3940
if [condition]; then # If something happens
3865
-
break; fi # End up the cycles and continue by following commands
3866
-
while read TEXTLINE; do # Run cycles on text file
3941
+
break # End up the cycles and continue by following commands
3942
+
fi
3943
+
# While loops are popular to process every line of input file
3944
+
# The text file use to contain e.g. list of files to process (if for
3945
+
# whatever reason 'for' loop construction is impractical)
3946
+
while read TEXTLINE; do # Run cycles on every line of text file
3867
3947
commands... # TEXTLINE contains in each cycle one line of the file
3868
3948
done < text_file_to_process.txt
3869
-
\end{bashcode}
3870
-
\end{frame}
3871
-
3872
-
\begin{frame}[fragile]{While and until cycles II}
3873
-
\begin{bashcode}
3874
-
while :; do echo "Press CTRL+C to exit..."; done # Infinite loop
3875
-
for (( ; ; )) ; do echo "Press CTRL+C to exit..."; done # Infinite loop
3949
+
# Infinite loops - common when waiting for some condition to proceed
3950
+
while :; do echo "Press CTRL+C to exit..."; done
3951
+
for (( ; ; )) ; do echo "Press CTRL+C to exit..."; done
3876
3952
\end{bashcode}
3877
3953
\end{frame}
3878
3954
@@ -4870,6 +4946,7 @@ \subsection{Resources}
4870
4946
\item Linux tutorial \url{https://ryanstutorials.net/linuxtutorial/}
4871
4947
\item Getting Started with BASH \url{https://www.hypexr.org/bash_tutorial.php}
0 commit comments