snipt

Ctrl+h for KB shortcuts

Bash

redirects stderr to stdout and into the named file.

bad_command >> filename 2>&1
https://snipt.net/embed/97c0ff1f0250c6ef9df9b060f3585daf/
/raw/97c0ff1f0250c6ef9df9b060f3585daf/
97c0ff1f0250c6ef9df9b060f3585daf
bash
Bash
1
2019-07-16T01:15:01
True
False
False
/api/public/snipt/28009/
redirects-stderr-to-stdout-and-into-the-named-file
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>bad_command &gt;&gt; filename 2&gt;<span class="p">&amp;</span>1 </span></pre></div> </td></tr></table>
"io redirection", bash, stderr, stdout

Bash

Check whether a file does not exist in Bash script

1
2
3
4
if [ ! -f /tmp/foo.txt ]
then
    echo "File not found!"
fi
https://snipt.net/embed/1793720d371e43bab75891b80779fc10/
/raw/1793720d371e43bab75891b80779fc10/
1793720d371e43bab75891b80779fc10
bash
Bash
4
2019-07-16T18:07:49
True
False
False
/api/public/snipt/14807/
check-whether-a-file-does-not-exist-in-bash-script
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">if</span> <span class="o">[</span> ! -f /tmp/foo.txt <span class="o">]</span> </span><span id="L-2"><a name="L-2"></a><span class="k">then</span> </span><span id="L-3"><a name="L-3"></a> <span class="nb">echo</span> <span class="s2">&quot;File not found!&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="k">fi</span> </span></pre></div> </td></tr></table>
"file does not exist", "file exists", bash, test

Bash

Assign value of $2 to $DIR unless $2 is empty, then assign '../otherdir/' to $DIR

DIR=${2:-'../otherdir/'}
https://snipt.net/embed/60a0fabc835212ee65c624a10e2ef613/
/raw/60a0fabc835212ee65c624a10e2ef613/
60a0fabc835212ee65c624a10e2ef613
bash
Bash
1
2019-07-11T09:00:42
True
False
False
/api/public/snipt/14678/
assign-value-of-2-to-dir-unless-2-is-empty-then-assign-otherdir-to-dir
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">DIR</span><span class="o">=</span><span class="si">${</span><span class="nv">2</span><span class="k">:-</span><span class="s1">&#39;../otherdir/&#39;</span><span class="si">}</span> </span></pre></div> </td></tr></table>
"default argument", "ternary operator", bash, scripting

Bash

Convert from PNG to TIFF and change density

convert PFS0481.png -density 300 -units PixelsPerInch PFS0481_300dpi.tif
https://snipt.net/embed/104255943808e4645685ff6aa48c633f/
/raw/104255943808e4645685ff6aa48c633f/
104255943808e4645685ff6aa48c633f
bash
Bash
1
2019-07-14T20:24:57
True
False
False
/api/public/snipt/14458/
convert-from-png-to-tiff-and-change-density
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>convert PFS0481.png -density <span class="m">300</span> -units PixelsPerInch PFS0481_300dpi.tif </span></pre></div> </td></tr></table>
bash, convert, density, imagemagick, png, tiff

MySQL

Export mySQL to CSV

1
2
3
4
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
FROM test_table;
https://snipt.net/embed/79a28e8da20bd538c988745d06ea4e5f/
/raw/79a28e8da20bd538c988745d06ea4e5f/
79a28e8da20bd538c988745d06ea4e5f
mysql
MySQL
4
2019-07-07T16:32:36
True
False
False
/api/public/snipt/6185/
export-mysql-to-csv
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">SELECT</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="k">INTO</span> <span class="k">OUTFILE</span> <span class="s1">&#39;/tmp/result.text&#39;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">FIELDS</span> <span class="k">TERMINATED</span> <span class="k">BY</span> <span class="s1">&#39;,&#39;</span> <span class="k">OPTIONALLY</span> <span class="k">ENCLOSED</span> <span class="k">BY</span> <span class="s1">&#39;&quot;&#39;</span> </span><span id="L-3"><a name="L-3"></a><span class="k">LINES</span> <span class="k">TERMINATED</span> <span class="k">BY</span> <span class="s1">&#39;\n&#39;</span> </span><span id="L-4"><a name="L-4"></a><span class="k">FROM</span> <span class="n">test_table</span><span class="p">;</span> </span></pre></div> </td></tr></table>
csv, export, mysql

Bash

checkout drupal contrib module

cvs -z6 -d:pserver:anonymous:[email protected]:/cvs/drupal-contrib checkout -r <version tag> -d moduleName contributions/modules/moduleName
https://snipt.net/embed/55ac008d59c1d06c0ee724b88b23f416/
/raw/55ac008d59c1d06c0ee724b88b23f416/
55ac008d59c1d06c0ee724b88b23f416
bash
Bash
1
2019-07-16T21:34:51
True
False
False
/api/public/snipt/3321/
checkout-drupal-contrib-module
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>cvs -z6 -d:pserver:anonymous:[email protected]:/cvs/drupal-contrib checkout -r &lt;version tag&gt; -d moduleName contributions/modules/moduleName </span></pre></div> </td></tr></table>
checkout, contrib, cvs, drupal, modules

Bash

delete .svn directories, good for copying sections of code to a new repository

find . -name ".svn" -type d -exec rm -rf {} \;
https://snipt.net/embed/6b5a5314f19ed662342d8f57f934507b/
/raw/6b5a5314f19ed662342d8f57f934507b/
6b5a5314f19ed662342d8f57f934507b
bash
Bash
1
2019-07-12T06:27:40
True
False
False
/api/public/snipt/3241/
delete-svn-directories-good-for-copying-sections-of-code-to-a-new-repository
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>find . -name <span class="s2">&quot;.svn&quot;</span> -type d -exec rm -rf <span class="o">{}</span> <span class="se">\;</span> </span></pre></div> </td></tr></table>
bash, exec, find, svn

PHP

select all nodes with no children

$nodesWithoutChildren = $xpath->query('*[not(*)]');
https://snipt.net/embed/5f10d8cd312a925702ccdc6cd6fbe8c5/
/raw/5f10d8cd312a925702ccdc6cd6fbe8c5/
5f10d8cd312a925702ccdc6cd6fbe8c5
php
PHP
1
2019-07-14T05:53:48
True
False
False
/api/public/snipt/1206/
select-all-nodes-with-no-children
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">$nodesWithoutChildren = $xpath-&gt;query(&#39;*[not(*)]&#39;);</span> </span></pre></div> </td></tr></table>
children, node, xpath

PHP

PHP ternary operator

1
2
3
4
<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
?>
https://snipt.net/embed/9993a2570f3419514bbae2e7ce1f11c8/
/raw/9993a2570f3419514bbae2e7ce1f11c8/
9993a2570f3419514bbae2e7ce1f11c8
php
PHP
4
2019-07-11T17:19:18
True
False
False
/api/public/snipt/747/
php-ternary-operator
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// Example usage for: Ternary Operator</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">$action</span> <span class="o">=</span> <span class="p">(</span><span class="k">empty</span><span class="p">(</span><span class="nv">$_POST</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]))</span> <span class="o">?</span> <span class="s1">&#39;default&#39;</span> <span class="o">:</span> <span class="nv">$_POST</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">];</span> </span><span id="L-4"><a name="L-4"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
ternary