Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my Unix Assignment where I need to write a script which shows today's date in calender and here's my code and the output. My professor gave me grade with feedback saying "fix the command there is a bug in it". I can't seem to figure out the bug. Any help will be great!

command is $c5/n*/4*/s*/today

Where those are just some directories and today is the name of my file. When I type in that command I see what I want to see which is output of my script, then how can there be a bug in my command.

#!/usr/dt/bin/dtksh

date
if [ $# -ge 1 ] ; then
exec $HOME/bin/cal $*
fi
#highlight today on this months calendar

daynumber=`date +%d`
#tput gets terminal specific characters, e.g. clear
rmso=`tput rmso` # get the chars for reverse video for this
smso=`tput smso` # terminal using tput

if [ $daynumber -lt 10 ] ; then
daynumber=" `echo $daynumber | sed 's/^0//`"
# daynumber=" `echo $daynumber | cut -c 2`"
fi

if cal | grep "$daynumber\$" > /dev/null; then
cal | sed "2,$ s/$daynumber/$smso$daynumber$rmso /"
else
cal | sed "2,$ s/$daynumber /$smso$daynumber$rmso /"
fi

Output... 30 will be highlighted since that is today's date.

Sun Mar 30 14:22:31 CDT 2014
   March 2014
 S  M Tu  W Th  F  S
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
**30** 31
share|improve this question
    
Not just one bug. $* and "$@" are very different things, and you almost certainly want the latter. –  Charles Duffy Mar 30 at 19:38
    
...also, there are much more efficient ways to kill leading zeros. daynumber=${daynumber%0} is much, much faster than forking, starting a pipeline, waiting for it to exit, etc. –  Charles Duffy Mar 30 at 19:40
    
You are missing a single quote in the daynumber=...sed... line. –  Jonathan Leffler Mar 30 at 19:40
    
He actually said there is bug in your command. Not my script –  user3478869 Mar 30 at 19:54
    
Why don't you ask him what he means? –  ruakh Mar 30 at 19:58

1 Answer 1

up vote 0 down vote accepted

The problem is "write a script which shows today's date in calender "

If I understand correctly, you are building a "fork" of cal. Your script doesn't use any of input from cmdline :

if [ $# -ge 1 ] ; then
exec $HOME/bin/cal $*
fi

=> remove that block.

It uses this as an input for the day of today ( which is what you want) :

daynumber=`date +%d`

Bonus : cal can highlight day by default, check cal manpage.

turn off highlighting ( -h)

share|improve this answer
    
Anyway Charles Duffy is right, $* is a bad idea. Check advanced bash guide. tldp.org/LDP/abs/html/internalvariables.html see Inconsistent $* and $@ behavior –  rMistero Mar 30 at 20:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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