blog-ready.md (2245B)
1 # Getting my blog ready for 2023 2 3 My first year of blogging is about to end, and I am happy with what 4 I wrote. I wanted to write at least one post every month, and I did. I 5 intend to keep this pace next year, but I want to make this easier by 6 writing shorter posts from time to time. This is not a trivial task: 7 I found out that writing good *short* content is harder than writing 8 good *long* content! 9 10 But if I keep writing, [my blogs's index page](../) is going to become 11 messy at some point! It would be nice to divide these posts by year... 12 13 ## Adding year sections to my blog index 14 15 Very easy: in my 16 [build.sh](https://git.tronto.net/sebastiano.tronto.net/file/build.sh.html) 17 script that I run to 18 [build my website](../2022-08-14-website), there is a 19 [`makeblog()`](https://git.tronto.net/sebastiano.tronto.net/file/build.sh.html#l52) 20 function that takes care of building the index page and RSS feed for my blog. 21 22 It is enough to add the following lines inside its main loop: 23 24 ``` 25 thisyear=$(echo $d | sed 's/-.*//') 26 if [ "$thisyear" != "$lastyear" ]; then 27 printf "\n## $thisyear\n\n" >> $bf 28 lastyear=$thisyear 29 fi 30 ``` 31 32 And that's it! These few lines introduce two new variables, `thisyear` 33 and `lastyear`, that keep track of the years of the last and next blog 34 post that the loop is scanning. If there was a year change, a new line 35 with the current year is added, and the `lastyear` variable is updated. 36 The first line refers to a variable `d` that holds the date of the 37 current post in `yyyy-mm-dd` format. 38 39 A last note on the variables: if you are familiar with other programming 40 languages, you might wonder where the variable `lastyear` is initialized. 41 After all, I am using it in the `if` statement's condition, so it must 42 be initialized outside of its body, right? 43 44 Actually, no. The shell's variable scoping does not work like in C or 45 similar languages, and a variable initialized inside a block is also 46 visibile outside of it. Moreover, un-initialized variables evaluate to 47 the empty string, so the first time the condition is checked it correctly 48 determines that the current year is different from the last. 49 50 This was my last UNIX shell tip for this year. Stay tuned for more! 51 52 ![My netbook and planner for 2023](pc-planner.jpg)