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

Clash 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:

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
add a comment |Â
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:

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
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 letcaldo the highlighting and instead do something likeecho $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,calwas simply called with no need to highlight the day manually.
â WinEunuuchs2Unix
Mar 12 at 5:48
add a comment |Â
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:

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
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:

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
bash
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 letcaldo the highlighting and instead do something likeecho $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,calwas simply called with no need to highlight the day manually.
â WinEunuuchs2Unix
Mar 12 at 5:48
add a comment |Â
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 letcaldo the highlighting and instead do something likeecho $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,calwas 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
add a comment |Â
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.
I regret to report thatcal -hno longer works with Ubuntu 18.04 LTS. I've had to switch code to @Steve H answer below.
â WinEunuuchs2Unix
Apr 29 at 4:44
add a comment |Â
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.
I performed tests with Ubuntu 18.04 andcal -his 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
add a comment |Â
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.
I regret to report thatcal -hno longer works with Ubuntu 18.04 LTS. I've had to switch code to @Steve H answer below.
â WinEunuuchs2Unix
Apr 29 at 4:44
add a comment |Â
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.
I regret to report thatcal -hno longer works with Ubuntu 18.04 LTS. I've had to switch code to @Steve H answer below.
â WinEunuuchs2Unix
Apr 29 at 4:44
add a comment |Â
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.
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.
answered Mar 11 at 20:18
Martin Thornton
2,43541730
2,43541730
I regret to report thatcal -hno longer works with Ubuntu 18.04 LTS. I've had to switch code to @Steve H answer below.
â WinEunuuchs2Unix
Apr 29 at 4:44
add a comment |Â
I regret to report thatcal -hno 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
add a comment |Â
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.
I performed tests with Ubuntu 18.04 andcal -his 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
add a comment |Â
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.
I performed tests with Ubuntu 18.04 andcal -his 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
add a comment |Â
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.
@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.
answered Apr 26 at 23:13
Steve H
112
112
I performed tests with Ubuntu 18.04 andcal -his 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
add a comment |Â
I performed tests with Ubuntu 18.04 andcal -his 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
caldo the highlighting and instead do something likeecho $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,
calwas simply called with no need to highlight the day manually.â WinEunuuchs2Unix
Mar 12 at 5:48