-
Basic shell script
Hi,
I'm just learning shell script using Kernighan and Pike's "Unix
Programming Environment" using the bash shell and an example isn't
working as it should.
Their modified cal program is:
# calx: Nicer interface to /usr/bin/cal.
case $# in
0) set 'date'; m=$2; y=$6 ;; # No args : use today.
1) m=$1; set 'date'; y=$6 ;; # One arg : use this year.
*) m=$1; y=$2 ;; # Two args: month and year.
esac
case $m in
jan*|Jan*) m=1 ;;
feb*|Feb*) m=2 ;;
mar*|Mar*) m=3 ;;
apr*|Apr*) m=4 ;;
may*|May*) m=5 ;;
jun*|Jun*) m=6 ;;
jul*|Jul*) m=7 ;;
aug*|Aug*) m=8 ;;
sep*|Sep*) m=9 ;;
oct*|Oct*) m=10 ;;
nov*|Nov*) m=11 ;;
dec*|Dec*) m=12 ;;
[1-9]|10|11|12) ;; # Numeric month
*) y=$m; m="" ;; # Plain year
esac
echo "The number of arguments passed is " $#
echo "Month is "$m
echo "Year is "$y
/usr/bin/cal $m $y # Run the real one.
However, when I run this on a server using the bash shell, if just a
single argument is passed, it appears to run the last case (*)).
No arguments works okay.
bash-2.05b$ calx
calx
+ calx
The number of arguments passed is 1
Month is
Year is
May 2007
Su Mo Tu We Th Fr Sa
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
Month and year also work okay.
bash-2.05b$ calx may 2007
calx may 2007
+ calx may 2007
The number of arguments passed is 2
Month is 5
Year is 2007
May 2007
Su Mo Tu We Th Fr Sa
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
Passing just the month does not work, the year is not populated.
bash-2.05b$ calx may
calx may
+ calx may
The number of arguments passed is 1
Month is 5
Year is
5
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31
April May June
I think it might be because when I pass just a single argument to the
global cal, it prints out the calendar for the year:
bash-2.05b$ cal2 may
cal2 may
+ cal2 may
The number of arguments passed is 1
Month is 5
Year is
5
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31
April May June
Is this the reason my shell script will not work?
Kim
-
Re: Basic shell script
On 13 May 2007 04:15:42 -0700, kimi
wrote:
>
>
> Hi,
> I'm just learning shell script using Kernighan and Pike's "Unix
> Programming Environment" using the bash shell and an example isn't
> working as it should.
>
> Their modified cal program is:
>
> # calx: Nicer interface to /usr/bin/cal.
>
> case $# in
> 0) set 'date'; m=$2; y=$6 ;; # No args : use today.
set `date`; # if you don't see a difference, use another font
--
Those who don't know, talk. Those who don't talk, know.
-
Re: Basic shell script
On May 13, 8:19 am, Bill Marcum wrote:
> On 13 May 2007 04:15:42 -0700, kimi wrote:
>
> > Hi,
> > I'm just learning shell script using Kernighan and Pike's "Unix
> > Programming Environment" using the bash shell and an example isn't
> > working as it should.
>
> > Their modified cal program is:
>
> > # calx: Nicer interface to /usr/bin/cal.
>
> > case $# in
> > 0) set 'date'; m=$2; y=$6 ;; # No args : use today.
>
> set `date`; # if you don't see a difference, use another font
>
> --
> Those who don't know, talk. Those who don't talk, know.
Same results, not sure what font has to do with the program, please
explain.
-
Re: Basic shell script
kimi wrote:
> On May 13, 8:19 am, Bill Marcum wrote:
>
>>On 13 May 2007 04:15:42 -0700, kimi wrote:
>>
>>
>>>Hi,
>>>I'm just learning shell script using Kernighan and Pike's "Unix
>>>Programming Environment" using the bash shell and an example isn't
>>>working as it should.
>>
>>>Their modified cal program is:
>>
>>># calx: Nicer interface to /usr/bin/cal.
>>
>>>case $# in
>>>0) set 'date'; m=$2; y=$6 ;; # No args : use today.
>>
>> set `date`; # if you don't see a difference, use another font
>>
>>--
>>Those who don't know, talk. Those who don't talk, know.
>
>
> Same results, not sure what font has to do with the program, please
> explain.
>
The quotes around "date" are different and have different semantics.
$ set 'date'
$ echo $*
date
$ set `date`
$ echo $*
Mon May 14 01:12:38 CEST 2007
Janis
-
Re: Basic shell script
On 13 May 2007 15:52:43 -0700, kimi
wrote:
>
>
> On May 13, 8:19 am, Bill Marcum wrote:
>> On 13 May 2007 04:15:42 -0700, kimi wrote:
>>
>> > Hi,
>> > I'm just learning shell script using Kernighan and Pike's "Unix
>> > Programming Environment" using the bash shell and an example isn't
>> > working as it should.
>>
>> > Their modified cal program is:
>>
>> > # calx: Nicer interface to /usr/bin/cal.
>>
>> > case $# in
>> > 0) set 'date'; m=$2; y=$6 ;; # No args : use today.
>>
>> set `date`; # if you don't see a difference, use another font
>>
>> --
>> Those who don't know, talk. Those who don't talk, know.
>
> Same results, not sure what font has to do with the program, please
> explain.
>
I was just saying you might be using a font where '' and `` look the
same.
set `date`; m=$2; y=$6; echo $m $y
--
jogger, n.:
An odd sort of person with a thing for pain.
-
Re: Basic shell script
The carbonbased lifeform kimi inspired comp.unix.shell with:
> On May 13, 8:19 am, Bill Marcum wrote:
>> On 13 May 2007 04:15:42 -0700, kimi wrote:
>>
>> > Hi,
>> > I'm just learning shell script using Kernighan and Pike's "Unix
>> > Programming Environment" using the bash shell and an example isn't
>> > working as it should.
>>
>> > Their modified cal program is:
>>
>> > # calx: Nicer interface to /usr/bin/cal.
>>
>> > case $# in
>> > 0) set 'date'; m=$2; y=$6 ;; # No args : use today.
>>
>> set `date`; # if you don't see a difference, use another font
>>
>> --
>> Those who don't know, talk. Those who don't talk, know.
>
> Same results, not sure what font has to do with the program, please
> explain.
Use $(), not backtics
Theo
--
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
"ik _heb_ niets tegen Microsoft, ik heb iets tegen
de uitwassen *van* Microsoft"
-
Re: Basic shell script
On May 14, 4:43 pm, "Theo v. Werkhoven"
werkhoven.nl.invalid> wrote:
> The carbonbased lifeform kimi inspired comp.unix.shell with:
>
>
>
> > On May 13, 8:19 am, Bill Marcum wrote:
> >> On 13 May 2007 04:15:42 -0700, kimi wrote:
>
> >> > Hi,
> >> > I'm just learning shell script using Kernighan and Pike's "Unix
> >> > Programming Environment" using the bash shell and an example isn't
> >> > working as it should.
>
> >> > Their modified cal program is:
>
> >> > # calx: Nicer interface to /usr/bin/cal.
>
> >> > case $# in
> >> > 0) set 'date'; m=$2; y=$6 ;; # No args : use today.
>
> >> set `date`; # if you don't see a difference, use another font
>
> >> --
> >> Those who don't know, talk. Those who don't talk, know.
>
> > Same results, not sure what font has to do with the program, please
> > explain.
>
> Use $(), not backtics
>
> Theo
> --
> theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
> linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
> "ik _heb_ niets tegen Microsoft, ik heb iets tegen
> de uitwassen *van* Microsoft"
This worked, thanks for the information. The backtics are showing up
incorrectly in this posting for some reason. If I copy and paste out
of the terminal application to a text file, they are backtics; if
posted to this site, they're not backtics.
Thanks for the assistance...