bonus2: do not escape preformatted text chars
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@264 a672b425-5310-4d7a-af5c-997e18724b81
This commit is contained in:
parent
eca54a3fdf
commit
90ff4fec71
4 changed files with 155 additions and 136 deletions
bonus2/src/ch/usi/inf/atelier/group1
docs/website_as_latex
site/pages/info
|
@ -92,9 +92,14 @@ class HtmlParser(private val singlePage: Boolean) {
|
|||
}
|
||||
|
||||
fun save() {
|
||||
val output = HtmlToLatexWriter(content.toString(), singlePage)
|
||||
|
||||
// Remove escapes from preformatted text
|
||||
output.unEscapeCharsInPreformatted()
|
||||
|
||||
val document = if (singlePage) {
|
||||
// End the singlePage'd document
|
||||
HtmlToLatexWriter(content.toString(), true).run {
|
||||
output.run {
|
||||
commit()
|
||||
endDocument()
|
||||
toString()
|
||||
|
|
|
@ -151,6 +151,21 @@ class HtmlToLatexWriter(private var content: String, private val singlePage: Boo
|
|||
}
|
||||
}
|
||||
|
||||
fun unEscapeCharsInPreformatted() {
|
||||
val pattern = Pattern.compile("(?:\\\\begin\\{verbatim}((?:.*\\r?\\n?)*?)\\\\end\\{verbatim})+")
|
||||
val matcher = pattern.matcher(content)
|
||||
|
||||
while (matcher.find()) {
|
||||
val found = matcher.group(0)
|
||||
content = content.replace(found, found.replace("\\_", "_")
|
||||
.replace("\\\$", "\$")
|
||||
.replace("\\[", "[")
|
||||
.replace("\\]", "]")
|
||||
.replace("\\#", "#"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replace <h2> with LaTeX \subsection
|
||||
*/
|
||||
|
@ -174,7 +189,7 @@ class HtmlToLatexWriter(private var content: String, private val singlePage: Boo
|
|||
* Replace <table> with LaTex tabular
|
||||
*/
|
||||
fun changeTable() {
|
||||
val pattern = Pattern.compile("(?:<table>((?:.*?\\r?\\n?)*)</table>)+")
|
||||
val pattern = Pattern.compile("(?:<table>((?:.*\\r?\\n?)*?)</table>)+")
|
||||
val matcher = pattern.matcher(content)
|
||||
|
||||
while (matcher.find()) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
% File autogenerated using the Bonus 2 program at 2018-11-18 11:45
|
||||
% File autogenerated using the Bonus 2 program at 2018-11-19 12:39
|
||||
\documentclass[hidelinks,12pt,a4paper,numbers=enddot]{scrartcl}
|
||||
|
||||
\usepackage[margin=2cm]{geometry}
|
||||
|
@ -197,7 +197,7 @@ When it has completed its operation for the last file, \texttt{paste} will outpu
|
|||
character and move on to the next line.
|
||||
|
||||
\begin{verbatim}
|
||||
paste \[flags\] \[file1\] \[file2\] ...
|
||||
paste [flags] [file1] [file2] ...
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -271,7 +271,7 @@ abcdefghi
|
|||
|
||||
The syntax command is:
|
||||
\begin{verbatim}
|
||||
colrm \[first\] \[last\]
|
||||
colrm [first] [last]
|
||||
\end{verbatim}
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -305,14 +305,14 @@ from each file, precede each set of lines by the name of the file and separate
|
|||
each set of lines by one vertical space.
|
||||
|
||||
\begin{verbatim}
|
||||
head \[flags\] \[file1\] \[file2\] ...
|
||||
head [flags] [file1] [file2] ...
|
||||
\end{verbatim}
|
||||
|
||||
The \texttt{tail} command is similar to the \texttt{head} command
|
||||
except that it reads the final lines in files rather than the first lines.
|
||||
|
||||
\begin{verbatim}
|
||||
tail \[flags\] \[file1\] \[file2\] ...
|
||||
tail [flags] [file1] [file2] ...
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -398,7 +398,7 @@ Huawei
|
|||
\end{verbatim}
|
||||
|
||||
The syntax command is:
|
||||
\begin{verbatim}grep \[flag\] \[keyword\] \[file\]\end{verbatim}
|
||||
\begin{verbatim}grep [flag] [keyword] [file]\end{verbatim}
|
||||
You can put different flags together to refine the search.
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -508,7 +508,7 @@ Videogame
|
|||
\end{verbatim}
|
||||
|
||||
The syntax command is:
|
||||
\begin{verbatim} nl \[flags\]\[file\] \end{verbatim}
|
||||
\begin{verbatim} nl [flags][file] \end{verbatim}
|
||||
|
||||
\begin{verbatim}
|
||||
nl example.txt
|
||||
|
@ -527,7 +527,7 @@ How you can see the command has numbered the lines.
|
|||
\begin{itemize}
|
||||
\item \textbf{-b (regex)}: Specify the lines to be numered
|
||||
\begin{verbatim}
|
||||
nl -b p\^\[cv\] example.txt
|
||||
nl -b p\^[cv] example.txt
|
||||
1 Car
|
||||
2 Computer
|
||||
3 Videogame
|
||||
|
@ -615,7 +615,7 @@ Pizza
|
|||
|
||||
The syntax command is:
|
||||
\begin{verbatim}
|
||||
comm \[flag\]\[file1\] \[file2\]
|
||||
comm [flag][file1] [file2]
|
||||
\end{verbatim}
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -869,7 +869,7 @@ An alternate backup suffix may be specified via the -B option's argument.\\
|
|||
Basic syntax is in the form
|
||||
|
||||
\begin{verbatim}
|
||||
install \[OPTION\]... SOURCE DEST
|
||||
install [OPTION]... SOURCE DEST
|
||||
\end{verbatim}
|
||||
|
||||
This line of code copies all .xyz file from \emph{/source/folder} to \emph{/destination/folder}
|
||||
|
@ -913,7 +913,7 @@ So, first of all, you need to download Unarchiver in order to use this command.\
|
|||
The basic syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
7z \[adeltux\] \[-\] \[SWITCH\] \texttt{\textless ARCHIVE\_NAME\textgreater \textless ARGUMENTS\textgreater }
|
||||
7z [adeltux] [-] [SWITCH] \texttt{\textless ARCHIVE_NAME\textgreater \textless ARGUMENTS\textgreater }
|
||||
\end{verbatim}
|
||||
|
||||
Here some useful options that we will use to explain some examples:
|
||||
|
@ -945,7 +945,7 @@ There are many type of switches, we will see the most common ones.
|
|||
Specifies the compression method
|
||||
|
||||
\begin{verbatim}
|
||||
-m\textless method\_parameters\textgreater
|
||||
-m\textless method_parameters\textgreater
|
||||
\end{verbatim}
|
||||
|
||||
The format for this switch depends on the archive type which are:
|
||||
|
@ -965,7 +965,7 @@ Specifies the method of treating filenames or wildcards on the command line.
|
|||
The syntax:
|
||||
|
||||
\begin{verbatim}
|
||||
-r\[- | 0\]
|
||||
-r[- | 0]
|
||||
\end{verbatim}
|
||||
|
||||
\begin{table}[h]
|
||||
|
@ -1020,7 +1020,7 @@ Specifes which filenames or wildcards must be excluded from the operation.
|
|||
Syntax:
|
||||
|
||||
\begin{verbatim}
|
||||
-x\[\textless recurse\_type\textgreater \]\textless file\_ref\textgreater
|
||||
-x[\textless recurse_type\textgreater ]\textless file_ref\textgreater
|
||||
\end{verbatim}
|
||||
|
||||
Sometimes we need to add files into an archive.7z, except one, this can be done as fllows:
|
||||
|
@ -1043,7 +1043,7 @@ The command \texttt{md5} calculates an unique string for each input given as
|
|||
argument using the \emph{md5} algorithm.
|
||||
|
||||
\begin{verbatim}
|
||||
md5 \[flags\] \[arg1\] \[arg2\]
|
||||
md5 [flags] [arg1] [arg2]
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the (optional) md5 flags, read below for more info,
|
||||
|
@ -1113,7 +1113,7 @@ The \emph{git} command providesa set of high-level operations and full access to
|
|||
Here is the basic syntax but commands may become some more complicated:
|
||||
|
||||
\begin{verbatim}
|
||||
git \[flags\] \[path\]
|
||||
git [flags] [path]
|
||||
\end{verbatim}
|
||||
|
||||
Start a working environment
|
||||
|
@ -1164,7 +1164,7 @@ The ping command sends \textbf{ICMP Echo Request packets} to the remote server f
|
|||
Each \textbf{packet echoed back } (via an ICMP Echo Response packet) is written to the shell output.
|
||||
|
||||
\begin{verbatim}
|
||||
ping \[-flag\] server
|
||||
ping [-flag] server
|
||||
\end{verbatim}
|
||||
|
||||
The main usages for the \texttt{ping} command are:
|
||||
|
@ -1257,7 +1257,7 @@ Flags
|
|||
With \texttt{shasum} command you can work with SHA Checksums.
|
||||
|
||||
\begin{verbatim}
|
||||
shasum \[flag\] \[file\]
|
||||
shasum [flag] [file]
|
||||
\end{verbatim}
|
||||
|
||||
Store the Checksum
|
||||
|
@ -1315,7 +1315,7 @@ Usage
|
|||
The default basename command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
basename \[flags\] \[string\] \[suffix\]
|
||||
basename [flags] [string] [suffix]
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the cat flags, read below for more info, the string command is
|
||||
|
@ -1347,7 +1347,7 @@ By writing the command \texttt{base64} you can encode and decode Base64 data.
|
|||
The command follows the structure:
|
||||
|
||||
\begin{verbatim}
|
||||
base64 \[flag\] \[-i input item\] \[-o output item\]
|
||||
base64 [flag] [-i input item] [-o output item]
|
||||
\end{verbatim}
|
||||
|
||||
Defining input and output files
|
||||
|
@ -1374,7 +1374,7 @@ Using the \texttt{-D} flag (which stands for decode), you obtain as
|
|||
output the input message decoded into binary data.
|
||||
|
||||
\begin{verbatim}
|
||||
base64 -D \[-i input item\] \[-o output item\]
|
||||
base64 -D [-i input item] [-o output item]
|
||||
\end{verbatim}
|
||||
|
||||
Summarize the tools
|
||||
|
@ -1424,7 +1424,7 @@ The \texttt{chroot} utility allows you to change its root directory to
|
|||
the one indicated in newroot and, if given, executes the command.\\
|
||||
|
||||
\begin{verbatim}
|
||||
chroot newroot \[command\]
|
||||
chroot newroot [command]
|
||||
\end{verbatim}
|
||||
|
||||
\section{watch}
|
||||
|
@ -1545,7 +1545,7 @@ Usage:
|
|||
There are different syntaxes for this command:
|
||||
|
||||
\begin{verbatim}
|
||||
tar {-c} \[options\] \[files | directories\]
|
||||
tar {-c} [options] [files | directories]
|
||||
\end{verbatim}
|
||||
|
||||
The first one is the default syntax. Where \texttt{{-c}} stays for the
|
||||
|
@ -1554,9 +1554,9 @@ we can use, \texttt{\[files | directories\]} for the files or directories
|
|||
what we want to compress.
|
||||
|
||||
\begin{verbatim}
|
||||
tar {-r | -u} -f archive-file \[options\] \[files | directories\]
|
||||
tar {-t | -x} \[options\] \[patterns\]
|
||||
tar \[bundled-flags args\] \[file | pattern ...\]
|
||||
tar {-r | -u} -f archive-file [options] [files | directories]
|
||||
tar {-t | -x} [options] [patterns]
|
||||
tar [bundled-flags args] [file | pattern ...]
|
||||
\end{verbatim}
|
||||
|
||||
The last one shows a bundled option word provided for compatibility with
|
||||
|
@ -1672,7 +1672,7 @@ all sections of the object files except the (\_\_TEXT,\_\_text) section.
|
|||
|
||||
|
||||
\begin{verbatim}
|
||||
strings \[options\] file\_name(s)
|
||||
strings [options] file_name(s)
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -1882,7 +1882,7 @@ Usage
|
|||
The default fg command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
fg \[\%job\_number\]
|
||||
fg [\%job_number]
|
||||
\end{verbatim}
|
||||
|
||||
The parameter \[\%job\_id\] contains the job number you wish to run in foreground.
|
||||
|
@ -1897,7 +1897,7 @@ you need to press the \texttt{ctrl + Z} keys. The job number to be used with the
|
|||
\begin{verbatim}
|
||||
top
|
||||
\^Z
|
||||
\[1\] + NNNN suspended top
|
||||
[1] + NNNN suspended top
|
||||
\end{verbatim}
|
||||
|
||||
\section{apropos}
|
||||
|
@ -1913,7 +1913,7 @@ it will output all command related to the keyword \\
|
|||
Example with the command \texttt{apropos archive}, the shell will output tar, zip ecc...\\
|
||||
|
||||
\begin{verbatim}
|
||||
apropos \[flags\] keyword
|
||||
apropos [flags] keyword
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -1940,7 +1940,7 @@ Usage
|
|||
The default passwd command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
passwd \[flags\] \[user\]
|
||||
passwd [flags] [user]
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the passwd flags, read below for more info, the parameter \[user\] can only be
|
||||
|
@ -2095,7 +2095,7 @@ Usage:
|
|||
The default tee command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
tee \[flags\] \[file\]
|
||||
tee [flags] [file]
|
||||
\end{verbatim}
|
||||
|
||||
Where \texttt{\[flags\]} are the tee flags (below you will find more info),
|
||||
|
@ -2137,7 +2137,7 @@ Usage
|
|||
The default cat command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
cat \[flags\] \[--\] \[file1\] \[file2\] \[file...\]
|
||||
cat [flags] [--] [file1] [file2] [file...]
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the \texttt{cat} flags, read below for more info, \[--\] indacates that the
|
||||
|
@ -2212,9 +2212,9 @@ You have to know also that "killall" sends SIGTERM, or signal number 15.
|
|||
If you want to specify a different signal you can use "-s".
|
||||
|
||||
\begin{verbatim}
|
||||
killall -s 9 \[process name\]
|
||||
killall -KILL \[process name\]
|
||||
killall -9 \[process name\]
|
||||
killall -s 9 [process name]
|
||||
killall -KILL [process name]
|
||||
killall -9 [process name]
|
||||
\end{verbatim}
|
||||
|
||||
kill
|
||||
|
@ -2224,9 +2224,9 @@ Without options, kill sends SIGTERM to the PID specified and asks the applicatio
|
|||
or service to shut itself down.
|
||||
|
||||
\begin{verbatim}
|
||||
kill \[PID\]
|
||||
kill -s KILL \[PID\]
|
||||
kill -KILL \[PID\]
|
||||
kill [PID]
|
||||
kill -s KILL [PID]
|
||||
kill -KILL [PID]
|
||||
\end{verbatim}
|
||||
|
||||
\section{curl}
|
||||
|
@ -2323,7 +2323,7 @@ Every process is an executing instance of a program which is assigned a unique P
|
|||
The basic syntax of \emph{ps} is:
|
||||
|
||||
\begin{verbatim}
|
||||
ps \[options\]
|
||||
ps [options]
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -2404,8 +2404,8 @@ To log in a remote server you need an user account which is
|
|||
usually created by the server admin and a password.
|
||||
|
||||
\begin{verbatim}
|
||||
localhost:\[~\]\textgreater ssh username@remote-server
|
||||
remote-server:\[~\]\textgreater
|
||||
localhost:[~]\textgreater ssh username@remote-server
|
||||
remote-server:[~]\textgreater
|
||||
\end{verbatim}
|
||||
|
||||
Once you're logged in, the local machine shell will be
|
||||
|
@ -2422,7 +2422,7 @@ Log out of a server
|
|||
Run a command on a remote machine
|
||||
|
||||
\begin{verbatim}
|
||||
localhost:\[~\]:\textgreater ssh user@remote-host "ls test"
|
||||
localhost:[~]:\textgreater ssh user@remote-host "ls test"
|
||||
online-backup.dat
|
||||
online-storage
|
||||
unix-dedicated-server.txt
|
||||
|
@ -2434,15 +2434,15 @@ Debug the client
|
|||
Debugging the ssh client can be useful to resolve connection errors.
|
||||
|
||||
\begin{verbatim}
|
||||
localhost:\[~\]:\textgreater ssh -v user@remote-host
|
||||
OpenSSH\_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
|
||||
debug1: Reading configuration data /etc/ssh/ssh\_config
|
||||
localhost:[~]:\textgreater ssh -v user@remote-host
|
||||
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
|
||||
debug1: Reading configuration data /etc/ssh/ssh_config
|
||||
debug1: Applying options for *
|
||||
debug1: Connecting to remote-host \[172.22.200.140\] port 22.
|
||||
debug1: Connecting to remote-host [172.22.200.140] port 22.
|
||||
debug1: Connection established.
|
||||
debug1: identity file /home/user/.ssh/identity type -1
|
||||
debug1: identity file /home/user/.ssh/id\_rsa type -1
|
||||
debug1: identity file /home/user/.ssh/id\_dsa type 2
|
||||
debug1: identity file /home/user/.ssh/id_rsa type -1
|
||||
debug1: identity file /home/user/.ssh/id_dsa type 2
|
||||
debug1: loaded 3 keys
|
||||
...
|
||||
\end{verbatim}
|
||||
|
@ -2847,7 +2847,7 @@ You can notice that the top lines in the window follow this structure:
|
|||
|
||||
\begin{verbatim}
|
||||
Last login: Day Month hh:mm:ss on console
|
||||
Device-name:~ username\$
|
||||
Device-name:~ username$
|
||||
\end{verbatim}
|
||||
|
||||
The tilde (~) just before your username means that the current position in
|
||||
|
@ -2876,7 +2876,7 @@ Use chgrp \[:group\] file to create a group for a file \\
|
|||
Use Sudo chown owner\[:group\], if you need root permission for add the user to the group
|
||||
|
||||
\begin{verbatim}
|
||||
chown user \[group\] file1 file2 ...
|
||||
chown user [group] file1 file2 ...
|
||||
\end{verbatim}
|
||||
|
||||
\begin{itemize}
|
||||
|
@ -2923,8 +2923,8 @@ General syntax for both is:
|
|||
|
||||
|
||||
\begin{verbatim}
|
||||
less \[flags\] path/name-file.*
|
||||
more \[flags\] path/name-file.*
|
||||
less [flags] path/name-file.*
|
||||
more [flags] path/name-file.*
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -3008,7 +3008,7 @@ The name stands for \emph{LiSt}.
|
|||
The default ls command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
ls \[flags\] \[path\]
|
||||
ls [flags] [path]
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the ls flags, read below for more info,and \[path\] is the
|
||||
|
@ -3050,7 +3050,7 @@ the displayed list using the \texttt{-a} flag.
|
|||
ls -a Photos
|
||||
. 01.png
|
||||
.. 02.png
|
||||
.DS\_Store 03.png
|
||||
.DS_Store 03.png
|
||||
.secret.png 04.jpg
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -3121,7 +3121,7 @@ The \texttt{info} command allows you to read that kind of files
|
|||
and to search for particular information by using special flags.
|
||||
|
||||
\begin{verbatim}
|
||||
info \[flag\] \[item\]
|
||||
info [flag] [item]
|
||||
\end{verbatim}
|
||||
|
||||
Look up for a specific string
|
||||
|
@ -3222,11 +3222,11 @@ you have to create the text files if they don't exist (with some text
|
|||
inside), to make the work.
|
||||
|
||||
\begin{verbatim}
|
||||
say -v Alex -f hello\_world.txt
|
||||
say -v Alex -f hello_world.txt
|
||||
\end{verbatim}
|
||||
|
||||
\begin{verbatim}
|
||||
say -o hi.aac -f hello\_world.txt
|
||||
say -o hi.aac -f hello_world.txt
|
||||
\end{verbatim}
|
||||
|
||||
\section{ln}
|
||||
|
@ -3255,9 +3255,9 @@ The symbolic link (symlink) instead contains the path to the original file and r
|
|||
will make the referencer point to the original file instead.\\
|
||||
|
||||
\begin{verbatim}
|
||||
ln \[flags\] \[source1\] \[source2\] ... \[target\_dir\]
|
||||
ln \[flags\] \[source\] \[target\]
|
||||
link \[source\] \[target\]
|
||||
ln [flags] [source1] [source2] ... [target_dir]
|
||||
ln [flags] [source] [target]
|
||||
link [source] [target]
|
||||
\end{verbatim}
|
||||
|
||||
The \texttt{link} command can be used instead of \texttt{ln}, but flags and multiple
|
||||
|
@ -3287,7 +3287,7 @@ The \texttt{du} command is used to display files and directories sizes.\\
|
|||
The name stands for \emph{Disk Usage}.
|
||||
|
||||
\begin{verbatim}
|
||||
du \[flags\] \[file1\] \[file2\] \[directory1\] ...
|
||||
du [flags] [file1] [file2] [directory1] ...
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -3354,7 +3354,7 @@ but with a different name.\\
|
|||
It stands for \emph{MoVe}.
|
||||
|
||||
\begin{verbatim}
|
||||
mv \[-finv\] source target
|
||||
mv [-finv] source target
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -3428,7 +3428,7 @@ If the permissions of the file do not permit writing, the user is prompted for c
|
|||
It stands for \emph{ReMove}.
|
||||
|
||||
\begin{verbatim}
|
||||
rm \[-dfiPRrvW\] file1 file2 file....
|
||||
rm [-dfiPRrvW] file1 file2 file....
|
||||
\end{verbatim}
|
||||
|
||||
Flags
|
||||
|
@ -3507,7 +3507,7 @@ The name stands for \emph{MaKe DIRectory}.\\
|
|||
The default ls command syntax is:
|
||||
|
||||
\begin{verbatim}
|
||||
mkdir \[flags\] \[-m mode\] directory\_name ...
|
||||
mkdir [flags] [-m mode] directory_name ...
|
||||
\end{verbatim}
|
||||
|
||||
Where \[flags\] are the mkdir \texttt{-p} and \texttt{-v} - flags,
|
||||
|
@ -3520,9 +3520,9 @@ Let's see how to create a new directory:
|
|||
\begin{verbatim}
|
||||
ls
|
||||
|
||||
mkdir test\_directory
|
||||
mkdir test_directory
|
||||
ls
|
||||
test\_directory
|
||||
test_directory
|
||||
\end{verbatim}
|
||||
|
||||
Create a path of directories
|
||||
|
@ -3530,7 +3530,7 @@ Using the flag \texttt{-p} we can create a path of directories, allowing us
|
|||
to build more than a directory at once.
|
||||
|
||||
\begin{verbatim}
|
||||
mkdir -p test\_directory/subdir/subsubdir
|
||||
mkdir -p test_directory/subdir/subsubdir
|
||||
\end{verbatim}
|
||||
|
||||
As you can see, we are now creating two directories: one named \emph{subdir} and
|
||||
|
@ -3543,7 +3543,7 @@ The \texttt{-m mode} option allows us to set permissions at the new directory
|
|||
that we are now creating.
|
||||
|
||||
\begin{verbatim}
|
||||
mkdir -m 777 test\_free\_directory
|
||||
mkdir -m 777 test_free_directory
|
||||
\end{verbatim}
|
||||
|
||||
Our new directory will now have read,write and execute permissions for user, group and others.
|
||||
|
@ -3602,7 +3602,7 @@ The default cd command syntax is:
|
|||
|
||||
|
||||
\begin{verbatim}
|
||||
cd \[flags\] \[path\]
|
||||
cd [flags] [path]
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -3679,7 +3679,7 @@ input as output
|
|||
To redirect an input from a file for a command, the symbol "\textless " is used.
|
||||
|
||||
\begin{verbatim}
|
||||
echo \$lt; \$(cat hello.txt)
|
||||
echo $lt; $(cat hello.txt)
|
||||
Sun
|
||||
Moon
|
||||
\end{verbatim}
|
||||
|
@ -3705,8 +3705,8 @@ We want to sort the animals whose name begins with letter "d" from the file
|
|||
\emph{animals.txt} and put it into \emph{d\_animals.txt}.\\
|
||||
|
||||
\begin{verbatim}
|
||||
grep d \textless animals.txt \textgreater d\_animals.txt
|
||||
cat d\_animals.txt
|
||||
grep d \textless animals.txt \textgreater d_animals.txt
|
||||
cat d_animals.txt
|
||||
Deer
|
||||
Dog
|
||||
Dolphin
|
||||
|
@ -3728,7 +3728,7 @@ This command print as output its entire argument on the command-line.\\
|
|||
\begin{verbatim}
|
||||
example="this is an example"
|
||||
|
||||
echo \$example
|
||||
echo $example
|
||||
this is an example
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -3785,7 +3785,7 @@ that make them suitable for different needs while programming.\\
|
|||
|
||||
While loops take this form:
|
||||
\begin{verbatim}
|
||||
while \[condition\]
|
||||
while [condition]
|
||||
do
|
||||
command1
|
||||
command2
|
||||
|
@ -3800,10 +3800,10 @@ Here is a first simple example:
|
|||
\begin{verbatim}
|
||||
i=0;
|
||||
|
||||
while \[\$i -lt 4\]
|
||||
while [$i -lt 4]
|
||||
do
|
||||
echo \$i
|
||||
i=\$((i + 1))
|
||||
echo $i
|
||||
i=$((i + 1))
|
||||
done
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -3832,16 +3832,16 @@ i=1;
|
|||
|
||||
while :
|
||||
do
|
||||
printf "i=\$i\i: Hello World"
|
||||
if \[ \$i == 3 \]; then
|
||||
printf "i=$i\i: Hello World"
|
||||
if [ $i == 3 ]; then
|
||||
echo "I love DrRacket"
|
||||
elif \[ \$i == 5\]; then
|
||||
elif [ $i == 5]; then
|
||||
echo "I love Bash"
|
||||
elif \[ \$i == 7 \]; then
|
||||
elif [ $i == 7 ]; then
|
||||
echo "I love this website"
|
||||
elif \[ \$i == 9 \]; then
|
||||
elif [ $i == 9 ]; then
|
||||
exit 0
|
||||
i=\$((i + 1))
|
||||
i=$((i + 1))
|
||||
done
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -3884,10 +3884,10 @@ done
|
|||
In scripting, while loops are often used to process files line by line. \\
|
||||
Here is an example:
|
||||
\begin{verbatim}
|
||||
while read -r first\_name last\_name phone;
|
||||
while read -r first_name last_name phone;
|
||||
do
|
||||
printf '\%s\\n' "\$last\_name"
|
||||
done \textless "\$file"
|
||||
printf '\%s\\n' "$last_name"
|
||||
done \textless "$file"
|
||||
\end{verbatim}
|
||||
|
||||
The \texttt{ read } command is used to read a file line by line.
|
||||
|
@ -3915,7 +3915,7 @@ whether run or not a piece of code based on a condition that we set. \\
|
|||
|
||||
If statements take this form:
|
||||
\begin{verbatim}
|
||||
if \[condition\]; then
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
|
@ -3930,7 +3930,7 @@ Here is a simple example:
|
|||
\begin{verbatim}
|
||||
i=210;
|
||||
|
||||
if \[\$i -ge 200\]; then
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
fi
|
||||
\end{verbatim}
|
||||
|
@ -3954,7 +3954,7 @@ true and another set of actions if our condition evaluates to false. We can do t
|
|||
the \emph{ if else } statement.
|
||||
\emph{ if else } sattements take this form:
|
||||
\begin{verbatim}
|
||||
if \[condition\]; then
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
|
@ -3971,7 +3971,7 @@ Here is a simple example:
|
|||
\begin{verbatim}
|
||||
i=50;
|
||||
|
||||
if \[\$i -ge 200\]; then
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
else
|
||||
echo "You chose a small number."
|
||||
|
@ -3998,12 +3998,12 @@ different paths. We can accommodate this need with the \emph{if else elif} mecha
|
|||
The \emph{if else elif} mechanism takes this form:
|
||||
|
||||
\begin{verbatim}
|
||||
if \[condition\]; then
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
elif \[condition\]; then
|
||||
elif [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
|
@ -4021,9 +4021,9 @@ Here is a simple example:
|
|||
\begin{verbatim}
|
||||
i=150;
|
||||
|
||||
if \[\$i -ge 200\]; then
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
elif \[\$i == 150\]; then
|
||||
elif [$i == 150]; then
|
||||
echo "You chose 150".
|
||||
else
|
||||
echo "You chose a small number"
|
||||
|
@ -4051,8 +4051,8 @@ Therefore, the output of this piece of code is:
|
|||
The second type of loops are for loops.
|
||||
They follow this syntax:
|
||||
\begin{verbatim}
|
||||
for \[variable\] in \[list\] do
|
||||
\[code\]
|
||||
for [variable] in [list] do
|
||||
[code]
|
||||
done
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -4078,7 +4078,7 @@ Here is a simple example:
|
|||
\begin{verbatim}
|
||||
for i in 1 2 3 4 5
|
||||
do
|
||||
echo "Welcome \$i times"
|
||||
echo "Welcome $i times"
|
||||
done
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -4124,10 +4124,10 @@ Other characters can't be used because they have a special meaning in Unix Shell
|
|||
Some simple examples are:
|
||||
|
||||
\begin{verbatim}
|
||||
VAR\_1
|
||||
VAR\_2
|
||||
NAME\_3
|
||||
name\_4
|
||||
VAR_1
|
||||
VAR_2
|
||||
NAME_3
|
||||
name_4
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -4136,14 +4136,14 @@ Defining a variable
|
|||
To define a certain variable, we could use the following basecase:
|
||||
|
||||
\begin{verbatim}
|
||||
variable\_name=variable\_value
|
||||
variable_name=variable_value
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
Let me show you a simple example:
|
||||
|
||||
\begin{verbatim}
|
||||
VAR\_1=Strawberry
|
||||
VAR_1=Strawberry
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -4151,8 +4151,8 @@ To access a variable we have to use the dollar sign (\$). So if I want to
|
|||
access VAR\_1, I have to write:
|
||||
|
||||
\begin{verbatim}
|
||||
VAR\_1="Strawberry"
|
||||
echo \$VAR\_1
|
||||
VAR_1="Strawberry"
|
||||
echo $VAR_1
|
||||
Strawberry
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -4164,13 +4164,13 @@ those that it tracks.\\
|
|||
To delete a variable we use the following command:
|
||||
|
||||
\begin{verbatim}
|
||||
unset variable\_name
|
||||
unset variable_name
|
||||
\end{verbatim}
|
||||
|
||||
which in our case would be:
|
||||
|
||||
\begin{verbatim}
|
||||
unset VAR\_1
|
||||
unset VAR_1
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
|
@ -4181,18 +4181,18 @@ changed or deleted.\\
|
|||
So, if we try to change the value of VAR\_1, the result will be the following:
|
||||
|
||||
\begin{verbatim}
|
||||
VAR\_1="Strawberry"
|
||||
readonly VAR\_1
|
||||
VAR\_1="Blueberry"
|
||||
VAR\_1: This variable is read only.
|
||||
VAR_1="Strawberry"
|
||||
readonly VAR_1
|
||||
VAR_1="Blueberry"
|
||||
VAR_1: This variable is read only.
|
||||
\end{verbatim}
|
||||
|
||||
If we try to delete the variable, shell will give us the following value:
|
||||
|
||||
\begin{verbatim}
|
||||
VAR\_1="Strawberry"
|
||||
unset VAR\_1
|
||||
VAR\_1: This variable is read only.
|
||||
VAR_1="Strawberry"
|
||||
unset VAR_1
|
||||
VAR_1: This variable is read only.
|
||||
\end{verbatim}
|
||||
|
||||
\section{Parameter expansion}
|
||||
|
@ -4207,14 +4207,14 @@ There are some special operations that can be performed on
|
|||
The general syntax for all parameter expansions is this one:
|
||||
|
||||
\begin{verbatim}
|
||||
\${CODE\_HERE}
|
||||
${CODE_HERE}
|
||||
\end{verbatim}
|
||||
|
||||
Depending on what you want to do with your variable, the code
|
||||
that goes inside the braces differs.\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE:-DEFAULT\_VALUE}
|
||||
${VARIABLE:-DEFAULT_VALUE}
|
||||
\end{verbatim}
|
||||
|
||||
If the variable VARIABLE exists and has a value (i.e. it is not null), this is equal to
|
||||
|
@ -4224,7 +4224,7 @@ Otherwise, it is equal to DEFAULT\_VALUE.\\
|
|||
Example: \texttt{echo "First name: \${firstname:-John}";}\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE:=DEFAULT\_VALUE}
|
||||
${VARIABLE:=DEFAULT_VALUE}
|
||||
\end{verbatim}
|
||||
|
||||
If the variable VARIABLE exists and has a value, this is equal to the value of VARIABLE.
|
||||
|
@ -4232,7 +4232,7 @@ Otherwise, it sets the variable to DEFAULT\_VALUE and is equal to it.\\
|
|||
Example: \texttt{echo "Last name: \${lastname:=Doe}";}\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE:?ERR\_VALUE}
|
||||
${VARIABLE:?ERR_VALUE}
|
||||
\end{verbatim}
|
||||
|
||||
If the variable VARIABLE exists and has a value, this is equal to the value of VARIABLE.
|
||||
|
@ -4241,7 +4241,7 @@ after this).\\
|
|||
Example: \texttt{currdate=\${date:?Operation failed: date unknown};}\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE:+VALUE}
|
||||
${VARIABLE:+VALUE}
|
||||
\end{verbatim}
|
||||
|
||||
If the variable VARIABLE exists and has a value, this is equal to VALUE.
|
||||
|
@ -4253,7 +4253,7 @@ All of these can also be written without the colon, in which case their meaning
|
|||
"If the variable VARIABLE exists at all (even if it is null), this is ..."\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE: NUMBER}
|
||||
${VARIABLE: NUMBER}
|
||||
\end{verbatim}
|
||||
|
||||
This is equal to the substring of the value of VARIABLE, starting at the character with
|
||||
|
@ -4262,7 +4262,7 @@ If NUMBER is negative, the substring starts NUMBER characters before the end of
|
|||
Example: \texttt{lastname=\${fullname:\$firstnamelength};}\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${VARIABLE: FROM:LENGTH}
|
||||
${VARIABLE: FROM:LENGTH}
|
||||
\end{verbatim}
|
||||
|
||||
This is equal to the substring of the value of VARIABLE, starting at the character with (0-based)
|
||||
|
@ -4271,7 +4271,7 @@ If FROM is negative, the substring starts FROM characters before the end of the
|
|||
Example: \texttt{lastname=\${middlename:\$firstnamelength:\$middlenamelength};}\\
|
||||
|
||||
\begin{verbatim}
|
||||
\${\#VARIABLE}
|
||||
${#VARIABLE}
|
||||
\end{verbatim}
|
||||
|
||||
This is equal to the length of the value of VARIABLE\\
|
||||
|
@ -4280,7 +4280,7 @@ Example: \texttt{echo "your name has \${\#name} characters";}\\
|
|||
Paremeter expansions can also be nested, like this:
|
||||
|
||||
\begin{verbatim}
|
||||
\${input:?\${INVALID\_INPUT\_ERR\_MSG:-An unknown error occurred}}
|
||||
${input:?${INVALID_INPUT_ERR_MSG:-An unknown error occurred}}
|
||||
\end{verbatim}
|
||||
|
||||
Further reading: <a href="http://www.gnu.org/software/bash/manual/bashref.html\#Shell-Parameter-Expansion">
|
||||
|
@ -4301,7 +4301,7 @@ To begin, we will see the simplest variable, which is the dollar sign (\$).
|
|||
This command simply gives us the process ID number of the current shell.\\
|
||||
|
||||
\begin{verbatim}
|
||||
echo \$\$
|
||||
echo $$
|
||||
11480
|
||||
\end{verbatim}
|
||||
|
||||
|
@ -4338,7 +4338,7 @@ This will create an array, called \texttt{names}, which holds 4 values.
|
|||
The values in the array can be accessed using the syntax
|
||||
|
||||
\begin{verbatim}
|
||||
\${names\[SELECTOR\]}
|
||||
${names[SELECTOR]}
|
||||
\end{verbatim}
|
||||
|
||||
where SELECTOR is the selector associated to the desired element. There are two kinds of
|
||||
|
@ -4355,7 +4355,7 @@ If we want to specify a particular index for our values when creating the array
|
|||
we can do it like this:
|
||||
|
||||
\begin{verbatim}
|
||||
names=(\[1\]="Anna" \[2\]="Bob" \[5\]="Ernie" \[12\]="Luigi")
|
||||
names=([1]="Anna" [2]="Bob" [5]="Ernie" [12]="Luigi")
|
||||
\end{verbatim}
|
||||
|
||||
As you might have guessed, this will create an array with \texttt{"Anna"} at index 1,
|
||||
|
@ -4364,7 +4364,7 @@ As you might have guessed, this will create an array with \texttt{"Anna"} at ind
|
|||
If our indices are all sequential and we just want to change the starting index, we can use
|
||||
|
||||
\begin{verbatim}
|
||||
names=(\[12\]="Luigi" "Mario" "Nate")
|
||||
names=([12]="Luigi" "Mario" "Nate")
|
||||
\end{verbatim}
|
||||
|
||||
Which will create an array with \emph{"Luigi"} at index 12,
|
||||
|
@ -4379,7 +4379,7 @@ In an associative array, the values are not mapped to a number but to some other
|
|||
Here's an example of an associative array:
|
||||
|
||||
\begin{verbatim}
|
||||
colors=(\[white\]="\#FFFFFF" \[black\]="\#000000" \[red\]="\#FF0000" \[yellow\]="\#00FFFF")
|
||||
colors=([white]="#FFFFFF" [black]="#000000" [red]="#FF0000" [yellow]="#00FFFF")
|
||||
\end{verbatim}
|
||||
|
||||
In this case it is not possible to omit the selector as there is no logical word
|
||||
|
@ -4388,13 +4388,13 @@ instead of a number as the selector.\\
|
|||
Arrays, both kinds, can also be modified after creation. We can use
|
||||
|
||||
\begin{verbatim}
|
||||
colors\[blue\]="\#0000FF"
|
||||
colors[blue]="#0000FF"
|
||||
\end{verbatim}
|
||||
|
||||
to add an element to the array (or modify it if it exists already) and
|
||||
|
||||
\begin{verbatim}
|
||||
unset colors\[black\]
|
||||
unset colors[black]
|
||||
\end{verbatim}
|
||||
|
||||
to remove one.\\
|
||||
|
@ -4409,13 +4409,13 @@ much like it works on strings, but it manipulates the members of the array inste
|
|||
For example:
|
||||
|
||||
\begin{verbatim}
|
||||
\${names\[@\]: 13}
|
||||
${names[@]: 13}
|
||||
\end{verbatim}
|
||||
|
||||
is equivalent to \texttt{"Mario" "Luigi"}, while
|
||||
|
||||
\begin{verbatim}
|
||||
\${\#names\[@\]}
|
||||
${#names[@]}
|
||||
\end{verbatim}
|
||||
|
||||
counts the number of elements in the array, in this case 3.\\
|
|
@ -7,7 +7,6 @@ author: Nicola Brunner
|
|||
title: Legal notice and copyright information
|
||||
---
|
||||
|
||||
<br>
|
||||
<h3>© 2018 THE SHELL - All rights reserved.</h3>
|
||||
<p>Any reproduction or distribution of material on the THE SHELL
|
||||
website for commercial purposes is prohibited without the express written
|
||||
|
|
Loading…
Reference in a new issue