Skip to content

Commit 54a1af3

Browse files
committed
Improvements of command line usage chapter. More BASH shortcuts. More about handling variables.
1 parent ee620c0 commit 54a1af3

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

presentation/linux_bash_metacentrum_course.tex

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ \subsection{Text}
10911091

10921092
\section{Command line}
10931093

1094-
\begin{frame}[allowframebreaks]{Command line}
1094+
\begin{frame}[allowframebreaks]{Command line}{Practical usage of command line tools}
10951095
\tableofcontents[currentsection, sectionstyle=show/hide, hideothersubsections]
10961096
\end{frame}
10971097

@@ -1103,14 +1103,14 @@ \section{Command line}
11031103
\begin{itemize}
11041104
\item \texttt{man somecommand}
11051105
\item Arrows to move up and down, \texttt{q} to quit
1106-
\item Type \texttt{/} and then type text to search and hit Enter to search --- next hit by \texttt{n}, quit search by ESC (twice)
1106+
\item Type \texttt{/} and then type text to search and hit Enter to search --- next hit by \texttt{n}
11071107
\item Command \texttt{info} more advanced --- type \texttt{?} for help
11081108
\end{itemize}
11091109
\item Parameters can be combined, order doesn't matter (same variants: \texttt{ls -la}; \texttt{ls -al}; \texttt{ls -a -l}; \texttt{ls -l -a})
11101110
\item \enquote{Long} parameters (\texttt{-{-}XXX}) must stay separated
11111111
\item Commands (applications) must be in PATH (slide~\ref{PATH}) --- actual directory isn't
11121112
\begin{itemize}
1113-
\item If the script is is current directory, use \texttt{./script.sh} or full path
1113+
\item If the script is is current directory (out of PATH), use \texttt{./script.sh} or full path
11141114
\end{itemize}
11151115
\item Custom scripts must have execute permission (\texttt{chmod +x script.sh})
11161116
\end{itemize}
@@ -1137,7 +1137,7 @@ \section{Command line}
11371137
# Install updated basic UNIX tools
11381138
brew install coreutils gnu-sed gawk grep bash gcc make wget dos2unix
11391139
brew list # List of installed packages (brew formulae)
1140-
brew info FORMULA # Information about particular formula
1140+
brew info FORMULA # Information about particular formula (package)
11411141
brew search KEYWORD # Search for applications
11421142
brew update # Update Homebrew
11431143
brew upgrade # Update all packages installed by Homebrew
@@ -1240,16 +1240,16 @@ \subsection{Screen}
12401240
\end{bashcode}
12411241
\vfill
12421242
\begin{itemize}
1243-
\item Toy server: user names \texttt{cu01}--\texttt{cu20}
1243+
\item Toy server: user names \texttt{courseuser01}--\texttt{courseuser15}
12441244
\end{itemize}
12451245
\vfill
12461246
\begin{bashcode}
1247-
ssh cuXY@vyuka.natur.cuni.cz
1247+
ssh courseuserXY@vyuka.natur.cuni.cz
12481248
\end{bashcode}
12491249
\vfill
12501250
\begin{itemize}
12511251
\item If fingerprint key changes, ssh complains a~lot --- possible \href{https://en.wikipedia.org/wiki/Man-in-the-middle_attack}{man in the middle attack}
1252-
\item From Windows use \href{https://www.putty.org/}{Putty}
1252+
\item From Windows use \href{https://www.putty.org/}{Putty} (see right figure)
12531253
\end{itemize}
12541254
\begin{center}
12551255
\includegraphics[height=6cm]{putty.png}
@@ -1293,22 +1293,30 @@ \subsection{BASH}
12931293
\end{itemize}
12941294
\end{frame}
12951295

1296-
\begin{frame}[allowframebreaks]{Nice BASH features for easier work (selection)} % TODO Add more BASH features
1296+
\begin{frame}[allowframebreaks]{Nice BASH features for easier work (selection)}
12971297
\begin{itemize}
12981298
\item Arrows up and down list in the history of commands
12991299
\item List whole history by command \texttt{history}
13001300
\item \texttt{Ctrl+R} --- reverse search in history --- type to search last command(s) containing typed character(s) --- repeat typing \texttt{Ctrl+R} to search deeper in history
1301+
\begin{itemize}
1302+
\item When on correct entry, hit Enter or use L/R arrow keys to edit it
1303+
\end{itemize}
1304+
\item \texttt{Ctrl+J} --- exit search
13011305
\item \texttt{TAB} --- list command and files starting by typed characters
1302-
\item \texttt{Home}/\texttt{End} --- go to beginning/end of the line
1306+
\item \texttt{Home}/\texttt{End} (or \texttt{Ctrl+A}/\texttt{Ctrl+E}) --- go to beginning/end of the line
13031307
\item \texttt{Ctrl+L} --- clear screen (like \texttt{clear} command)
13041308
\item \texttt{Ctrl+Shift+C}/\texttt{V} --- copy/paste the text from terminal emulator
13051309
\item \texttt{Ctrl+C} --- cancel running task
13061310
\item \texttt{Ctrl+D} --- log out (like commands \texttt{exit} or \texttt{logout})
13071311
\item \texttt{Ctrl+U} --- move text before cursor into clipboard
13081312
\item \texttt{Ctrl+K} --- move text after cursor into clipboard
1313+
\item \texttt{Ctrl+Y} --- paste selection from the above commands
13091314
\item \texttt{Ctrl+left/right arrow} --- skip words
1315+
\item \texttt{Ctrl+S} --- suspend output of long verbose commands (resume with \texttt{Ctrl+Q})
13101316
\item \texttt{Ctrl+T} --- flip current and left character
1317+
\item \texttt{!xx} --- launch last command starting with \texttt{xx} (use with care)
13111318
\item \texttt{Ctrl+X+E} --- start text editor (default, defined in \texttt{$\sim$/.bashrc}) in current directory
1319+
\item Can slightly differ (be limited) among various systems, terminal emulators, etc.
13121320
\end{itemize}
13131321
\end{frame}
13141322

@@ -1326,7 +1334,7 @@ \subsection{BASH}
13261334
\begin{frame}[fragile]{BASH settings (popular examples)}{Write them into BASH configuration file}
13271335
\label{bashrc}
13281336
\begin{itemize}
1329-
\item In any text editor open \texttt{$\sim$/.bashrc} and edit it
1337+
\item In any text editor open \texttt{$\sim$/.bashrc} (and/or \texttt{$\sim$/.bash\_profile}) and edit it
13301338
\item Behavior of BASH can be set to fit user's needs
13311339
\item Terminal emulators allow to set custom fonts and colors,~\ldots
13321340
\end{itemize}
@@ -1484,9 +1492,9 @@ \subsection{Archives}
14841492
\begin{tabular}{m{2.25cm}m{6.3cm}m{5.3cm}}
14851493
\textbf{Archive} & \textbf{Compressing command} & \textbf{Decompressing command}\\
14861494
*.tar & tar cvf archive.tar file1 file2 file3 & tar xvf archive.tar\\
1487-
*.tar.gz\alert{/} *.tgz & tar czvf archive.tar.gz\alert{/}.tgz file1 file2 & tar xzvf archive.tar.gz\alert{/}.tgz\\
1488-
*.tar.bz\alert{/} *.tbz\alert{/} *.tar.bz2 & tar cjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2 file1 file2 file3 file4 & tar xjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2\\
1489-
*.tar.xz & tar cvJf archive.tar.xz file1 file2 file4 & tar xvJf archive.tar.xz\\
1495+
*.tar.gz \alert{/} *.tgz & tar czvf archive.tar.gz\alert{/}.tgz file1 file2 & tar xzvf archive.tar.gz\alert{/}.tgz\\
1496+
*.tar.bz \alert{/} *.tbz \alert{/} *.tar.bz2 & tar cjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2 file1 file2 file3 file4 & tar xjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2\\
1497+
*.tar.xz & tar cvJf archive.tar.xz file1 file2 file3 & tar xvJf archive.tar.xz\\
14901498
*.tar.lzma & tar cvf - file1 file2 file3 file4 | lzma > archive.tar.lzma & lzcat archive.tar.lzma | tar xvf -\\
14911499
*.gz & gzip file & gunzip archive.gz\\
14921500
*.bz2 & bzip2 file & bunzip2 archive.bz2\\
@@ -1499,9 +1507,9 @@ \subsection{Archives}
14991507
15001508
\begin{frame}{Compressing and decompressing archives}
15011509
\begin{itemize}
1502-
\item \texttt{gzip}, \texttt{bzip2}, \texttt{xz} and \texttt{lzma} are able to pack only one file --- use them together with \texttt{tar} to pack multiple files (when used \textbf{without} \texttt{tar} they \alert{move} file into archive)
1510+
\item \texttt{gzip}, \texttt{bzip2}, \texttt{xz} and \texttt{lzma} are able to \textbf{pack only one file} --- use them together with \texttt{tar} to pack multiple files (when used \textbf{without} \texttt{tar} they \alert{move} file into archive)
15031511
\item In Linux, \texttt{gzip} (and less \texttt{bzip2}) are the most commonly used
1504-
\item Rar is not used at Linux/UNIX at all
1512+
\item Rar and arj are not used at Linux/UNIX at all
15051513
\item Zip is probably the most portable between Linux/UNIX and Windows
15061514
\item \texttt{lzma} and \texttt{xz} have excellent compression, but can be very slow, use similar algorithm, often confused
15071515
\end{itemize}
@@ -1750,12 +1758,28 @@ \subsection{Variables}
17501758
\end{bashcode}
17511759
\end{frame}
17521760
1761+
\begin{frame}{How quotes influence reading of variable content III}
1762+
\begin{itemize}
1763+
\item As soon as variable contains only Latin alphanumerical characters, assignment like \texttt{MYVAR=whatever} works, but it is not recommended
1764+
\item If variable should contain another variable, special characters, etc, use double quotes, e.g. \texttt{WORKDIR="/home/\$USER/data"}
1765+
\begin{itemize}
1766+
\item It might be called like \texttt{echo \$MYVAR}, but if there would be non-standard characters anywhere, it'd fail, so prefer \texttt{"\$MYVAR"} (it \textit{might} work without quotes, but don't risk it) or \texttt{"\$\textbraceleft MYVAR\textbraceright"}
1767+
\end{itemize}
1768+
\item If variable should contain any special character (including space), or you wish to be sure you keep it's literal value, use single quotes, e.g. \texttt{MYDATA='$\sim$/My Doc/exp 1/data.fsa'} (and then \texttt{"\$MYVAR"})
1769+
\item Similar for using variables
1770+
\begin{itemize}
1771+
\item Using \texttt{\$MYVAR} is fine if it doesn't contain anything special (but are you really sure?)
1772+
\item Best practice is to use \texttt{"\$\textbraceleft MYVAR\textbraceright"} as there is highest security if \texttt{MYVAR} would contain special characters, and there is room for various manipulations (see next slide)
1773+
\end{itemize}
1774+
\end{itemize}
1775+
\end{frame}
1776+
17531777
\begin{frame}[fragile]{The PATH variable}
17541778
\label{PATH}
17551779
\begin{itemize}
17561780
\item Lists directories (separated by colon \texttt{:}) where the current shell searches for commands
17571781
\item If some software is installed outside standard locations, the user must specify the full path (or update the \texttt{\$PATH})
1758-
\item In case there are two commands with the same name (e.g. \texttt{/bin/somecommand} and \texttt{/usr/bin/somecommand}), the order of directories in \texttt{\$PATH} matters --- the first occurrence is used, any possible later ignored
1782+
\item In case there are two commands with the same name (e.g. \texttt{/usr/bin/somecommand} and \texttt{/home/\$USER/bin/somecommand}), the order of directories in \texttt{\$PATH} matters --- the first occurrence is used, any possible later ignored
17591783
\item Computing clusters (like MetaCentrum) use to have special command (e.g. \texttt{module}) to load particular software (including particular version) by extending user's \texttt{\$PATH}
17601784
\end{itemize}
17611785
\begin{bashcode}
@@ -1789,11 +1813,30 @@ \subsection{Variables}
17891813
\end{bashcode}
17901814
\end{frame}
17911815
1792-
% TODO Add manipulation with variables like ${VAR%...}
1816+
\begin{frame}[fragile]{Manipulating with content of variable}
1817+
\begin{itemize}
1818+
\item It is possible to change/remove extension (suffix) of variable, do simple search and replace with its content, etc. (useful in scripts e.g. for input and output files)
1819+
\item Search and replace syntax is related to \texttt{sed} (slide~\ref{sedex})
1820+
\end{itemize}
1821+
\begin{bashcode}
1822+
MYVAR='oxalis_assembly_6235.aln.fasta' # Assign new variable
1823+
echo "$MYVAR" # See it - it works: oxalis_assembly_6235.aln.fasta
1824+
# Remove the '.fasta' extension
1825+
echo "${MYVAR%.fasta}" # oxalis_assembly_6235.aln
1826+
# Replace extension '.fasta' with '.fsa'
1827+
echo "${MYVAR%.fasta}".fsa # oxalis_assembly_6235.aln.fsa
1828+
# Remove any last extension
1829+
echo "${MYVAR%.*}" # oxalis_assembly_6235.aln
1830+
# Replace 'aln' within variable name with 'aligned'
1831+
echo "${MYVAR/aln/aligned}" # oxalis_assembly_6235.aligned.fasta
1832+
# More complex manipulations with variable content (try to avoid this)
1833+
echo "$(echo "$MYVAR" | sed 's/assembly/contig/')"
1834+
# sed allows usage of variables (sed "s/pattern/$SOMEVAR/")
1835+
\end{bashcode}
1836+
\end{frame}
17931837

1794-
\begin{frame}[fragile]{BASH expressions}
1838+
\begin{frame}[fragile]{BASH expressions}{Basic arithmetic operations and more with BASH}
17951839
\begin{bashcode}
1796-
# Many operands have special meaning in BASH - must be escaped
17971840
echo $((5 < 7)) # Is 5 smaller than 7? TRUE (1)
17981841
echo $((3 > 4)) # Is 3 greater than 4? FALSE (0)
17991842
echo $((16 / 3)) # Division (without decimal part)
@@ -1832,13 +1875,16 @@ \subsection{Variables}
18321875
\begin{itemize}
18331876
\item Key feature of BASH --- work with individual commands as with Lego to get new functionality
18341877
\item \alert{\texttt{\&}} --- command will be launched in background, terminal is available for next typing: \texttt{firefox \&} (when launching graphical application, hit \textbf{Enter} afterward if there is no active command line prompt)
1878+
\begin{itemize}
1879+
\item If the application has any output, it goes to the screen, so it can be bit messy\ldots
1880+
\end{itemize}
18351881
\item \alert{\texttt{\&\&}} --- second command is launched only when first command exits without error (exits with status \texttt{0}): \texttt{mkdir NewDir \&\& cd NewDir}
18361882
\item \alert{\texttt{;}} --- second command is launched regardless exit status of the first one: \texttt{kshfskcbd; hostname}
18371883
\item \alert{\texttt{\textbraceleft\ldots\textbraceright}} --- commands within curl brackets are launched as one block
18381884
\begin{itemize}
18391885
\item Closing bracket \texttt{\textbraceright} must be either on new line or preceded by semicolon (\texttt{\ldots{ }; \textbraceright})
18401886
\end{itemize}
1841-
\item \alert{\texttt{||}} --- second command is launched when first command fails (has non zero exit status):\\\texttt{cd newdir || \textbraceleft~mkdir newdir \&\& cd newdir; \textbraceright}
1887+
\item \alert{\texttt{||}} --- second command is launched when first command fails (has non-zero exit status):\\\texttt{cd newdir || \textbraceleft~mkdir newdir \&\& cd newdir; \textbraceright}
18421888
\begin{itemize}
18431889
\item Easy way how to do something when previous command fails, either exit script (\texttt{\ldots{ }|| exit 1}) or somehow fix it (see above), report problem (\texttt{\ldots{ }|| \textbraceleft echo "It failed!" \&\& exit 1; \textbraceright}) or so
18441890
\end{itemize}
@@ -1955,8 +2001,8 @@ \subsection{Variables}
19552001
{ commandX; ls; } > outputfile.log 2>&1 # Inspect outputfile.log
19562002
{ commandX; ls; } >> outputfile.log 2>&1 # Inspect outputfile.log
19572003
# Add error output to the error log text file
1958-
command >> outputfile.log 2>error.log # Example:
1959-
{ commandX; ls; } >> outputfile.txt 2>error.log
2004+
command >> outputfile.log 2> error.log # Example:
2005+
{ commandX; ls; } >> outputfile.txt 2> error.log
19602006
cat outputfile.txt # See results
19612007
cat error.log # See results
19622008
\end{bashcode}
@@ -2152,7 +2198,7 @@ \subsection{Network}
21522198
\end{itemize}
21532199
\end{frame}
21542200
2155-
\begin{frame}[fragile]{Basic network information and testing}
2201+
\begin{frame}[fragile]{Basic network information and testing} % TODO rewrite netstat examples for ss
21562202
\begin{bashcode}
21572203
hostname # Get name of the computer
21582204
ping web.natur.cuni.cz # Ping host. Is it alive? Cancel by Ctrl+C
@@ -2167,7 +2213,7 @@ \subsection{Network}
21672213
netstat -atn # Information about all network connections
21682214
netstat -ntplu # Show open TCP/UDP ports
21692215
netstat -anp # Show active connections
2170-
netstat -h # See for explanation of 3 above examples
2216+
ss --help # netstat is deprecated, prefer usage of ss
21712217
# If using nmap at faculty, firewall disconnects you for 10 minutes!
21722218
nmap -r someserver.cz # Scan someserver.cz for opened ports
21732219
nmap botany.natur.cuni.cz --script ssh-hostkey # See SSH key
@@ -2252,15 +2298,15 @@ \subsection{Network}
22522298
\begin{itemize}
22532299
\item Private key is typically in \texttt{$\sim$/.ssh/id\_*} file(s) (according to cipher)
22542300
\item Public key is typically in \texttt{$\sim$/.ssh/id\_*.pub} file(s) (according to selected cipher) and are copied to target servers
2255-
\item Private key is unlocked by password and it then allows to login to any server having the public key
2301+
\item Private key is unlocked by password (might be without password) and it then allows to login to any server having the public key
22562302
\item Having only public key is not enough to login, private key without password is still not enough, but user must be sure private key is kept securely and not lost or stolen
2257-
\item When running it, \texttt{ssh-keygen} asks bunch of questions --- unless having special needs, keep defaults (hit Enter), using password for key is optional
2303+
\item When running it, \texttt{ssh-keygen} asks bunch of questions --- unless having special needs, keep defaults (hit Enter), using password for key is optional (but recommended)
22582304
\end{itemize}
2259-
\item File \texttt{$\sim$/.ssh/known\_hosts} contain servers you have ever connect to, and their SSH fingerprints (unique IDs) --- if this changes, SSH complains a lot as it could be \href{https://en.wikipedia.org/wiki/Man-in-the-middle_attack}{Man-in-the-middle attack}
2305+
\item File \texttt{$\sim$/.ssh/known\_hosts} contain servers you have ever connected to, and their SSH fingerprints (unique IDs) --- if this changes, SSH complains a lot as it could be \href{https://en.wikipedia.org/wiki/Man-in-the-middle_attack}{Man-in-the-middle attack}
22602306
\item File \texttt{$\sim$/.ssh/authorized\_keys} contains public keys allowing logging to the machine
2261-
\item \texttt{~/.ssh/config} can store various settings for particular servers
2307+
\item \texttt{$\sim$//.ssh/config} can store various settings for particular servers
22622308
\item SSH keys work in all applications using SSH (SFTP, SSHFS, rsync, many graphical file managers,~\ldots)
2263-
\item User can have various local passwords on servers, but use single SSH key to connect to all of them
2309+
\item User can have various local passwords on servers, but use single SSH key to connect to all of them, very convenient if connecting to multiple servers
22642310
\end{itemize}
22652311
\end{frame}
22662312
@@ -2323,7 +2369,7 @@ \subsection{Network}
23232369
\end{enumerate}
23242370
\end{frame}
23252371
2326-
\begin{frame}{Faculty web server}
2372+
\begin{frame}{Faculty web server(s)}
23272373
\begin{itemize}
23282374
\item Login requires same credentials as to \href{https://ldapuser.cuni.cz/}{CAS} (login name, no ISIC number)
23292375
\item Faculty information are \href{https://www.natur.cuni.cz/fakulta/cit/web-aplikace/webhosting}{only in Czech}
@@ -2343,7 +2389,7 @@ \subsection{Parallelisation}
23432389
\begin{itemize}
23442390
\item \href{https://www.gnu.org/software/parallel/}{GNU Parallel} can distribute task among CPU threads of one computer, or even among different computers in network
23452391
\item It is not (so) effective for short/small tasks
2346-
\item Important operands (for more see \texttt{man parallel})
2392+
\item Important operands (for more see \texttt{man parallel} and next slides)
23472393
\begin{itemize}
23482394
\item \texttt{\{\}} --- input line --- whole line read from input source (typically standard input)
23492395
\item \texttt{\{.\}} --- input line without extension
@@ -5190,4 +5236,3 @@ \subsection{The very end}
51905236
% * Přeskládat řetězení, přesměrování, globbing, proměnné, atd.
51915237
% * Zkontrolovat syntax skriptování (hlavně v prezentaci), povyházet zastaralé konstrukce.
51925238
% * cluster vs. grid vs. supercomputer
5193-
% * vyhodit netstat

0 commit comments

Comments
 (0)