User Tools

Site Tools


bash_cheatsheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
bash_cheatsheet [2014/12/07 19:31] – [Fonctions] ginkobash_cheatsheet [2014/12/07 23:16] – [Redirection] ginko
Line 87: Line 87:
  
 ===== Redirection ===== ===== Redirection =====
 +[[http://www.losurs.org/docs/redirection|Deux trois trucs intéressants sur les redirections]]
   * Diriger plusieurs lignes vers un fichier :<code bash>/bin/cat <<EOM >$FILE   * Diriger plusieurs lignes vers un fichier :<code bash>/bin/cat <<EOM >$FILE
 text1 text1
Line 93: Line 94:
 text4 text4
 EOM</code> EOM</code>
 +  * Normal:
 +    * ''binary > file'' (send stdout to file)
 +    * ''binary 2> file'' (send stderr to file)
 +    * ''binary > file 2>&1'' (send stdout and stderr to file)
 +    * ''binary < file'' (take stdin from file) 
 +  * Append:
 +    * ''binary %%>>%% file'' (send stdout to end of file)
 +    * ''binary 2%%>>%% file'' (send stderr to end of file)
 +    * ''binary %%>>%% file 2>&1'' (send stdout and stderr to end of file)
 +    * ''binary %%<<%%x'' (take stdin until "x" occurs) 
 +  * Pipes:
 +    * ''binary1 | binary2'' (pipe stdout of binary1 to stdin of binary2)
 +    * ''binary1 2>&1 | binary2'' (pipe stdout and stderr of binary1 to binary2) 
 +
 +
 +==== The problem with cats ... ====
 +
 +Take a simple redirection example such as this:
 +
 +''cat file1 | tr -d '\015' > file2''
 +
 +You've wasted a process on cat, since you could accomplish the same thing (and have it execute faster!) by doing this:
 +
 +''tr -d '\015' < file1 > file2''
 +
 +==== Sending stderr through a pipe ====
 +
 +Sending only stderr down a pipe, while having stdout still go to the screen, is an interesting trick. It can be done by passing the stdout and stderr file descriptors to temporary file descriptors, and basically playing a game of 3 card monte with the values:
 +
 +''(binary 3>&1 1>&2 2>&3 3>&-) | mail me@somewhere.org &''
  
 ===== Fonctions ===== ===== Fonctions =====
bash_cheatsheet.txt · Last modified: 2019/02/13 16:07 by ginko