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 Every script begins with \texttt{\#!/bin/bash} (or alternative for another shells, Perl,~\ldots)
3426
3426
\item Add any commands you like\ldots
3427
3427
\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}
3430
3429
\item The most simple script:
3431
3430
\end{itemize}
3432
3431
\vfill
3433
3432
\begin{bashcode}
3434
3433
#!/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
3437
3436
echo "Hi, ${USER}, today is $(date) and your PATH is ${PATH}."
3437
+
echo "Free space on your disk:"
3438
+
df -h
3438
3439
echo
3439
3440
exit
3440
3441
\end{bashcode}
3441
3442
\end{frame}
3442
3443
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
+
3443
3460
\subsection{Functions}
3444
3461
3445
3462
\begin{frame}[fragile]{Functions in BASH}{Pieces of code, which can be used repeatedly}
@@ -3463,22 +3480,6 @@ \subsection{Functions}
3463
3480
\end{bashcode}
3464
3481
\end{frame}
3465
3482
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
-
3482
3483
\subsection{Reading variables}
3483
3484
3484
3485
\begin{frame}{It is important to check user input\ldots}
@@ -3902,7 +3903,7 @@ \subsection{Branching the code}
3902
3903
commands2
3903
3904
else
3904
3905
commands3
3905
-
fi
3906
+
fi # Branching can be long, hard to read and susceptible to errors
3906
3907
\end{bashcode}
3907
3908
\end{frame}
3908
3909
@@ -3923,29 +3924,32 @@ \subsection{Branching the code}
\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.
3927
3928
\item Expanding file names --- \texttt{if [[ -a *.sh ]]} (variant with only one bracket doesn't work when there are multiple sh files)
3928
3929
\item More detailed test, e.g. \texttt{if [[ \$num -eq 3 \&\&"\$\textbraceleft STRINGVAR\textbraceright " == XXX ]]\ldots}
3929
3930
\end{itemize}
3930
3931
\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}
3949
3953
\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)
3950
3954
\item Do not confuse globing patterns and regular expressions when using \texttt{[[~\ldots~]]}
0 commit comments