5

Beware the Ides of March Caesar

I'm getting a weird error in bash using: ${string:offset:length}.

The fifth output line for the middle of March has the wrong output:

$ substring_test.sh
     March 2018       
Su Mo Tu We Th Fr Sa  
             1  2  3  T=   T=   T=   T=   T= 1 T= 2 T= 3 
 4  5  6  7  8  9 10  T= 4 T= 5 T= 6 T= 7 T= 8 T= 9 T=10 
11 12 13 14 15 16 17  T= T= T= 1 T= 1 T= 1 T= 1 T= 1 
18 19 20 21 22 23 24  T=18 T=19 T=20 T=21 T=22 T=23 T=24 
25 26 27 28 29 30 31  T=25 T=26 T=27 T=28 T=29 T=30 T=31 
                      T=   T=   T=   T=   T=   T=   T=   

It is showing:

T= T= T= 1 T= 1 T= 1 T= 1 T= 1

But it should show:

T=11 T=12 T=13 T=14 T=15 T=16 T=17

The code is pretty straight forward:

#!/bin/bash

cal > /tmp/terminal

CalLineCnt=1
Today=$(date +"%d")

# Prefix with space when length < 2
if [[ ${#Today} < 2 ]] ; then
    Today=" "$Today
fi

while IFS= read -r Cal; do
    printf "$Cal"
    if [[ $CalLineCnt > 2 ]] ; then
        # See if today is on current line & invert background
        for (( j=0 ; j <= 18 ; j += 3 )) ; do
            Test=${Cal:$j:2}    # Current day on calendar line
printf "T=$Test "
            if [[ "$Test" == "$Today" ]] ; then
                printf "Offset: $j "
            fi
        done
    fi
    tput cud1           # Down one line
    CalLineCnt=$((++CalLineCnt))
done < /tmp/terminal

Can anyone point me in the right direction?


End Result

Applying fix of cal -h recommended below it worked fine until Ubuntu 18.04 LTS was released and tested on April 28, 2018. Now the fix recommended by @Steve H is used:

now.png

Some are curious about the what the code does so I provided the screen shot above. Others have asked for the full code and it is available here:How can I get this terminal splash screen?

  • 1
    You might want to make an MCVE – wjandrea Mar 11 '18 at 20:09
  • 3
    @wjandrea MCVE=Move Computer Violently Elsewhere? – WinEunuuchs2Unix Mar 11 '18 at 20:10
  • Out of curiosity, why can't you just let cal do the highlighting and instead do something like echo $weather; cal? – Nic Hartley Mar 12 '18 at 5:32
  • @NicHartley I updated the question with a snapshot after the problem was fixed. Before inserting the weather, cal was simply called with no need to highlight the day manually. – WinEunuuchs2Unix Mar 12 '18 at 5:48
8

Your problem is that cal is already highlighting the current day, and the control codes are throwing off the offsets

Look at /tmp/terminal in a hex editor. Today (the 11th) is: 5F 08 31 5F 08 31, and not 31 31

Use cal -h to switch off the auto highlighting of today's date.

  • I regret to report that cal -h no longer works with Ubuntu 18.04 LTS. I've had to switch code to @Steve H answer below. – WinEunuuchs2Unix Apr 29 '18 at 4:44
1

@Martin Thornton would be OK if all implementations of cal actually recognised the -h as switching off formatting. Debian doesn't even though man suggests it does. It displays the usage instead. So this would work.

cal > /tmp/terminal1
tr -cd '\11\12\15\40\60-\136\140-\176' < /tmp/terminal1  > /tmp/terminal

The file gets the cal output with formatting and the next line removes anything we don't want and lets the rest of the code do its work.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.