`cal -h` command: Bash substring `$string:offset:length` error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
5
down vote

favorite












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?










share|improve this question



















  • 1




    You might want to make an MCVE
    – wjandrea
    Mar 11 at 20:09






  • 3




    @wjandrea MCVE=Move Computer Violently Elsewhere?
    – WinEunuuchs2Unix
    Mar 11 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 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 at 5:48














up vote
5
down vote

favorite












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?










share|improve this question



















  • 1




    You might want to make an MCVE
    – wjandrea
    Mar 11 at 20:09






  • 3




    @wjandrea MCVE=Move Computer Violently Elsewhere?
    – WinEunuuchs2Unix
    Mar 11 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 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 at 5:48












up vote
5
down vote

favorite









up vote
5
down vote

favorite











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?










share|improve this question















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?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 29 at 17:11

























asked Mar 11 at 19:47









WinEunuuchs2Unix

36k759134




36k759134







  • 1




    You might want to make an MCVE
    – wjandrea
    Mar 11 at 20:09






  • 3




    @wjandrea MCVE=Move Computer Violently Elsewhere?
    – WinEunuuchs2Unix
    Mar 11 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 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 at 5:48












  • 1




    You might want to make an MCVE
    – wjandrea
    Mar 11 at 20:09






  • 3




    @wjandrea MCVE=Move Computer Violently Elsewhere?
    – WinEunuuchs2Unix
    Mar 11 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 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 at 5:48







1




1




You might want to make an MCVE
– wjandrea
Mar 11 at 20:09




You might want to make an MCVE
– wjandrea
Mar 11 at 20:09




3




3




@wjandrea MCVE=Move Computer Violently Elsewhere?
– WinEunuuchs2Unix
Mar 11 at 20:10




@wjandrea MCVE=Move Computer Violently Elsewhere?
– WinEunuuchs2Unix
Mar 11 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 at 5:32





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 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 at 5:48




@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 at 5:48










2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










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.






share|improve this answer




















  • 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 at 4:44

















up vote
1
down vote













@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 '1112154060-136140-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.






share|improve this answer




















  • I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
    – WinEunuuchs2Unix
    Apr 29 at 4:42










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1013954%2fcal-h-command-bash-substring-stringoffsetlength-error%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
8
down vote



accepted










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.






share|improve this answer




















  • 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 at 4:44














up vote
8
down vote



accepted










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.






share|improve this answer




















  • 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 at 4:44












up vote
8
down vote



accepted







up vote
8
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 11 at 20:18









Martin Thornton

2,43541730




2,43541730











  • 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 at 4:44
















  • 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 at 4:44















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 at 4:44




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 at 4:44












up vote
1
down vote













@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 '1112154060-136140-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.






share|improve this answer




















  • I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
    – WinEunuuchs2Unix
    Apr 29 at 4:42














up vote
1
down vote













@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 '1112154060-136140-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.






share|improve this answer




















  • I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
    – WinEunuuchs2Unix
    Apr 29 at 4:42












up vote
1
down vote










up vote
1
down vote









@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 '1112154060-136140-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.






share|improve this answer












@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 '1112154060-136140-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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 26 at 23:13









Steve H

112




112











  • I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
    – WinEunuuchs2Unix
    Apr 29 at 4:42
















  • I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
    – WinEunuuchs2Unix
    Apr 29 at 4:42















I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
– WinEunuuchs2Unix
Apr 29 at 4:42




I performed tests with Ubuntu 18.04 and cal -h is no longer supported. Your solution to remove all formatting works. I've updated the script: askubuntu.com/questions/1020692/…
– WinEunuuchs2Unix
Apr 29 at 4:42

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1013954%2fcal-h-command-bash-substring-stringoffsetlength-error%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Which professions warranted travel in Medieval times?

How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

Trouble downloading packages list due to a “Hash sum mismatch” error