summaryrefslogtreecommitdiff
path: root/public/post/mess-with-bash-1/index.html
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2023-01-02 06:02:01 +0800
committergarhve <git@garhve.com>2023-01-02 06:02:01 +0800
commitbe772f40c42711de54a3331db2781b1511acba9d (patch)
tree0808a7750d3c1055b0e86071c219d872775b1f92 /public/post/mess-with-bash-1/index.html
parent3ae5ecf803ed2d4ece2c9da6d91aae0f075c5b0c (diff)
change to zola
Diffstat (limited to 'public/post/mess-with-bash-1/index.html')
-rw-r--r--public/post/mess-with-bash-1/index.html194
1 files changed, 194 insertions, 0 deletions
diff --git a/public/post/mess-with-bash-1/index.html b/public/post/mess-with-bash-1/index.html
new file mode 100644
index 0000000..3de81d7
--- /dev/null
+++ b/public/post/mess-with-bash-1/index.html
@@ -0,0 +1,194 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <meta charset="utf-8">
+
+<title>Mess with bash(1) | garhve&#x27;s gibberish</title>
+
+
+ <link rel="shortcut icon" type="image/png" href="&#x2F;images&#x2F;favicon.png">
+ <meta name="viewport" content="width=device-width,initial-scale=1">
+
+ <link id="stylesheet" rel="stylesheet" type="text/css" href="/dark.css">
+
+ <script type="text/javascript" src="/js/theme.js"></script>
+
+</head>
+
+<div class="header">
+ <div class="site_title">
+ <p><a href="/"><img src="https:&#x2F;&#x2F;blog.garhve.com&#x2F;images&#x2F;logo.ico" alt="garhve&#x27;s gibberish"
+ width="70" height=auto></a></p>
+ <p><a href="/">&nbsp;garhve&#x27;s gibberish</a></p>
+ </div>
+ <div class="menu">
+ <!-- <a href="/tags">tags</a>
+ &nbsp;--><a href="/categories">categories</a>
+ </div>
+</div>
+
+<body onload="getTheme()">
+ <section class="section">
+ <div class="container">
+
+<p>
+ <div class="title_postpage">Mess with bash(1)</div>
+</p>
+<p>
+ <div class="date_postpage">2022-08-25</div>
+ <div class="taxonomies_postpage">
+
+
+ <a href="https://blog.garhve.com/categories/code/">/code</a>
+
+
+
+
+ &emsp;<a href="https://blog.garhve.com/tags/bash/">#bash</a>
+
+
+ </div>
+</p>
+
+<p>
+ <h3 id="crontab">Crontab</h3>
+<p><code>crontab</code> is a useful tool, I really regret that I don't familar it earlier.</p>
+<p>It's usage really simple, and these two are my frequent using:</p>
+<pre data-lang="bash" style="background-color:#2e3440;color:#d8dee9;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#88c0d0;">crontab</span><span> -e </span><span style="color:#616e88;">#edit crontab file that reside in /var/spool/cron
+</span><span style="color:#88c0d0;">crontab</span><span> -l </span><span style="color:#616e88;">#list current crontab job
+</span></code></pre>
+<p>It basic syntax as follow, also really simple
+<img src="https://assets.garhve.com/pictures/screenshots/2022/08/1857817000.jpg" alt="crontab-layout.jpg" /></p>
+<p>example of usage:</p>
+<ol>
+<li>delete file <em>foo</em> every minute
+<code>* * * * * rm foo</code></li>
+<li>delete file <em>foo</em> every 15 minutes
+<code>15 * * * * rm foo</code></li>
+<li>delete file <em>foo</em> every beginning of hour
+<code>0 * * * * rm foo</code></li>
+<li>delete file <em>foo</em> every minute after 3 hours
+<code>* 3 * * * rm foo</code></li>
+<li>delete file <em>foo</em> every day at 18:30
+<code>30 18 * * * rm foo</code></li>
+<li>delete file <em>foo</em> every beginning of month
+<code>0 0 0 * * rm foo</code></li>
+<li>delete file <em>foo</em> on beginning of 1st,10th of month
+<code>0 0 1,10 * * rm foo</code></li>
+</ol>
+<p>The usage really simple, I now use it to renew my SSL certification and daily update bt-tracker.</p>
+<hr />
+<h3 id="tr">tr</h3>
+<p><code>tr</code> is really useful when encountered situation that needs struggle with string.
+Three frequency ways of using <code>tr</code></p>
+<ol>
+<li>
+<p><strong>shrink</strong> multiple characters into single one</p>
+<p><code>tr -s '[string]'</code>
+e.g. <code>echo &quot;ssssssspaaaaace&quot; | tr -s 'sa'</code> would convert &quot;ssssssspace&quot; to &quot;space&quot;</p>
+</li>
+<li>
+<p><strong>delete</strong> specific character. I usually use it to delete white space or delimiter</p>
+<p><code>tr -d '[string]'</code>
+e.g. <code>echo &quot;blog.garhve.com&quot; | tr -d '.o'</code> would convert url to &quot;blggarhvecm&quot;</p>
+</li>
+<li>
+<p><strong>convert</strong> specific character to another one.</p>
+<p><code>tr '[string1]' '[string2]'</code>
+e.g. <code>echo &quot;woopwon | tr &quot;wo&quot; &quot;fe&quot;</code> would result &quot;feepfen&quot;</p>
+</li>
+</ol>
+<hr />
+<h3 id="cut">cut</h3>
+<p>I use <code>cut</code> mostly to get word from a string, especially get relative path from absolute path. Because I always want to loop to get same sub-directories file from different main directory, <code>cut</code> helps a lot.</p>
+<p>for now, I only use it one way</p>
+<p><code>echo string | cut -d '[character]' -f position</code>
+e.g. <code>echo path/to/most/inner/file | cut -d '/' -f1</code> this will give me word before first '/', which is 'path'</p>
+<p>Often, <code>rev</code> will co-work with <code>cut</code> to get last one word</p>
+<p>e.g. <code>echo blog.garhve.com | rev | cut -d '.' -f 1 | rev</code> this will give word after last '/', which is com.</p>
+<blockquote>
+<p><em>a worth noting here is that the <code>rev</code> command needs to appear twice because it usage is not so intuitive, it reverse whole string</em></p>
+<p><code>echo &quot;hello world&quot; | rev</code> will get 'dlrow olleh`</p>
+<p><code>echo &quot;hello world&quot; | cut -d ' ' -f1 | rev</code> will give result of 'dlrow'</p>
+<p><code>echo &quot;hello world&quot; | rev | cut -d ' ' -f1</code> will give same result, as 'dlrow'</p>
+<p><code>echo &quot;hello world&quot; | rev | cut -d ' ' -f1 | rev</code> will give expected result, as 'world'</p>
+</blockquote>
+<h3 id="daily-update-bt-tracker">Daily update bt tracker</h3>
+<p>I already learn shell script for a while.. so I wrote a simple script to test whether I really got used to it, but result is obviously, I need more and more practice to memorize commands.</p>
+<pre data-lang="bash" style="background-color:#2e3440;color:#d8dee9;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#616e88;">#! /bin/sh
+</span><span style="color:#616e88;">#bt-tracker.txt
+</span><span>site</span><span style="color:#81a1c1;">=</span><span style="color:#a3be8c;">https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt
+</span><span>
+</span><span>file</span><span style="color:#81a1c1;">=</span><span style="color:#a3be8c;">/path_to_aria_directory/aria2.conf
+</span><span>
+</span><span>Addr</span><span style="color:#81a1c1;">=</span><span style="color:#a3be8c;">user@addr
+</span><span>
+</span><span style="color:#616e88;"># Get bt-tracker and format it to fulfill aria needs, then store in variable
+</span><span>bt</span><span style="color:#81a1c1;">=$</span><span style="color:#a3be8c;">(</span><span style="color:#88c0d0;">curl </span><span style="color:#81a1c1;">$</span><span>site </span><span style="color:#81a1c1;">| </span><span style="color:#88c0d0;">tr</span><span> -s </span><span style="color:#a3be8c;">&#39;[:space:]&#39; </span><span style="color:#81a1c1;">| </span><span style="color:#88c0d0;">tr </span><span style="color:#a3be8c;">&#39;[:space:]&#39; &#39;,&#39;)
+</span><span>
+</span><span style="color:#616e88;"># sshpass is a software, that can allow me pass ssh password as argument
+</span><span style="color:#616e88;"># so that I don&#39;t need to wait prompt
+</span><span style="color:#616e88;"># 410 is the line of bt tracker resides, I now no other way to replace it.
+</span><span style="color:#81a1c1;">$</span><span style="color:#88c0d0;">(sshpass</span><span> -p </span><span style="color:#a3be8c;">&#39;password&#39;</span><span style="color:#88c0d0;"> ssh</span><span> -T </span><span style="color:#81a1c1;">$</span><span>Addr </span><span style="color:#a3be8c;">&quot;sed -i &#39;410d&#39; </span><span style="color:#81a1c1;">$</span><span>file</span><span style="color:#a3be8c;"> | echo </span><span style="color:#81a1c1;">$</span><span>bt</span><span style="color:#a3be8c;"> &gt;&gt; </span><span style="color:#81a1c1;">$</span><span>file</span><span style="color:#a3be8c;">&quot;</span><span style="color:#88c0d0;">)
+</span></code></pre>
+<blockquote>
+<p>Usage of <a href="https://blog.garhve.com/index.php/archives/23/#tr">tr</a> and <a href="https://blog.garhve.com/index.php/archives/23/#cut">cut</a>, expansion of 'command tips' is needed</p>
+</blockquote>
+<h3 id="ways-to-find-files-or-specific-string-in-files">Ways to find files or specific string in files</h3>
+<p>In order to look up C definitions, I need to know where linux stores header files or which files store definitions I need. So that here comes up some methods to fulfill this need:</p>
+<ol>
+<li><code>find 'path' -name 'file_name'</code></li>
+</ol>
+<blockquote>
+<p>e.g. <code>find / -name stdio.h</code> will return multiple location that stdio.h resides. this could expand to look for others</p>
+<p><img src="https://assets.garhve.com/pictures/screenshots/2022/09/find_name.png" alt="image.png" /></p>
+<p>more info could see <code>man find</code></p>
+</blockquote>
+<hr />
+<ol start="2">
+<li><code>grep -rnw 'path' -e 'pattern'</code></li>
+</ol>
+<blockquote>
+<p>e.g. `grep --include=*.{h,c} -rnw / -e &quot;from_kuid&quot; will return string and filename that contains string.</p>
+<p><img src="https://assets.garhve.com/pictures/screenshots/2022/09/grep_string.png" alt="image.png" /></p>
+<ul>
+<li><code>-r</code> or <code>-R</code> is recursive,</li>
+<li><code>-n</code> is line number, and</li>
+<li><code>-w</code> stands for match the whole word.</li>
+<li><code>-l</code> (lower-case L) can be added to just give the file name of matching files.</li>
+<li><code>-e</code> is the pattern used during the search</li>
+</ul>
+<p>Along with these, <code>--exclude</code>, <code>--include</code>, <code>--exclude-dir</code> flags could be used for efficient searching:</p>
+<ul>
+<li>This will only search through those files which have .c or .h extensions:</li>
+</ul>
+<pre data-lang="bash" style="background-color:#2e3440;color:#d8dee9;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#88c0d0;">grep</span><span> --include</span><span style="color:#81a1c1;">=</span><span style="color:#ebcb8b;">\*</span><span>.{c</span><span style="color:#eceff4;">,</span><span>h} -rnw </span><span style="color:#a3be8c;">&#39;/path/to/somewhere/&#39;</span><span> -e </span><span style="color:#a3be8c;">&quot;pattern&quot;
+</span></code></pre>
+<ul>
+<li>This will exclude searching all the files ending with .o extension:</li>
+</ul>
+<pre data-lang="bash" style="background-color:#2e3440;color:#d8dee9;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#88c0d0;">grep</span><span> --exclude</span><span style="color:#81a1c1;">=</span><span style="color:#ebcb8b;">\*</span><span>.o -rnw </span><span style="color:#a3be8c;">&#39;/path/to/somewhere/&#39;</span><span> -e </span><span style="color:#a3be8c;">&quot;pattern&quot;
+</span></code></pre>
+<ul>
+<li>For directories it's possible to exclude one or more directories using the <code>--exclude-dir</code> parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:</li>
+</ul>
+<pre data-lang="bash" style="background-color:#2e3440;color:#d8dee9;" class="language-bash "><code class="language-bash" data-lang="bash"><span style="color:#88c0d0;">grep</span><span> --exclude-dir</span><span style="color:#81a1c1;">=</span><span>{dir1</span><span style="color:#eceff4;">,</span><span>dir2</span><span style="color:#eceff4;">,</span><span style="color:#81a1c1;">*</span><span>.dst} -rnw </span><span style="color:#a3be8c;">&#39;/path/to/somewhere/&#39;</span><span> -e </span><span style="color:#a3be8c;">&quot;pattern&quot;
+</span></code></pre>
+<p>more info could see <a href="https://ss64.com/bash/grep.html">man grep</a>.</p>
+</blockquote>
+
+</p>
+
+
+
+
+ </div>
+ </section>
+</body>
+
+<div class="footer">
+ &emsp;&copy; garhve
+</div>
+
+</html>