Linux/BASH: Difference between revisions
< Linux
No edit summary |
m (→If) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Shebang == | |||
* <pre>#!/bin/bash</pre> | |||
== Output & Piping == | |||
* output to file | |||
<pre> | |||
command > file.txt # redirect output | |||
command >> file.txt # append to file | |||
command &> file.txt # stdout and stderr to file | |||
command &>> file.txt # stdout and stderr append to file | |||
command 2> file.txt # stderr to file | |||
command 2>> file.txt # stderr append to file | |||
command | tee -a file.txt # stdout to terminal and file (append) | |||
command |& tee -a file.txt # stdout+stderr to terminal and file (append) | |||
</pre>redirecting output<pre> | |||
command 2>&1 >/dev/null # stderr to stdout and stdout to null | |||
</pre> | |||
== If == | |||
* | |||
<pre> | |||
if [ condition ] | |||
then command1 | |||
else command2 | |||
fi | |||
</pre><syntaxhighlight lang="bash"> | |||
if [ -f file.txt ]; then echo "file exists"; fi | |||
</syntaxhighlight> | |||
== For loop == | |||
<syntaxhighlight lang="bash"> | |||
for OUTPUT in $(linuxcommand) | |||
do | |||
command1 on $OUTPUT | |||
command2 on $OUTPUT | |||
commandN | |||
done | |||
</syntaxhighlight> | |||
== Links == | |||
* https://devhints.io/bash | |||
* http://tldp.org/LDP/abs/html/special-chars.html | * http://tldp.org/LDP/abs/html/special-chars.html | ||
* http://tldp.org/LDP/abs/html/internalvariables.html | * http://tldp.org/LDP/abs/html/internalvariables.html | ||
Line 5: | Line 48: | ||
* http://www.pantz.org/software/shell/shelloneliners.html | * http://www.pantz.org/software/shell/shelloneliners.html | ||
[[Category:Linux/Bash]] | [[Category:Linux/Bash]] | ||
[[Category:Linux]] |
Latest revision as of 10:18, 21 January 2021
Shebang
#!/bin/bash
Output & Piping
- output to file
command > file.txt # redirect output command >> file.txt # append to file command &> file.txt # stdout and stderr to file command &>> file.txt # stdout and stderr append to file command 2> file.txt # stderr to file command 2>> file.txt # stderr append to file command | tee -a file.txt # stdout to terminal and file (append) command |& tee -a file.txt # stdout+stderr to terminal and file (append)
redirecting output
command 2>&1 >/dev/null # stderr to stdout and stdout to null
If
if [ condition ] then command1 else command2 fi
if [ -f file.txt ]; then echo "file exists"; fi
For loop
for OUTPUT in $(linuxcommand)
do
command1 on $OUTPUT
command2 on $OUTPUT
commandN
done
Links
- https://devhints.io/bash
- http://tldp.org/LDP/abs/html/special-chars.html
- http://tldp.org/LDP/abs/html/internalvariables.html
- http://wiki.laub-home.de/index.php/Shell_Skripte_-_What_to_know_about
- http://wiki.laub-home.de/index.php/Shell_Skripte_-_eine_kleine_Sammlung
- http://www.pantz.org/software/shell/shelloneliners.html