From 05373617acbdd4bfd842ed1ce3c97f2aa3bf2a90 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 26 Jan 2022 16:40:43 -0300 Subject: [PATCH] Style edition --- bash_tutorial.org | 226 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 214 insertions(+), 12 deletions(-) diff --git a/bash_tutorial.org b/bash_tutorial.org index 7db83bf..94a1cbc 100644 --- a/bash_tutorial.org +++ b/bash_tutorial.org @@ -163,9 +163,18 @@ the script in a directory that can be read from ~$PATH~ *** Option c: run from graphical interface +** Run R scripts + +Write the script and save it. Then go to the directory where the script +was saved and run this code from terminal + +#+begin_src bash +Rscript script.R +#+end_src + * Manuals -Use the command ~man~ or ~info~ followed by the name of the function or program +To explore the manuals use the command ~man~ or ~info~ followed by the name of the function or program #+begin_src bash man @@ -175,11 +184,15 @@ man | What manual page do you want? | | For example, try 'man man'. | +Search for information about the manual interface within ~man~ + : man man +Description of the system hierarchy + : man hier -Showing only the first four lines +Showing only the first four lines of "info" interface #+begin_src bash info | head -4 @@ -191,8 +204,18 @@ info | head -4 | This | is | the | Info | main | menu | (aka | directory | node). | | | | | A | few | useful | Info | commands: | | | | | | | | +Search for ~info~ page about Bash shell features + : info bash +The manuals can also be accessed from emacs using "M-x" (using the "meta" key or CTRL + x) + +: M-x info + +: M-x man + +: M-x woman + * Show command history : history @@ -216,7 +239,7 @@ Apertium can be used in the command line : echo Hola | apertium spa-eng : echo Hello | apertium eng-spa -"|" pipe lets the input to be passed to apertium +The pipe operator "~|~" is used to pass the input to apertium * Files and directories @@ -230,6 +253,92 @@ Create a file redirecting the shell output using "~>~" operator : echo I will put this text wthin a file > file2.txt +** Redirect input, output and standard error + +Everything in GNU systems is a file. The keyboard and the terminal are also a file. +The keyboard is used for input and the terminal is used by commands as output. + +The files can be found in directory "fd", which stands for file descriptor) + +#+begin_src bash +cd /dev/fd/ +ls -l +#+end_src + +#+RESULTS: +| total | 0 | | | | | | | | | | +| lr-x------ | 1 | user | user | 64 | dec | 15 | 10:31 | 0 | -> | /tmp/babel-yl1RTd/ob-input-izpXuk | +| l-wx------ | 1 | user | user | 64 | dec | 15 | 10:31 | 1 | -> | pipe:[42063] | +| lr-x------ | 1 | user | user | 64 | dec | 15 | 10:31 | 17 | -> | /dev/pts/0 | +| l-wx------ | 1 | user | user | 64 | dec | 15 | 10:31 | 2 | -> | /tmp/emacsA1M18o | + + * standard input stream is bound to /dev/fd/0 + * standard output (stout) stream is bound to /dev/fd/1 + * standard errror (stderr) stream is bound to /dev/fd/2 + +During interaction a terminal gets written to stdin (dev/fd/0), which +a command read. The command then do an action and writes the output +(stout or stderr) where it will read by the terminal and is displayed +to the user. + +To redirect use the operator `>` as follows: + * `command > file_to_redirect` + +The operator `>` redirects stout, i.e., is the same that 1>. This code +creates a new file with the list of files present in the actual directory: + +#+begin_src bash +ls -l > file_redirect.txt +cat file_redirect.txt +#+end_src + +#+RESULTS: +| total | 32 | | | | | | | | +| -rw-r--r-- | 1 | user | user | 15782 | dec | 15 | 10:49 | bash_tutorial.org | +| -rw-r--r-- | 1 | user | user | 0 | dec | 15 | 10:49 | file_redirect.txt | +| -rw-r--r-- | 1 | user | user | 11895 | dec | 9 | 17:44 | markdown.md | +| -rw-r--r-- | 1 | user | user | 121 | dec | 9 | 17:17 | README.org | + +Use "2>&1" to redirect stderr with the stout. Using an arbitrary word and redirect it +to the file should print a warning message that is redirected to the file: + +#+begin_src bash +ls -l > file_redirect.txt +paco >> file_redirect.txt 2>&1 +cat file_redirect.txt +#+end_src + +#+RESULTS: +| total | 32 | | | | | | | | +| -rw-r--r-- | 1 | user | user | 15995 | dec | 15 | 12:39 | bash_tutorial.org | +| -rw-r--r-- | 1 | user | user | 0 | dec | 15 | 13:41 | file_redirect.txt | +| -rw-r--r-- | 1 | user | user | 11895 | dec | 9 | 17:44 | markdown.md | +| -rw-r--r-- | 1 | user | user | 121 | dec | 9 | 17:17 | README.org | +| bash: | line | 2: | paco: | command | not | found | | | + + +Use the "<" operator to pass commands to the file: + +#+begin_src bash +ls -l > file_redirect.txt +paco >> file_redirect.txt 2>&1 +sort < file_redirect.txt +cat file_redirect.txt +#+end_src + +#+RESULTS: +| bash: | line | 2: | paco: | command | not | found | | | +| -rw-r--r-- | 1 | user | user | 0 | dec | 15 | 13:44 | file_redirect.txt | +| -rw-r--r-- | 1 | user | user | 121 | dec | 9 | 17:17 | README.org | +| -rw-r--r-- | 1 | user | user | 16501 | dec | 15 | 13:41 | bash_tutorial.org | +| total | 24 | | | | | | | | +| total | 24 | | | | | | | | +| -rw-r--r-- | 1 | user | user | 16501 | dec | 15 | 13:41 | bash_tutorial.org | +| -rw-r--r-- | 1 | user | user | 0 | dec | 15 | 13:44 | file_redirect.txt | +| -rw-r--r-- | 1 | user | user | 121 | dec | 9 | 17:17 | README.org | +| bash: | line | 2: | paco: | command | not | found | | | + + ** List files Use ~ls~ in the directory you want to explore @@ -277,11 +386,17 @@ Create directory : mkdir new_dir -Removes directory +Remove (unlink) a directory + +Some arguments: + + * -r: recursively unlink + * -v: verbose + * -f: force : rm -r new_dir -Removes a file +Remove a file : rm file.txt @@ -368,7 +483,7 @@ In the example a file "foo" is converted from ~odt~ to ~org~ Convert odt file "tcl_online3.odt" to pdf -: $ libreoffice --headless --convert-to pdf tcl_online3.odt +: libreoffice --headless --convert-to pdf tcl_online3.odt ** Using 'sed' to substitute text @@ -516,11 +631,11 @@ Showing the first two entries Back up of "home" - - c = create file - - v = verbose - - f = write to a file/device - - z = compress the file (gzip) "tar.bz" - - j = compress the file (bzip2) + * c = create file + * v = verbose + * f = write to a file/device + * z = compress the file (gzip) "tar.bz" + * j = compress the file (bzip2) : tar -czvf /tmp/home.tar.gz /home @@ -537,7 +652,68 @@ Showing the first two entries ** Searching -Print lines that match patterns +*** Search for files + +'find' can be used with multiple arguments such as: + + * -name: search for name + * -iname: search for case-insensitive name + * -type: type of file + * f = file + * d = directory + +Search within all the directory and subdirectories by name + +#+begin_src bash +find ./ -name "gitea*" +#+end_src + +#+RESULTS: +./imagens/gitea-sm.png + +Search for all the files ending in '.png' + +#+begin_src bash +find ./ -name "*.png" +#+end_src + +#+RESULTS: +./imagens/gitea-sm.png +./files/plot.png + +Search for empty files in 'home' + +#+begin_src bash +find /home -type f -empty +#+end_src + +'find' can also be used with grep to match patterns: + +#+begin_src bash +cd ~/Documentos/dat/intro_r/answers +find . | grep -E '.js|.png' +#+end_src + +#+RESULTS: +| ./plot.png | +| ./vizjs.js | +| ./Rplot.png | +| ./bipartiteD3Script.js | +| ./demo1.js | + +To find file with different name patters, 'find' can also be used as follows: + +#+begin_src bash + find . -type f \( -name "*.org" -o -name "*.md" \) +#+end_src + +#+RESULTS: +| ./README.org | +| ./bash_tutorial.org | + +*** Matching patterns + +Print lines within files that match patterns : grep @@ -550,6 +726,31 @@ grep 'root' /etc/passwd #+RESULTS: : root:x:0:0::/root:/bin/bash +Additional arguments can be used with ~grep~: + * r: recursive search (within directories) + * n: print line number + * i: case-insensitive search + * '*' Wildcards to search in all the directories + +Search within files in the actual directory with wildcards + +#+begin_src bash +grep -rni IFELSE * +#+end_src + +#+RESULTS: +|answers/repl_values.R:137: mutate(Status = ifelse(Status == "almost peak" &| +|answers/categ_colum.R:40:df$lead_likely <- ifelse(grepl("UNCOVERED CURB | SERVICE", df$remarks), "non-lead", "unknown")| +|docs/vectorization.org:354:(counting number of repeated value), and =ifelse= (vectorized if...else| + +'grep' can be used also to search for files in directories as shown in the anterior +example using grep with extended regular expressions ("-E") and 'find' + +: find . | grep -E '.js|.png' + +In this example, 'find' will be passed to 'grep' and any pattern matching +will be printed + ** Download files This code download the file and the argument ~-O~ is to rename the file @@ -557,6 +758,7 @@ This code download the file and the argument ~-O~ is to rename the file #+begin_example wget https://filetodownload.org -O new_file #+end_example + * Devices - mount /what /where