<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: taijidude</title>
    <description>The latest articles on Forem by taijidude (@taijidude).</description>
    <link>https://forem.com/taijidude</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F438241%2Fcdc2f2af-7e2d-4c09-85a2-313d328f0fad.jpg</url>
      <title>Forem: taijidude</title>
      <link>https://forem.com/taijidude</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/taijidude"/>
    <language>en</language>
    <item>
      <title>Small prototype... easy coding flash cards on android</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Thu, 07 Nov 2024 17:32:27 +0000</pubDate>
      <link>https://forem.com/taijidude/small-prototype-easy-coding-flash-cards-on-android-4paj</link>
      <guid>https://forem.com/taijidude/small-prototype-easy-coding-flash-cards-on-android-4paj</guid>
      <description>&lt;h1&gt;
  
  
  My Problem...
&lt;/h1&gt;

&lt;p&gt;I recently read the wonderful book &lt;a href="https://www.manning.com/books/the-programmers-brain" rel="noopener noreferrer"&gt;the-programmers-brain&lt;/a&gt;. One take-away was that if you study concepts with flash cards it will help learn new stuff more efficiently. So i was looking for a while now how to do that using my android phone. &lt;/p&gt;

&lt;p&gt;I'm a pretty heavy obsidian user and had a look at the plugins i knew of but was not really satisfied and looked and some apps. This didn't really worked easly or made me jump through more hoops than i wanted to. &lt;/p&gt;

&lt;p&gt;I'm no Android Developer yet and not really a web dev either. I want to use flash cards to learn angular and typescript, so my options were a litte limited. Here is what i came up with: &lt;/p&gt;

&lt;h1&gt;
  
  
  My Solution...
&lt;/h1&gt;

&lt;p&gt;There is a fantastic app called termux on android which gives you a complete linux environment. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://termux.dev/en/" rel="noopener noreferrer"&gt;termux&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In termux you are able to work with python and git. So i came up with a small python project containing of one python script and a markdown file at the moment. &lt;/p&gt;

&lt;p&gt;The python file uses the rich library because i can render markdown pretty to the cli (with syntax highlighting).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;rich.console&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;rich.markdown&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Markdown&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;console&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;angular.md&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;cards&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;!-- Card --&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;numberOfCards&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cards&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cards&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;sides&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;!-- Sides --&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Markdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; 
            &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;show the answer!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Markdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
            &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;next card!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The content of my flashcards is stored in a markdown file. I use html comments to seperate the individual cards and the question and the answer on each individual card. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ap3d04ljxypi3lqaajg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ap3d04ljxypi3lqaajg.PNG" alt="Image description" width="452" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have this all on a little github project. &lt;a href="https://github.com/taijidude/cli-flashcards" rel="noopener noreferrer"&gt;github&lt;/a&gt;. (The name of the project will change.) S &lt;/p&gt;

&lt;p&gt;On my phone i have to clone the repo in termux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/taijidude/mochi-cards.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the needed dependencies (typer is a neat cli framework which brings the rich library as dependency. I use &lt;a href="https://typer.tiangolo.com/" rel="noopener noreferrer"&gt;typer&lt;/a&gt; here because i want to do more with it in the future. )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;typer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and than can run the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally i get a basic flashcard functionality with syntax highlighting. &lt;br&gt;
After starting:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pc9zbavj234aa2xhbp4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pc9zbavj234aa2xhbp4.jpg" alt="Image description" width="800" height="1777"&gt;&lt;/a&gt;&lt;br&gt;
When hitting enter the answer is shown: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevnmd2d3wkp7oq8rtzjr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevnmd2d3wkp7oq8rtzjr.jpg" alt="Image description" width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When hitting enter again i get the next question: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyu8pa7uk8mi59y005is.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyu8pa7uk8mi59y005is.jpg" alt="Image description" width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for your Attention! &lt;/p&gt;

</description>
      <category>python</category>
      <category>termux</category>
      <category>cli</category>
      <category>learning</category>
    </item>
    <item>
      <title>A first try of typescript and deno 2</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Sat, 02 Nov 2024 17:28:19 +0000</pubDate>
      <link>https://forem.com/taijidude/a-first-try-of-typescript-and-deno-2-3c74</link>
      <guid>https://forem.com/taijidude/a-first-try-of-typescript-and-deno-2-3c74</guid>
      <description>&lt;p&gt;After joining a new company I get to work with angular and typescript. To get more familiar with typescript I'm trying to use it as much as possible. I recently started with a small cli app for my git workflow. (I'm fully aware that this can be done in bash... 😉).&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem...
&lt;/h2&gt;

&lt;p&gt;When working with git I tend to use the git bash on windows. I switch branches often and would like to quickly go back to the branch I have been previously working on.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
The first thing I want my solution to do is to show me the branch I previously worked on. This information should be updated, if I switch branches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;test1&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gst &lt;span class="c"&gt;#Shows the previous branch name along with the output of git status  &lt;/span&gt;
previous branch: main ❗
On branch test1
nothing to commit, working tree clean

my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;test1&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gch main &lt;span class="c"&gt;# checks out the branch with the name main&lt;/span&gt;
Switched to branch &lt;span class="s1"&gt;'main'&lt;/span&gt;

my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gst
previous branch: test1 ❗
On branch main
nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, I want to be able to switch back to the previous branch. Like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gst
previous branch: test1
On branch main
nothing to commit, working tree clean

my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gchl ❗
Switched to branch &lt;span class="s1"&gt;'test1'&lt;/span&gt;

my_computer MINGW64 /c/Dev/temp/blogarticle &lt;span class="o"&gt;(&lt;/span&gt;test1&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gst
previous branch: main
On branch test1
nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The solution...
&lt;/h2&gt;

&lt;p&gt;Deno intrigued me for a long time, so I picked it for this first try. Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can compile your code into native executables.&lt;/li&gt;
&lt;li&gt;It comes with a key-value store functionality.&lt;/li&gt;
&lt;li&gt;with version 2 you are now able to use npm libraries. I've used commander js to implement the command line functionality. Installing a npm library into your project looks like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno &lt;span class="nb"&gt;install &lt;/span&gt;npm:commander
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The code...
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;program&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;commander&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dbPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;Dev&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;projects&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;rememberGitBranch&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;db.sqlite3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;branchKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;previousBranch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Deno&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;openKv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dbPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;kv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;branchKey&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;branch&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;kv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Deno&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;openKv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dbPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastBranch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;kv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;branchKey&lt;/span&gt;&lt;span class="p"&gt;])).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
  &lt;span class="nx"&gt;kv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lastBranch&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;program&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;save&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;save the name of the branch you worked with last&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requiredOption&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-b, --branch &amp;lt;branch&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The branch to work with&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;program&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;load&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loads the last branch name saved with the save command&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets have a look at the output of the help function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my_computer MINGW64 /c/Dev/projects/rememberGitBranch
&lt;span class="nv"&gt;$ &lt;/span&gt;./rememberGitBranch.exe &lt;span class="nt"&gt;--help&lt;/span&gt;
Usage: main &lt;span class="o"&gt;[&lt;/span&gt;options] &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

Options:
  &lt;span class="nt"&gt;-h&lt;/span&gt;, &lt;span class="nt"&gt;--help&lt;/span&gt;    display &lt;span class="nb"&gt;help &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;command

&lt;/span&gt;Commands:
  save &lt;span class="o"&gt;[&lt;/span&gt;options]  save the name of the branch you worked with last   
  load          loads the last branch name saved with the save &lt;span class="nb"&gt;command
  help&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;  display &lt;span class="nb"&gt;help &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the app can be run with three commands, with one beeing the help function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;save:&lt;/strong&gt; the save command takes a string parameter and runs the save function in the main.ts file. This function calls key-value Store stores it with a key named lastBranch. When opening the connection to the kv store i give it the path of the database file. Normally i would not have to do this, but because the deno runtime has a default storage file. But when you compile your code into an executable it doesn't know about the default file. So you have to explicitly give it a path. &lt;br&gt;
If the file doesn't exists it will get created. &lt;/p&gt;

&lt;p&gt;The output for the help call about the save command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./rememberGitBranch.exe save &lt;span class="nt"&gt;--help&lt;/span&gt;
Usage: main save &lt;span class="o"&gt;[&lt;/span&gt;options]

save the name of the branch you worked with last

Options:
  &lt;span class="nt"&gt;-b&lt;/span&gt;, &lt;span class="nt"&gt;--branch&lt;/span&gt; &amp;lt;branch&amp;gt;  The branch to work with
  &lt;span class="nt"&gt;-h&lt;/span&gt;, &lt;span class="nt"&gt;--help&lt;/span&gt;            display &lt;span class="nb"&gt;help &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;load:&lt;/strong&gt; this commands executes the load function in the main.ts file. This function opens a connection to the kv store and queries it with the key "previousBranch". The function return the result of the query as string. This string gets printed. &lt;/p&gt;

&lt;p&gt;The output for the help call about the load command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./rememberGitBranch.exe load &lt;span class="nt"&gt;--help&lt;/span&gt;
Usage: main load &lt;span class="o"&gt;[&lt;/span&gt;options]

loads the last branch name saved with the save &lt;span class="nb"&gt;command

&lt;/span&gt;Options:
  &lt;span class="nt"&gt;-h&lt;/span&gt;, &lt;span class="nt"&gt;--help&lt;/span&gt;  display &lt;span class="nb"&gt;help &lt;/span&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;command&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Compiling the executable
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno compile &lt;span class="nt"&gt;--allow-write&lt;/span&gt; &lt;span class="nt"&gt;--allow-read&lt;/span&gt; &lt;span class="nt"&gt;--unstable-kv&lt;/span&gt; main.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is a shiny new exe file in my project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my_computer MINGW64 /c/Dev/projects_patrick/rememberGitBranch
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;db.sqlite3  deno.json  deno.lock  main.ts  main_test.ts  rememberGitBranch.exe&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Putting everything together...
&lt;/h1&gt;

&lt;p&gt;Here you see some parts of my .bashrc file which uses the new cli app. This allows me to do the things i have shown in the problem section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;gst&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'echo "previous branch:" $(rememberGitBranch.exe load) &amp;amp;&amp;amp; git status'&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;gch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'rememberGitBranch.exe save -b "$(git branch --show-current)"&amp;amp;&amp;amp;git checkout'&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;gchl &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;currentBranch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;git branch &lt;span class="nt"&gt;--show-current&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    git checkout &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;rememberGitBranch.exe load&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    rememberGitBranch.exe save &lt;span class="nt"&gt;-b&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$currentBranch&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Take Aways
&lt;/h2&gt;

&lt;p&gt;I had fun trying this out and after a short amount of time had something in my hand that i will use in my day to day workflow. I think it's a nice way to learn about the language and most of the frameworks on a frequent basis. I have some more ideas for some cli apps and for some webapps but i need to get a little further in the udemy angular course i'm doing to work on those.  &lt;/p&gt;

&lt;p&gt;Thank you for reading! Would you have done anything different? Is there something to make the code more idiomatic?&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>deno</category>
      <category>cli</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Check if date is between two other dates</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Wed, 18 Sep 2024 14:24:10 +0000</pubDate>
      <link>https://forem.com/taijidude/check-if-date-is-between-two-other-dates-4nk3</link>
      <guid>https://forem.com/taijidude/check-if-date-is-between-two-other-dates-4nk3</guid>
      <description>&lt;p&gt;A small method to check if a LocalDataTime is in a Range between to other LocalDateTime - Objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isInDateRange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                        &lt;span class="nc"&gt;LocalDateTime&lt;/span&gt; &lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                        &lt;span class="nc"&gt;LocalDateTime&lt;/span&gt; &lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                        &lt;span class="nc"&gt;LocalDateTime&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isUpper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isLower&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLower&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;isUpper&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isBefore&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isAfter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>localdatetime</category>
    </item>
    <item>
      <title>Convert LocalDateTime to Date and back</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Wed, 18 Sep 2024 13:43:58 +0000</pubDate>
      <link>https://forem.com/taijidude/convert-localdatetime-to-date-4o4c</link>
      <guid>https://forem.com/taijidude/convert-localdatetime-to-date-4o4c</guid>
      <description>&lt;p&gt;At the moment i have to write a lot of Integeration Tests for a migration project. The old code uses java.util.Date a lot, but the project runs in Java 17. So i wanted to use the newer Date Classes for my tests. &lt;/p&gt;

&lt;p&gt;I used the following two helper methods to convert Date to LocalDateTime and back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;LocalDateTime&lt;/span&gt; &lt;span class="nf"&gt;toLocalDateTime&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;toConvert&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;instant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toConvert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toInstant&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;zonedDateTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;atZone&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ZoneId&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;systemDefault&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;zonedDateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toLocalDateTime&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="nf"&gt;toDate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LocalDateTime&lt;/span&gt; &lt;span class="n"&gt;toConvert&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;zonedDateTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toConvert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;atZone&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ZoneId&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;systemDefault&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zonedDateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toInstant&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>localdatetime</category>
    </item>
    <item>
      <title>Pod not starting in openshift</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Mon, 18 Sep 2023 13:16:40 +0000</pubDate>
      <link>https://forem.com/taijidude/pod-not-starting-in-openshift-14n3</link>
      <guid>https://forem.com/taijidude/pod-not-starting-in-openshift-14n3</guid>
      <description>&lt;p&gt;Today i had a curious case of a pod in openshift  with problems to start. The pod contained a simple enough springboot rest web service. The logs looked all fine, But nevertheless openshift would restart the service 4 times and give up with a fairly meaningless error message.&lt;/p&gt;

&lt;p&gt;Turns the dependency for the actuator was missing, so openshift could not reach the two endpoints /actuator/health and /actuator/info. These endpoints were configured in the openshift deployment config for the liveness probe and the readiness probe. So openshift could not know the spring app was started successfully and gave up after 4 tries.&lt;/p&gt;

&lt;p&gt;I will add two tests for those endpoints. &lt;/p&gt;

</description>
      <category>java</category>
      <category>openshift</category>
      <category>springboot</category>
    </item>
    <item>
      <title>pgsql Cheatsheet</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Fri, 16 Jun 2023 17:59:55 +0000</pubDate>
      <link>https://forem.com/taijidude/pgsql-cheatsheet-and-notes-4i4e</link>
      <guid>https://forem.com/taijidude/pgsql-cheatsheet-and-notes-4i4e</guid>
      <description>&lt;p&gt;One of my recent tasks was the migration of a small spring boot web service to openshift. The webservice works with a postgres which runs in an openshift pod. I had to migrate the database as well. &lt;/p&gt;

&lt;p&gt;The psql CLI Tool came in very handy with working with the database. I could get stuff done quickly and work directly on the openshift pods. &lt;/p&gt;

&lt;p&gt;Here are my Notes on the psql Tool: &lt;/p&gt;

&lt;h3&gt;
  
  
  Get help
&lt;/h3&gt;

&lt;p&gt;From the shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psql &lt;span class="nt"&gt;--help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While running the tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# \?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Connect to a database
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psql &amp;lt;database name&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--User&lt;/span&gt; &amp;lt;username&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psql TESTDB &lt;span class="nt"&gt;--User&lt;/span&gt; testuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Quit psql
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=#\q 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  list all tables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=#\dt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  list all sequences
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=#\ds 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  list all users on the data base
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=#\du
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Execute SQL Queries and Scripts
&lt;/h3&gt;

&lt;p&gt;From the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql &amp;lt;database name&amp;gt; [--User &amp;lt;username&amp;gt; -c &amp;lt;command&amp;gt; 
psql &amp;lt;database name&amp;gt; [--User &amp;lt;Username&amp;gt;] -f &amp;lt;Filename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the running psql tool you can just write your sql statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=#select * from TESTTABLE; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Some more command line tools.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Drop a database
&lt;/h3&gt;

&lt;p&gt;Besides the SQL Drop Database Statement postgres offers another cli utility: dropdb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dropdb &amp;lt;connection parameters&amp;gt; &amp;lt;options&amp;gt; &amp;lt;database name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Data Dump
&lt;/h3&gt;

&lt;p&gt;To backup data the pg_dump tool might come in handy. The pattern for it is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; pg_dump &lt;span class="nt"&gt;--table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;tablename&amp;gt; &lt;span class="nt"&gt;--data-only&lt;/span&gt; &lt;span class="nt"&gt;--column-inserts&lt;/span&gt; &amp;lt;databasename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>security</category>
      <category>crypto</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>TIL: isBlank() String Method</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Tue, 25 Apr 2023 16:09:44 +0000</pubDate>
      <link>https://forem.com/taijidude/til-isblank-string-method-55d8</link>
      <guid>https://forem.com/taijidude/til-isblank-string-method-55d8</guid>
      <description>&lt;p&gt;Today i learned that in Java 11 there was a new String Method introduced that checks if a string is empty or just contains blanks and the same time. &lt;/p&gt;

&lt;p&gt;So instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;toCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isBlank&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Makes the if statement a bit cleaner. &lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>todayilearned</category>
      <category>string</category>
    </item>
    <item>
      <title>Get help for a maven plugin</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Thu, 09 Mar 2023 14:04:10 +0000</pubDate>
      <link>https://forem.com/taijidude/get-help-for-a-maven-plugin-3738</link>
      <guid>https://forem.com/taijidude/get-help-for-a-maven-plugin-3738</guid>
      <description>&lt;p&gt;Today i was fighting to get a vaadin app building again with maven. During my search for documentation of the vaadin maven plugin i came across this neat little trick. You can get maven to print out help goal descriptions for you with the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn &lt;span class="nb"&gt;help&lt;/span&gt;:describe &lt;span class="nt"&gt;-Dplugin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;vaadin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;...
Name: Vaadin Maven plugin
Description: Vaadin Platform
Group Id: com.vaadin
Artifact Id: vaadin-maven-plugin
Version: 14.8.0
Goal Prefix: vaadin

This plugin has 8 goals:

vaadin:build-frontend
  Description: Goal that builds the frontend bundle. It performs the
    following actions when creating a package:
    - Update Constants.PACKAGE_JSON file with the NpmPackage annotations
      defined &lt;span class="k"&gt;in &lt;/span&gt;the classpath,
    - Copy resource files used by flow from &lt;span class="sb"&gt;`&lt;/span&gt;.jar&lt;span class="sb"&gt;`&lt;/span&gt; files to the &lt;span class="sb"&gt;`&lt;/span&gt;node_modules&lt;span class="sb"&gt;`&lt;/span&gt;
      folder
    - Install dependencies by running npm &lt;span class="nb"&gt;install&lt;/span&gt;
    - Update the FrontendUtils.IMPORTS_NAME file imports with the JsModule
      Theme and JavaScript annotations defined &lt;span class="k"&gt;in &lt;/span&gt;the classpath,
    - Update FrontendUtils.WEBPACK_CONFIG file.

vaadin:clean-frontend
  Description: Goal that cleans the frontend files to a clean state.
    Deletes Vaadin dependencies from package.json, the generated frontend
    folder and the npm/pnpm-related files and folders:

    - node_modules
    - pnpm-lock.yaml
    - package-lock.json

vaadin:copy-production-files
  Description: Goal that copies all production mode files into the
    CopyProductionFilesMojo.copyOutputDirectory directory. Files are copied
    from CopyProductionFilesMojo.frontendWorkingDirectory directory, WebJars
    and regular jars, refer to ProductionModeCopyStep &lt;span class="k"&gt;for &lt;/span&gt;details.

vaadin:dance
  Description: This is the hidden &lt;span class="sb"&gt;`&lt;/span&gt;vaadin:dance&lt;span class="sb"&gt;`&lt;/span&gt; to clean up the frontend
    files.

vaadin:migrate-to-p3
  Description: This goal migrates project from compatibility mode to NPM
    mode.

vaadin:package-for-production
  Description: Goal that prepares all web files from
    PackageForProductionMojo.transpileEs6SourceDirectory &lt;span class="k"&gt;for &lt;/span&gt;production mode:
    minifies, transpiles and bundles them.

vaadin:prepare-frontend
  Description: This goal checks that node and npm tools are installed, copies
    frontend resources available inside &lt;span class="sb"&gt;`&lt;/span&gt;.jar&lt;span class="sb"&gt;`&lt;/span&gt; dependencies to &lt;span class="sb"&gt;`&lt;/span&gt;node_modules&lt;span class="sb"&gt;`&lt;/span&gt;,
    and creates or updates &lt;span class="sb"&gt;`&lt;/span&gt;package.json&lt;span class="sb"&gt;`&lt;/span&gt; and &lt;span class="sb"&gt;`&lt;/span&gt;webpack.config.json&lt;span class="sb"&gt;`&lt;/span&gt; files.

vaadin:validate
  Description: This goal checks that node and npm tools are installed, copies
    frontend resources available inside &lt;span class="sb"&gt;`&lt;/span&gt;.jar&lt;span class="sb"&gt;`&lt;/span&gt; dependencies to &lt;span class="sb"&gt;`&lt;/span&gt;node_modules&lt;span class="sb"&gt;`&lt;/span&gt;,
    and creates or updates &lt;span class="sb"&gt;`&lt;/span&gt;package.json&lt;span class="sb"&gt;`&lt;/span&gt; and &lt;span class="sb"&gt;`&lt;/span&gt;webpack.config.json&lt;span class="sb"&gt;`&lt;/span&gt; files.
  Deprecated. use &lt;span class="o"&gt;{&lt;/span&gt;@link PrepareFrontendMojo&lt;span class="o"&gt;}&lt;/span&gt; instead

For more information, run &lt;span class="s1"&gt;'mvn help:describe [...] -Ddetail'&lt;/span&gt;

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another example, you can use this with the help plugin itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn &lt;span class="nb"&gt;help&lt;/span&gt;:describe &lt;span class="nt"&gt;-Dplugin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Name: Apache Maven Help Plugin
Description: The Maven Help plugin provides goals aimed at helping to make
  sense out of the build environment. It includes the ability to view the
  effective POM and settings files, after inheritance and active profiles have
  been applied, as well as a describe a particular plugin goal to give usage
  information.
Group Id: org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version: 3.3.0
Goal Prefix: &lt;span class="nb"&gt;help

&lt;/span&gt;This plugin has 8 goals:

&lt;span class="nb"&gt;help&lt;/span&gt;:active-profiles
  Description: Displays a list of the profiles which are currently active &lt;span class="k"&gt;for
    &lt;/span&gt;this build.

&lt;span class="nb"&gt;help&lt;/span&gt;:all-profiles
  Description: Displays a list of available profiles under the current
    project.
    Note: it will list all profiles &lt;span class="k"&gt;for &lt;/span&gt;a project. If a profile comes up with a
    status inactive &lt;span class="k"&gt;then &lt;/span&gt;there might be a need to &lt;span class="nb"&gt;set &lt;/span&gt;profile activation
    switches/property.

&lt;span class="nb"&gt;help&lt;/span&gt;:describe
  Description: Displays a list of the attributes &lt;span class="k"&gt;for &lt;/span&gt;a Maven Plugin and/or
    goals &lt;span class="o"&gt;(&lt;/span&gt;aka Mojo - Maven plain Old Java Object&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="nb"&gt;help&lt;/span&gt;:effective-pom
  Description: Displays the effective POM as an XML &lt;span class="k"&gt;for &lt;/span&gt;this build, with the
    active profiles factored &lt;span class="k"&gt;in&lt;/span&gt;, or a specified artifact. If verbose, a comment
    is added to each XML element describing the origin of the line.

&lt;span class="nb"&gt;help&lt;/span&gt;:effective-settings
  Description: Displays the calculated settings as XML &lt;span class="k"&gt;for &lt;/span&gt;this project,
    given any profile enhancement and the inheritance of the global settings
    into the user-level settings.

&lt;span class="nb"&gt;help&lt;/span&gt;:evaluate
  Description: Evaluates Maven expressions given by the user &lt;span class="k"&gt;in &lt;/span&gt;an
    interactive mode.

&lt;span class="nb"&gt;help&lt;/span&gt;:help
  Description: Display &lt;span class="nb"&gt;help &lt;/span&gt;information on maven-help-plugin.
    Call mvn &lt;span class="nb"&gt;help&lt;/span&gt;:help &lt;span class="nt"&gt;-Ddetail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="nt"&gt;-Dgoal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;goal-name&amp;gt; to display parameter
    details.

&lt;span class="nb"&gt;help&lt;/span&gt;:system
  Description: Displays a list of the platform details like system properties
    and environment variables.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even more details if you add -Ddetail to your command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn &lt;span class="nb"&gt;help&lt;/span&gt;:describe &lt;span class="nt"&gt;-Dplugin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;help&lt;/span&gt; &lt;span class="nt"&gt;-Ddetail&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;...
&lt;span class="nb"&gt;help&lt;/span&gt;:system
  Description: Displays a list of the platform details like system properties
    and environment variables.
  Implementation: org.apache.maven.plugins.help.SystemMojo
  Language: java

  Available parameters:

    output
      User property: output
      Optional parameter to write the output of this &lt;span class="nb"&gt;help &lt;/span&gt;&lt;span class="k"&gt;in &lt;/span&gt;a given file,
      instead of writing to the console.
      Note: Could be a relative path.
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>maven</category>
      <category>vaadin</category>
      <category>awesome</category>
    </item>
    <item>
      <title>Path conversion in git bash</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Fri, 17 Feb 2023 13:49:47 +0000</pubDate>
      <link>https://forem.com/taijidude/paths-conversion-in-git-bash-3jeh</link>
      <guid>https://forem.com/taijidude/paths-conversion-in-git-bash-3jeh</guid>
      <description>&lt;p&gt;Hello again, &lt;/p&gt;

&lt;p&gt;today i have a small advice for you if you work with the git bash on windows. &lt;/p&gt;

&lt;p&gt;To quickly convert paths from windows format to unix format you can use the &lt;strong&gt;cygpath&lt;/strong&gt; tool: &lt;/p&gt;

&lt;p&gt;Unix to windows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;cygpath &lt;span class="nt"&gt;-w&lt;/span&gt; /d/temp
D:&lt;span class="se"&gt;\t&lt;/span&gt;emp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Windows to unix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;cygpath &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="s1"&gt;'D:\temp'&lt;/span&gt;
/d/temp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another nice tool that helps you with the conversion of an relative path is &lt;strong&gt;realpath&lt;/strong&gt;. This is useful because it allows your scripts to accept relative paths and than figure out the absolute paths themselve. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls 
&lt;/span&gt;test.txt
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;realpath&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; ./test.txt
/d/realpathtest/test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you give an absolute path to realpath it will just print this path. So if you are working with file paths, this will make your scripts more flexible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;realpath&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /d/realpathtest/test.txt
/d/realpathtest/test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

</description>
      <category>security</category>
      <category>cryptocurrency</category>
      <category>web3</category>
    </item>
    <item>
      <title>check the return code in an if clause</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Thu, 27 Oct 2022 10:32:55 +0000</pubDate>
      <link>https://forem.com/taijidude/check-the-return-code-in-an-if-5d73</link>
      <guid>https://forem.com/taijidude/check-the-return-code-in-an-if-5d73</guid>
      <description>&lt;p&gt;I'm working on a bash script that uses git to clone all of our git repositories (only the necessary stuff). We have a mixture of master (old projects, and yes we have a task to migrate this) and main as the repo main branches.&lt;/p&gt;

&lt;p&gt;I want the script to first try to clone the branch named master and if this fails try to clone the branch named main.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://git-scm.com/docs/git-clone"&gt;git docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My first try&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 &lt;span class="nt"&gt;--branch&lt;/span&gt; main &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
   &lt;/span&gt;git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 &lt;span class="nt"&gt;--branch&lt;/span&gt; master &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
 &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first statement tries to clone the master branch of the repository with a url contained within the var repoUrl. And it clones it into a directory contained in the var targetFolder.&lt;br&gt;
Only the latest commit is cloned because of the --depth 1.  &lt;/p&gt;

&lt;p&gt;So far so good. But i have started using &lt;a href="https://github.com/koalaman/shellcheck"&gt;shellcheck&lt;/a&gt; a while ago (blog post coming) and it alerted me to slightly better solution.&lt;/p&gt;

&lt;p&gt;Turns out it's possible to write it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 &lt;span class="nt"&gt;--branch&lt;/span&gt; main &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 &lt;span class="nt"&gt;--branch&lt;/span&gt; master &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are able to run the command whose return code you want to check directly in the if clause. I'm doing this here and checking for a return code that says the execution was not successful.&lt;/p&gt;

&lt;p&gt;Still some repetition. We could pull this into a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gitclone&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 &lt;span class="nt"&gt;--branch&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; gitclone &lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;gitclone &lt;span class="s2"&gt;"master"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$repoUrl&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$targetFolder&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>bash</category>
      <category>shell</category>
      <category>cli</category>
      <category>git</category>
    </item>
    <item>
      <title>TIL: Slightly over engineered (😅) text replace function (sed multiple replacements)</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Sun, 21 Aug 2022 11:58:00 +0000</pubDate>
      <link>https://forem.com/taijidude/til-slightly-overenigineered-text-replace-function-50kh</link>
      <guid>https://forem.com/taijidude/til-slightly-overenigineered-text-replace-function-50kh</guid>
      <description>&lt;p&gt;I recently had to read a lot of long bash scripts. Stumbled about a few things I found interesting and played a little with them. I will explain them later. But first behold the mighty text replace function i created will played around.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;function &lt;/span&gt;repl &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"$#"&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[ERROR]The parameter list needs at least 3 items. The file to work with, the string to replace and the new string! "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return
    fi
    &lt;/span&gt;&lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
            &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[ERROR]The &lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt; doesn't exists!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return
    fi
    &lt;/span&gt;&lt;span class="nb"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"$#"&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[ERROR]No parameters except the file found!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return
    fi
    if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;expr&lt;/span&gt; &lt;span class="s2"&gt;"$#"&lt;/span&gt; % 2 &lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 1 &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[ERROR]Invalid Amount of parameters after the filename!"&lt;/span&gt;
        &lt;span class="k"&gt;return
    fi
    &lt;/span&gt;&lt;span class="nv"&gt;old&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="nv"&gt;new&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="nv"&gt;replacements&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;item &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;do
        if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$old&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;then
            &lt;/span&gt;&lt;span class="nv"&gt;old&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$old&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;
        &lt;span class="k"&gt;then
            &lt;/span&gt;&lt;span class="nv"&gt;new&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
        &lt;span class="k"&gt;fi
        if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$old&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;
        &lt;span class="k"&gt;then
            &lt;/span&gt;&lt;span class="nv"&gt;replacements&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"s/&lt;/span&gt;&lt;span class="nv"&gt;$old&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="s2"&gt;/g; "&lt;/span&gt;
            &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$replacements&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nv"&gt;$file&lt;/span&gt;
            &lt;span class="nb"&gt;unset &lt;/span&gt;old&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nb"&gt;unset &lt;/span&gt;new&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;fi
    done&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a convenient wrapper function around a sed call to edit a file. For an example let's assume we have the following data file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The Republic is in control of this planet now.
A Jedi will arrive shortly to solve the most important problems.
The local ewok tribes will be rescued.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file needs a few corrections. In the bash shell normally sed can be used to edit this file in place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; s/Republic/Empire/g StarWars.txt
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; s/Jedi/Inquisitor/g StarWars.txt
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; s/rescued/murdered/g StarWars.txt
&lt;span class="nb"&gt;cat &lt;/span&gt;StarWars.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The Empire is in control of this planet now.
A Inquisitor will arrive shortly to solve the most important problems.
The local ewok tribes will be murdered.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ah, that's better. 😉&lt;/p&gt;

&lt;p&gt;You can even write this in one line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; s/Republic/Empire/g&lt;span class="p"&gt;;&lt;/span&gt; s/Jedi/Inquisitor/g&lt;span class="p"&gt;;&lt;/span&gt; s/rescued/murdered/g&lt;span class="p"&gt;;&lt;/span&gt; StarWars.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this discovering this, i relealized i might not need my function at all. But as an excersis it was nice and i learned  a few things.&lt;/p&gt;

&lt;p&gt;So how does a call of my function look?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;repl StarWars.txt Republic Empire Jedi Inquisitor rescued murdered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a little bit less verbose. And provides a little bit of additional error handling. But I need to check out sed's error handling. Maybe I can throw out my custom error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One big takeaway from this (again): What are you trying to do has likely been done before and there likely is a solution for this&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But anyway, &lt;strong&gt;What else did I learn?&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;local:&lt;/strong&gt; If you put the local keyword in front of a variable. The variable is only visible in the block of code where it is declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;function &lt;/span&gt;ltest &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;local test&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Hello from the ltest function!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$test&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
ltest
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"--&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$test&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./test456.sh
Hello from the ltest &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;unset:&lt;/strong&gt; With this keyword you are able to clear previously set variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Hello!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"--&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$test&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;unset test
echo&lt;/span&gt; &lt;span class="s2"&gt;"--&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$test&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./test456.sh
&lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;Hello!
&lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;test command:&lt;/strong&gt; To test specific conditions for if statement bash has the test command. With this tool you are able to check file attributes and to string and number comparisons. It doesn't has output. But if it exits with a with 0 that means the supplied expression is true. A 1 stands for false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;test &lt;/span&gt;1 &lt;span class="nt"&gt;-lt&lt;/span&gt; 2 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="c"&gt;# There is an shortcut to write this&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; 1 &lt;span class="nt"&gt;-lt&lt;/span&gt; 2 &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="c"&gt;# You will see a third way of writing this.&lt;/span&gt;
&lt;span class="o"&gt;[[&lt;/span&gt; 1 &lt;span class="nt"&gt;-lt&lt;/span&gt; 2]] &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="c"&gt;# This will run another test command that bash has adopted from the kornshell.  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The kornshell test command seems to offer more functionality and the general recommendation seems to be to use it instead of the bash one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shift:&lt;/strong&gt; This shifts every parameter in you function to the left and puts every parameter in a lower position. The first parameter gets dropped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./test456.sh 1 2 3
1 2 3
2 3
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;exit/return:&lt;/strong&gt; To abort a script you can use the exit keyword. But this will close the shell window as well. If you don't want the window to close, just use the return keyword.&lt;/p&gt;

</description>
      <category>shell</category>
      <category>bash</category>
      <category>sed</category>
      <category>cli</category>
    </item>
    <item>
      <title>Background Jobs are not run at $PSScriptRoot</title>
      <dc:creator>taijidude</dc:creator>
      <pubDate>Tue, 21 Jun 2022 05:20:40 +0000</pubDate>
      <link>https://forem.com/taijidude/background-jobs-are-not-run-at-psscriptroot-310k</link>
      <guid>https://forem.com/taijidude/background-jobs-are-not-run-at-psscriptroot-310k</guid>
      <description>&lt;p&gt;Stumbled over this one a while ago. While i was developing a script which started several background jobs, i was making the assumption that the jobs would be run in the same directory the "mother script" was in. But turns out this is not true, as we can see in the following example. &lt;/p&gt;

&lt;p&gt;test.ps1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Start-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ScriptBlock&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="n"&gt;Get-Location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Wait-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;120&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Receive-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nTwq8pIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zupv62ga77jr9gpjkqqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nTwq8pIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zupv62ga77jr9gpjkqqi.png" alt="Image description" width="880" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you want to work with your script location, you can just pass it to your scriptblock via the argumentlist. &lt;/p&gt;

&lt;p&gt;test2.ps1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Start-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ScriptBlock&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Set-Location&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Get-Location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ArgumentList&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$PSScriptRoot&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Wait-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;120&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Receive-Job&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test2&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the final output: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--95PVEVrB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo98l0u7v7zk06h6e9tb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--95PVEVrB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo98l0u7v7zk06h6e9tb.png" alt="Image description" width="880" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>cli</category>
      <category>shell</category>
      <category>comandline</category>
    </item>
  </channel>
</rss>
