<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Colas.Nahaboo.net - linux</title>
    <subtitle>Colas Nahaboo personal site, with discussions about programming code, web and computing topics, surfing and SUPing, and various musings.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://colas.nahaboo.net/tags/linux/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://colas.nahaboo.net"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2022-03-03T00:00:00+00:00</updated>
    <id>https://colas.nahaboo.net/tags/linux/atom.xml</id>
    <entry xml:lang="en">
        <title>Suntimes, a small shell utility</title>
        <published>2022-03-03T00:00:00+00:00</published>
        <updated>2022-03-03T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://colas.nahaboo.net/code/suntimes-a-small-shell-utility/"/>
        <id>https://colas.nahaboo.net/code/suntimes-a-small-shell-utility/</id>
        
        <content type="html" xml:base="https://colas.nahaboo.net/code/suntimes-a-small-shell-utility/">&lt;p&gt;I just made a small command-line utility to display the sunrise and sunset times at a location. It can be quite useful in shell scripts.&lt;&#x2F;p&gt;
&lt;p&gt;It is a minimal Go wrapper around the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nathan-osman&#x2F;go-sunrise&quot;&gt;nathan-osman&#x2F;go-sunrise library&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can find it at my repository: &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ColasNahaboo&#x2F;suntimes&quot;&gt;GitHub - ColasNahaboo&#x2F;suntimes&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Bash lessons learned with AoC 2021</title>
        <published>2022-01-11T00:00:00+00:00</published>
        <updated>2022-01-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://colas.nahaboo.net/code/bash-lessons-learned-with-aoc-2021/"/>
        <id>https://colas.nahaboo.net/code/bash-lessons-learned-with-aoc-2021/</id>
        
        <content type="html" xml:base="https://colas.nahaboo.net/code/bash-lessons-learned-with-aoc-2021/">&lt;p&gt;I completed all the exercises of the Advent of Code 2021 in bash! (see &lt;a href=&quot;&#x2F;code&#x2F;advent-of-code-2021&quot;&gt;my previous post&lt;&#x2F;a&gt;)&lt;&#x2F;p&gt;
&lt;p&gt;You can find my solutions with comments in my GitHub repository at https:&#x2F;&#x2F;github.com&#x2F;ColasNahaboo&#x2F;advent-of-code-my-solutions&#x2F;tree&#x2F;main&#x2F;bash&#x2F;2021&lt;&#x2F;p&gt;
&lt;p&gt;I must say I &quot;cheated&quot; a bit. My  solution for the Day 24 was too slow in bash, and as I was a bit out of  steam, I did not try to find a smart algorithm. I just noticed that I  solved it by building a bash arithmetic expression (of 1 million  characters...) that I then evaluated in bash, and since its syntax was  exactly the one for C... I just made the bash script compile the bash  expression in C and execute it, using the C compiler as a bash  arithmetic just-in-time compiler :-)&lt;&#x2F;p&gt;
&lt;p&gt;I just discovered AoC this year, and I am impressed. Challenging, fun,  and a great way to progress. I am going to do the previous years, too,  but in &quot;real&quot; languages this time. At least ones with data structures... I will start with Go.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-i-learned&quot;&gt;What I learned:&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-i-learned&quot; aria-label=&quot;Anchor link for: what-i-learned&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Coding in bash is not so bad, even if it can border on insanity at times :-).&lt;&#x2F;li&gt;
&lt;li&gt;Modern bash features are often overlooked but very useful.&lt;&#x2F;li&gt;
&lt;li&gt;Passing shellcheck should be a mandatory goal for each bash programmer. I resented it at first since I thought it was adding unecessary syntaxic sugar to my code until I realized that it was a symptom that my coding style was the problem.&lt;&#x2F;li&gt;
&lt;li&gt;Use &lt;code&gt;[[...]]&lt;&#x2F;code&gt; for strings and &lt;code&gt;((...))&lt;&#x2F;code&gt; for integers other any of the legacy constructs like &lt;code&gt;[...]&lt;&#x2F;code&gt;, &lt;code&gt;test&lt;&#x2F;code&gt;, etc... The code is then much cleaner and safer (and a tad faster), as you do not have to quote as much. E.g: &lt;code&gt;[[ -z foo ]]&lt;&#x2F;code&gt; instead of &lt;code&gt;[ -z &quot;$foo&quot; ]&lt;&#x2F;code&gt;, or even &lt;code&gt;((i=j))&lt;&#x2F;code&gt; instead of &lt;code&gt;i=&quot;$j&quot;&lt;&#x2F;code&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;but&lt;&#x2F;strong&gt;, this makes traditional debugging with &lt;code&gt;set -x&lt;&#x2F;code&gt; less useful as the values of variables are not displayed anymore. E.g if j is 2, the tracing of &lt;code&gt;i=&quot;$j&quot;&lt;&#x2F;code&gt; shows &lt;code&gt;i=2&lt;&#x2F;code&gt; whereas the tracing of &lt;code&gt;((i=j))&lt;&#x2F;code&gt; only shows &lt;code&gt;((i=j))&lt;&#x2F;code&gt;. This could be where a bash debugger would be useful, but I know only one, &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;bashdb.sourceforge.net&#x2F;bashdb.html&quot;&gt;bashdb&lt;&#x2F;a&gt;, and it does not seem updated anymore, and I could not find a version working with bash 5.1. The &lt;code&gt;trap DEBUG&lt;&#x2F;code&gt; trick can be useful, though.&lt;&#x2F;li&gt;
&lt;li&gt;So, I tend to write now &lt;code&gt;i=$((j+k))&lt;&#x2F;code&gt; while developing code, and maybe later for production switch to the a bit more efficient &lt;code&gt;((i = j+k))&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;I avoided arrays in bash because I found out they were abysmally slow when first introduced, to the point that managing data in files with grep, sed, ... was actually faster than using arrays. But not anymore! Bash arrays should now be used as much as possible.&lt;&#x2F;li&gt;
&lt;li&gt;A lot of bash functions can &quot;map&quot; on arrays. For instance &lt;code&gt;${tab[@]&#x2F;&#x2F;x&#x2F;y}&lt;&#x2F;code&gt; will string-replace x by y in all the elements of the array tab, and is super fast.&lt;&#x2F;li&gt;
&lt;li&gt;Use arrays rather than the classic way to represent lists in bash by space-separated (or tab-separated) substrings in a  string.&lt;&#x2F;li&gt;
&lt;li&gt;Bash can have typed variables: integer ones via &lt;code&gt;declare -i&lt;&#x2F;code&gt; or &lt;code&gt;local -i&lt;&#x2F;code&gt;, and using them makes your code safer.&lt;&#x2F;li&gt;
&lt;li&gt;Bash functions can be passed variables by name, useful for efficiency to avoid copying big arrays or strings, and to provide multiple return values by modifying passed variables. But it cannot recurse as it is not a passing by reference, but by name.&lt;&#x2F;li&gt;
&lt;li&gt;Working with arrays makes using &lt;code&gt;$(...)&lt;&#x2F;code&gt; impractical, as commands are executed in a subshell and cannot access arrays anymore to update them in the parent shell. So I tend to pass the return value(s) into global variables of the same name of a function. E.g. instead of &lt;code&gt;x=$(foo)&lt;&#x2F;code&gt;, I write &lt;code&gt;foo; x=&quot;$foo&quot;&lt;&#x2F;code&gt;. Or pass variables by name to set them if I want to return multiple results.&lt;&#x2F;li&gt;
&lt;li&gt;To parse a space-separated string, the fastest is using &lt;code&gt;set&lt;&#x2F;code&gt; to map the elements in the positional parameters &lt;code&gt;$1&lt;&#x2F;code&gt;, &lt;code&gt;$2&lt;&#x2F;code&gt;, ... then the &lt;code&gt;${string#* }&lt;&#x2F;code&gt; and &lt;code&gt;${string% *}&lt;&#x2F;code&gt; operators are the fastest, closely followed by a read, the full &lt;code&gt;[[ $string =~ ([-[:digit:]]+)[[:space:]]... ]]&lt;&#x2F;code&gt; being 3 times slower. And if possible, using indexes is even faster: &lt;code&gt;${string:i:j}&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Use the faster &lt;code&gt;$(&amp;lt; filename)&lt;&#x2F;code&gt; instead of &lt;code&gt;$(cat filename)&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;To copy an associative array A1 to A2 in bash 4.4+, do:
&lt;code&gt;A1_def=$(declare -p A1) &amp;amp;&amp;amp; declare -A A2=&quot;${A1_def#*=}&quot;&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;To access more than 9 parameters in a function: use braces: &lt;code&gt;$10&lt;&#x2F;code&gt; wont work, but &lt;code&gt;${10}&lt;&#x2F;code&gt; does.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Advent of Code 2021</title>
        <published>2021-12-07T00:00:00+00:00</published>
        <updated>2021-12-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://colas.nahaboo.net/code/advent-of-code-2021/"/>
        <id>https://colas.nahaboo.net/code/advent-of-code-2021/</id>
        
        <content type="html" xml:base="https://colas.nahaboo.net/code/advent-of-code-2021/">&lt;p&gt;I just discovered the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;&quot;&gt;Advent of code&lt;&#x2F;a&gt; coding challenge. It exists since 2015, and seems really interesting in that it is language-agnostic.&lt;&#x2F;p&gt;
&lt;p&gt;Each day a problem is described, with input files consisting of numeric values for the problem. You must submit the answer which is a number by whatever means you want.&lt;&#x2F;p&gt;
&lt;p&gt;I started to do the current 2021 challenge, in &lt;strong&gt;bash&lt;&#x2F;strong&gt; to force myself to code in a more modern style that pass &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.shellcheck.net&#x2F;&quot;&gt;shellcheck&lt;&#x2F;a&gt;. And later, i will probably do the other years in &lt;strong&gt;Go&lt;&#x2F;strong&gt; to learn it.&lt;&#x2F;p&gt;
&lt;p&gt;I publish my solutions as I code them on &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ColasNahaboo&#x2F;advent-of-code-my-solutions&#x2F;tree&#x2F;main&#x2F;bash&#x2F;2021&quot;&gt;my github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;More info:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;&quot;&gt;advent of code&lt;&#x2F;a&gt; (aka AOC) site&lt;&#x2F;li&gt;
&lt;li&gt;The sub-reddit  &lt;a href=&quot;&#x2F;r&#x2F;adventofcode&quot;&gt;&#x2F;r&#x2F;adventofcode&lt;&#x2F;a&gt; to discuss AOC&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Mercurial web templating</title>
        <published>2008-10-09T00:00:00+00:00</published>
        <updated>2008-10-09T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://colas.nahaboo.net/blog/mercurial-web-templating/"/>
        <id>https://colas.nahaboo.net/blog/mercurial-web-templating/</id>
        
        <content type="html" xml:base="https://colas.nahaboo.net/blog/mercurial-web-templating/">&lt;p&gt;For some time now, I have seen the light and I switched to the new wave
of the Distributed Source Control management systems. &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4XpnKHJAok8&quot;&gt;Linus famous
video&lt;&#x2F;a&gt; of his Google talk
decided me to try. I was a bit apprehensive at first, wary of engaging
myself on a technology that would bring more problems than solutions,
but after some days of use, the realisation dawned over me: Distributed
Source Control may be one little step for a programmer, but it is a
giant step for programming. Why? because, its &lt;strong&gt;mental model actually
follows your first intuitions&lt;&#x2F;strong&gt; that most of us developed as young
programmers before using any source control system. Suppose you want to
try a feature? instead of just copying the directory, you clone it. You
mess an operation? you just remove the directory, no embarrassing traces
left to keep the burning scar of shame on you for the following decades
of a central repository. You are used to think of your enhancements as
patches, you can work with patches. I could go on and on, but many
people have done it much better than me so I&#x27;ll just say that you should
definitely try it.&lt;&#x2F;p&gt;
&lt;p&gt;By the way, between the 3 main contenders, &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;git-scm.com&#x2F;&quot;&gt;git&lt;&#x2F;a&gt;,
&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.mercurial-scm.org&#x2F;&quot;&gt;mercurial&lt;&#x2F;a&gt; (aka HG), and
&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;GNU_Bazaar&quot;&gt;bazaar&lt;&#x2F;a&gt;, I chose mercurial because of its
simplicity, its better support of windows (I work in Linux since 1995
but my coworkers currently use Windows), and because some people at ILOG
started using git so I wanted to be able to try something else. I did
not choose bazaar as I wanted to stay close enough of git, to be able to
switch to it if ever my dream to work in a windows-free world
materializes one day...&lt;&#x2F;p&gt;
&lt;p&gt;So I started to set up a public web repository of my Open Source personal
work with mercurial, which gave me a simple way to publish my work in
full detail, but, although mercurial is quite easy to use, but I had a
bit of trouble figuring out how to customize its look &amp;amp; feel.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
