[comp.unix.shell] bourne shell query

fred@maccs.dcss.mcmaster.ca (Fred Whiteside) (08/30/90)

	i have a simple little shell question.  I am attempting
to write a script that will perform various manipulations on news
articles.  my problem seems to be with understanding variable
substitution in sh.  a minimal example is the enclosed sh script.  what
it should do is set the variable files to be the names of those
articles in comp.std.c whose name (article number) is greater than or
equal to the numeric value of the scripts first argument.  this does
not behave as expected.  i have tried quite a unmber of variations on
this theme (mostly centering around multiple $'s and interesting
quoting of commands, but i tend to get all of the files or none of them
depending on (for some reason) the number of $'s preceding the zots
variable name (also on  the number in front of the 1, $1 or $$1, etc.
but i expected that ...) this is running under sunos4.1 if that is
interesting.  i have tried to read the manual, but with little
success.  anyone interested in pointing out my clear stupidity
publicly?  i *would* appreciate it ....

	many thanks ...

#!/bin/sh
cd /usr/spool/news/comp/std/c
zots=$1
echo "Should ignore files with name < $zots"
files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
echo $files

-- 
Fred Whiteside   Chedoke-McMaster Hospitals, Hamilton, Ontario, Canada
whitesid@{mcmaster.ca,darwin.cmh.mcmaster.ca,130.113.2.18}
fred@maccs.DCSS.McMaster.CA   ...!uunet!utai!utgpu!maccs!fred

jtp@ngs.fi (Jukka Partanen) (08/30/90)

In article <26DC6447.15922@maccs.dcss.mcmaster.ca> fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes:
>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`

files=`ls -rt | grep -v '.*[a-zA-Z].*' | awk '$1 >= '$zots' {print}`

--

	  A mountain is something you don't wanna fuck with.

jtp@ngs.fi (Jukka Partanen) (08/30/90)

In article <JTP.90Aug30141736@ngs.fi> jtp@ngs.fi (Jukka Partanen) writes:

	Argh, forgot one '

>files=`ls -rt | grep -v '.*[a-zA-Z].*' | awk '$1 >= '$zots' {print}`

files=`ls -rt | grep -v '.*[a-zA-Z].*' | awk '$1 >= '$zots' {print}'`
--

	  A mountain is something you don't wanna fuck with.

jeff@quark.WV.TEK.COM (Jeff Beadles) (08/30/90)

fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes:
:>
:>	i have a simple little shell question.  I am attempting
:>to write a script that will perform various manipulations on news
:>articles.  my problem seems to be with understanding variable
:>substitution in sh.  a minimal example is the enclosed sh script.  what
:>it should do is set the variable files to be the names of those
:>articles in comp.std.c whose name (article number) is greater than or
:>equal to the numeric value of the scripts first argument.  this does
:>not behave as expected.  i have tried quite a unmber of variations on
:>this theme (mostly centering around multiple $'s and interesting
:>quoting of commands, but i tend to get all of the files or none of them
:>depending on (for some reason) the number of $'s preceding the zots
:>variable name (also on  the number in front of the 1, $1 or $$1, etc.
:>but i expected that ...) this is running under sunos4.1 if that is
:>interesting.  i have tried to read the manual, but with little
:>success.  anyone interested in pointing out my clear stupidity
:>publicly?  i *would* appreciate it ....
:>
:>	many thanks ...
:>
:>#!/bin/sh
:>cd /usr/spool/news/comp/std/c
:>zots=$1
:>echo "Should ignore files with name < $zots"
:>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
              I see a quoting problem here     ^^^^^^^^^^^^^^^^^^^^^
:>echo $files
:>
:>-- 
:>Fred Whiteside   Chedoke-McMaster Hospitals, Hamilton, Ontario, Canada
:>whitesid@{mcmaster.ca,darwin.cmh.mcmaster.ca,130.113.2.18}
:>fred@maccs.DCSS.McMaster.CA   ...!uunet!utai!utgpu!maccs!fred




>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`

This line is the problem, replace it with:

files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk "\$1 >= $zots {print}"`

Or, to make it a little more simpler... (You don't need .*'s in the grep)

files=`ls -rt * |grep -v '[a-zA-Z]' | awk "\$1 >= $zots {print}"`

To explain:

The only real change was to change thw quoting around the awk script.
$1 was getting expanded to the shell's $1, not the 1st awk field.

By using " as a quoting character, the shell is able to expand $zot to whatever
number it represents.  The \$1 is to prevent the shell from trying to expand $1
to the shell's $1, rather than the 1st awk field.

After the shell is finished with variable substitution,

awk "\$1 >= $zots {print}"

is transformed, and awk see's:  (if zot = 10)

awk "$1 >= 10 {print}"


Hope this helps!  A decent book on shell programming and quoting is the Howard
Sam's book on shell programming.

	-Jeff
-- 
Jeff Beadles				jeff@quark.WV.TEK.COM 
Utek Engineering, Tektronix Inc.	+1 503 685 2568
			SPEEA - Just say no.

scott@tab00.larc.nasa.gov (Scott Yelich) (08/30/90)

>:>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
>I see a quoting problem here     ^^^^^^^^^^^^^^^^^^^^^
>files=`ls -rt * |grep -v '[a-zA-Z]' | awk "\$1 >= $zots {print}"`
>To explain:
> The only real change was to change thw quoting around the awk script.
>$1 was getting expanded to the shell's $1, not the 1st awk field.
>   By using " as a quoting character, the shell is able to expand $zot
>to whatever number it represents.  The \$1 is to prevent the shell
>from trying to expand $1 to the shell's $1, rather than the 1st awk
>field.

I still see a quoting problem here...
At least, when I try this-- it doesn't work.

I have only been able to get awk to work properly if I use
single quotes and not double quotes.

As a side question, does ANYONE have any bourne shell routines which do
math... reasonably effeciently?  (For numbers > 1000?)
--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------

brister@decwrl.dec.com (James Brister) (08/30/90)

On 30 Aug 90 00:56:38 GMT,
fred@maccs.dcss.mcmaster.ca (Fred Whiteside) said:

> #!/bin/sh
> cd /usr/spool/news/comp/std/c
> zots=$1
> echo "Should ignore files with name < $zots"
> files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
> echo $files

I changed the next to last line to this:
files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk '$1 >= '$zots' {print}'`


Awk expects its program to be one argument. You've given it four. You DON'T
want the $1 to be expanded, but you DO want the $zots to be expanded, hence
the funny quoting.

James
--
James Brister                                           brister@decwrl.dec.com
DEC Western Software Lab., Palo Alto, California.         .....!decwrl!brister

jeff@quark.WV.TEK.COM (Jeff Beadles) (08/31/90)

scott@tab00.larc.nasa.gov (Scott Yelich) writes:

>As a side question, does ANYONE have any bourne shell routines which do
>math... reasonably effeciently?  (For numbers > 1000?)

Well, I use expr.  For example:

a=123456
b=123

c=`expr $a - $b`

d=`expr $a \* $b`

echo "c = $c"
echo "d = $d"

Note, that the '*' MUST be quoted, as the shell will expand it.

"Shell qouting --  Know it, live it, be it.  -Jeff"    :-)

	-Jeff
-- 
Jeff Beadles				jeff@quark.WV.TEK.COM 
Utek Engineering, Tektronix Inc.	+1 503 685 2568
			SPEEA - Just say no.

mvadh@cbnews.att.com (andrew.d.hay) (08/31/90)

In article <26DC6447.15922@maccs.dcss.mcmaster.ca>, fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes:
[]
"								     what
" it should do is set the variable files to be the names of those
" articles in comp.std.c whose name (article number) is greater than or
" equal to the numeric value of the scripts first argument.
[]
" 
" #!/bin/sh
" cd /usr/spool/news/comp/std/c
" zots=$1
" echo "Should ignore files with name < $zots"
" files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
" echo $files

how about:
#!/bin/sh
cd /usr/spool/news/comp/std/c
echo "Should ignore files with name < $1 (formerly $zots)"
for TMP in `echo * | grep -v '[a-zA-Z]'`; do if [ "$TMP" -ge $1 ]; then echo "$TMP"; fi; done

or, if you -need- to create a variable:
files=`ls -rt | grep -v '[a-zA-Z]' | while read TMP; do if [ "$TMP" -ge $1 ]; then echo "$TMP"; fi; done`
echo $files

these should also be much faster, as it relies more on shell builtins.

i'm not familiar with awk (shame on me!), but i don't see why you need
it for this...

-- 
Andrew Hay		+------------------------------------------------------+
Ragged Individualist	| 	You just have _N_O idea!  It's the difference    |
AT&T-BL Ward Hill MA	|	between _S_H_O_O_T_I_N_G a bullet and _T_H_R_O_W_I_N_G it!     |
a.d.hay@att.com		+------------------------------------------------------+

mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) (09/01/90)

In article <8465@orca.wv.tek.com> jeff@quark.WV.TEK.COM (Jeff Beadles) writes:
:scott@tab00.larc.nasa.gov (Scott Yelich) writes:
:
:>As a side question, does ANYONE have any bourne shell routines which do
:>math... reasonably effeciently?  (For numbers > 1000?)
:
:Well, I use expr.  For example:
:
:a=123456
:b=123
:
:c=`expr $a - $b`
:
:d=`expr $a \* $b`
:
:echo "c = $c"
:echo "d = $d"
:
:Note, that the '*' MUST be quoted, as the shell will expand it.
:
:"Shell qouting --  Know it, live it, be it.  -Jeff"    :-)
:
:	-Jeff
:-- 
:Jeff Beadles				jeff@quark.WV.TEK.COM 
:Utek Engineering, Tektronix Inc.	+1 503 685 2568
:			SPEEA - Just say no.


If your system has named pipes - fifos - and bc then you may find this
useful:


# 
# these instructions illustrate how to set up a background
# engine in a shell script - a simple application is shown only
# for illustration purposes - it numbers lines of standard input

# first allocate fifos

ITMP=/usr/tmp/i.$LOGNAME
OTMP=/usr/tmp/o.$LOGNAME

/bin/rm -f $ITMP $OTMP 2>/dev/null
/etc/mknod $ITMP p  # input fifo for engine
/etc/mknod $OTMP p  # output fifo for engine

# start background engine and open $ITMP as file descriptor 3
# and $OTMP as file descriptor 4

bc <$ITMP >$OTMP &
exec 3>$ITMP
exec 4<$OTMP

# send command to background engine
# if background engine is bc,  counters can be maintained
# echo "x = 1"
# echo "x = x + 1" or echo "++x"
#the second version returns the new value of x

echo "x = 0" >&3

while line=`line`
	do
	echo "x++" >&3
	# read response
	read ans <&4
	echo "$ans: $line"
	done < engine

# terminate background engine and remove fifos

echo "quit" >&3
/bin/rm -f $ITMP $OTMP

exit 0
-- 

Dan Mercer
Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer)
"MAN - the only one word oxymoron in the English Language"

smithln@honeydew.cs.rochester.edu (Neil Smithline) (09/01/90)

In article <26DC6447.15922@maccs.dcss.mcmaster.ca>, fred@maccs (Fred Whiteside) writes:
>
>...
>
>#!/bin/sh
>cd /usr/spool/news/comp/std/c
>zots=$1
>echo "Should ignore files with name < $zots"
>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
>echo $files
>

The best way to figure this out is to go into the shell and start
typing.  To find the problem, run /bin/sh and go into the correct
directory.  Once there try the command
	ls -rt *
and see if this produces the correct output - if it does see what
happens if you add the grep.  If this works try the awk.  You'll see
that this causes awk to bail out.  Even if you reduce the awk
statement to
	awk $1 '>=' 1 {print}
awk still gives you an error message.  From this you can conclude that
this has nothing to do with the rest of the line and resort to the old
awk man page.  If you quote the arguments, all works fine.  That is:
	awk '$1 >= 1 {$print}'
works.  Now to add the shell variable $zots to this just insert it
*unqoted* into the line
	zots=1
	awk '$1 >= '$zots' {print}'
Once you have this just replace the awk statement in your program with
the above line and all works properly.

In general, I find that complex shell quoting is best solved with an
inside-out approach, get the inside to work and then add the next
level of quotes to that - of course, as you get better you need to
do less and less of this - Neil
-- 
========================================================================
Neil Smithline
ARPA:    smithln@cs.rochester.edu
UUCP:    ..!rutgers!rochester!smithln 

roger@kymijoki.wrs.com (Roger Rohrbach) (09/01/90)

fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes:

>cd /usr/spool/news/comp/std/c
>zots=$1
>echo "Should ignore files with name < $zots"
>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
>echo $files


There are two problems: first, awk expects its first argument to be a
an awk program; you are giving the program as 4 separate arguments
($1, '>=', $zots, {print}), so your program currently will cause awk
to bail out.  Second, you are not escaping the "$1" in your awk program,
so the shell is substituting the first parameter to your script there
(i.e., awk is seeing the number you gave as an argument, not "$1").
If you change the last part of your pipeline to

    awk '$1 >= '$zots' {print}'

it will work, because the "$1" is quoted for awk's sake, and all white
space is quoted to prevent the shell from passing 4 strings, not 1, to
awk as arguments.  Note that we step out of the quotes long enough to
let the shell substitute for "$zots".


ADVANCED DISCUSSION:

One might think you could use double quotes around the whole awk program,
and just escape the "$1" but not "$zots", thus:

    awk "\$1 >= $zots {print}"

and indeed this would work if it were not for the fact that the entire
pipeline is enclosed in backquotes.  A peculiarity of backquoting is
that it forces an extra level of evaluation, so "\$1" is no safer than
"$1".  You must use

    awk "\\$1 >= $zots {print}"

to get this to work.

Another subtlety: if your program were not doing a numeric comparison,
you'd have to arrange for "$zots" to be presented to awk as a string.
My first example would become something like

    awk '$1 == "'$zots'" {print}'

and the second,

    awk "\\$1 == \"$zots\" {print}"

Roger Rohrbach                                  sun!wrs!roger    roger@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "I was an infinitely hot and dense dot."                            |

wrp@PRC.Unisys.COM (William R. Pringle) (09/01/90)

In article <1209@wrs.wrs.com> roger@kymijoki.wrs.com (Roger Rohrbach) writes:
>fred@maccs.dcss.mcmaster.ca (Fred Whiteside) writes:
>
>>cd /usr/spool/news/comp/std/c
>>zots=$1
>>echo "Should ignore files with name < $zots"
>>files=`ls -rt * |grep -v '.*[a-zA-Z].*' | awk $1 '>=' $zots {print}`
>>echo $files
>
>
(Followed by several variations on how to arrange quotes so that
a shell variable can get into an AWK program.)

I'm surprised no one mentioned the simple way:

	awk '$1 >= MAX{print}' MAX=$zots -

The MAX=value sets the awk variable MAX to 'value', the dash is needed
to indicate using stdin.  You could eliminate the grep by letting awk
do it for you:

	awk '/^[0-9]*/{if ($1 >= MAX) print}' MAX=$zots -

No that we have probably confused the original poster beyond hope ...

Good luck!
Bill Pringle

The nice thing about Unix is that there are so many right answers!

mday@iconsys (Matt Day) (09/02/90)

In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov> scott@tab00.larc.nasa.gov (Scott Yelich) writes:
>As a side question, does ANYONE have any bourne shell routines which do
>math... reasonably effeciently?  (For numbers > 1000?)

For the Bourne shell, the most efficient way to do math is to use the "expr"
program.  It doesn't support floating point, but it doesn't have any trouble
with numbers larger than 1000.  If you have a shell script that needs to do
a lot of math, I suggest you use the C shell built in math operators; it's
much faster (or perl, of course).
-- 
- Matthew T. Day, Sanyo/ICON, mday@iconsys.icon.com || uunet!iconsys!mday

george@hls0.hls.oz (George Turczynski) (09/05/90)

In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov> scott@tab00.larc.nasa.gov (Scott Yelich) writes:
>As a side question, does ANYONE have any bourne shell routines which do
>math... reasonably effeciently?  (For numbers > 1000?)

I don't know whose UNIX you have but you may have a utility called
"dc", for "desk calulator".  If you have it, read the man page.  It
will handle numbers > 1000 AND numbers with decimal fractions.

for example:

|>---------Cut here----------<|

#! /bin/sh

num="10000.0"

while [ $num != "10010.0" ];
do
        num=`echo "$num 0.2 + p" | dc`

        echo $num
done

|>---------Cut here----------<|


Just another idea...

-- 
| George P. J. Turczynski.          |---------------------------------------------------- 
| Computer Systems Engineer.        | ACSnet: george@highland.oz | I can't speak for the |
| Highland Logic Pty. Ltd.          | Phone: +61 48 683490       | company, I can barely |
| Suite 1, 348-354 Argyle St        | Fax:   +61 48 683474       | speak for myself...   |
| Moss Vale. NSW. Australia. 2577   |---------------------------------------------------- 

scott@tab29.larc.nasa.gov (Scott Yelich) (09/05/90)

>In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov> scott@tab00.larc.nasa.gov (Scott Yelich) writes:
>>As a side question, does ANYONE have any bourne shell routines which do
                                           ^^^^^^ ^^^^^ ^^^^^^^^
                                           |||||| ||||| ||||||||

>>math... reasonably effeciently?  (For numbers > 1000?)
>I don't know whose UNIX you have but you may have a utility called
>"dc", for "desk calulator".  If you have it, read the man page.  It
>will handle numbers > 1000 AND numbers with decimal fractions.
>   for example:

I'D RATHER USE PERL!
(Thas'a joke, son... I could'a had'a V8! or at least a V5r4!)

Sometimes people program for the ART of it (Oh no! Did I say that?)

for instance, which do you prefer?  Which do you consider better form?

1) if [ "$1" = "$2" ]; then
     echo "Yeas!"
   else
     echo "No-way!"
   fi

- or -

2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"

What about?

3) if [ "$1" ]; then
     echo "Yup!"
   fi
 
4) eval ${1:+'echo "Yup!"'}

I prefer 2 and 4....

Now, does anyone have and BOURNE SHELL (ie: /bin/sh) routines to do math?
what about getting information like ``ls -Flagsi filename'' without calling
ls?

Please: no perl, exec, c, pascal, fortran, csh, ksh, etc.  /bin/sh

--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------

de5@de5.CTD.ORNL.GOV (Dave Sill) (09/06/90)

In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov>, scott@tab29.larc.nasa.gov (Scott Yelich) writes:
>
>2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"
>
>4) eval ${1:+'echo "Yup!"'}
>
>I prefer 2 and 4....

I hope I never have to maintain any of your scripts.  

-- 
Dave Sill (de5@ornl.gov)		These are my opinions.
Martin Marietta Energy Systems
Workstation Support

fuchs@it.uka.de (Harald Fuchs) (09/06/90)

scott@tab29.larc.nasa.gov (Scott Yelich) writes:
>2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"

>I prefer 2 and 4....

I don't. The third command is executed if the second fails, which is
surely not what I want.
--

Harald Fuchs <fuchs@it.uka.de>

eliot@dg-rtp.dg.com (Topher Eliot) (09/06/90)

In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov>,
scott@tab29.larc.nasa.gov (Scott Yelich) writes:
|> 
|> Sometimes people program for the ART of it (Oh no! Did I say that?)
|> 
|> for instance, which do you prefer?  Which do you consider better
form?
...
|> 2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"
...
|> 4) eval ${1:+'echo "Yup!"'}
|> I prefer 2 and 4....

My wife and I have for a long time had an inside joke:  "I can't
understand
it.  It must be Art."  I never thought I'd apply it to a shell
script...
--
Topher Eliot
Data General Corporation                eliot@dg-rtp.dg.com
62 T. W. Alexander Drive               
{backbone}!mcnc!rti!dg-rtp!eliot
Research Triangle Park, NC 27709        (919) 248-6371
Obviously, I speak for myself, not for DG.

chet@cwns1.CWRU.EDU (Chet Ramey) (09/06/90)

In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov> scott@tab29.larc.nasa.gov (Scott Yelich) writes:

>1) if [ "$1" = "$2" ]; then
>     echo "Yeas!"
>   else
>     echo "No-way!"
>   fi

What if $1 = '=' or '-t'?

>2) test "$1" = "$2" && echo "Yeas!" || echo "No-way!"

cwns1$ foo()
> {
> /bin/test "$1" = "$2" && echo "Yeas" || echo "No-way"
> }
cwns1$ foo -r -r
test: too many arguments
No-way

>What about?
>
>3) if [ "$1" ]; then
>     echo "Yup!"
>   fi

What if $1 = `-f' or `-r'?

cwns1$ foo()
> {
> if [ "$1" ] ; then
> echo yup
> fi
> }
cwns1$ foo a
yup
cwns1$ foo -r
[: argument expected

>4) eval ${1:+'echo "Yup!"'}

This *is* a nice way to test whether or not a variable is set.

Nobody seems to consider the case statement, which avoids these problems:

equal()
{
	case "$1" in
		"$2")	return 0
			;;
		*)	return 1
			;;
	esac
}

cwns1$ equal a b
cwns1$ echo $?
1
cwns1$ equal a a
cwns1$ echo $?
0
cwns1$ equal -r -r
cwns1$ echo $?
0

>Now, does anyone have and BOURNE SHELL (ie: /bin/sh) routines to do math?

Well, the shell running as /bin/sh on my machine is bash, and it has
$[] to do arithmetic substitution.

Chet
-- 
Chet Ramey				``Levi Stubbs' tears run down
Network Services Group			  his face...''
Case Western Reserve University	
chet@ins.CWRU.Edu		

scott@tab00.larc.nasa.gov (Scott Yelich) (09/06/90)

   |> Sometimes people program for the ART of it (Oh no! Did I say that?)
   |> for instance, which do you prefer?  Which do you consider better
   My wife and I have for a long time had an inside joke:  "I can't
   understand
   it.  It must be Art."  I never thought I'd apply it to a shell
   script...

Please read the charter [?] for this group!  

I believe programming is an art... and if you program in sh, then
that can be an art too.

ps:  re: test "" && {} || {}
     I am not worried about the second command failing.

--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------

scott@tab00.larc.nasa.gov (Scott Yelich) (09/06/90)

>cwns1$ foo -r -r
>test: too many arguments
>cwns1$ foo -r
>[: argument expected
> >4) eval ${1:+'echo "Yup!"'}

Right, if I am at all worried about a variable evr starting with a dash,
I do something as ugly as:
test "${var:+:}"

See, most ALL of my internal variables I SET and they will never be -something.
And if they ever get to be -something then the value is invalid.

>This *is* a nice way to test whether or not a variable is set.
I use it for cases when there MIGHT be a -something.

For instance, I have special routines which parse command line options.

>  Nobody seems to consider the case statement, which avoids these problems:
>	   "$2")	return 0
>	   *)	return 1

Yeah, perhaps test should just be smarter, but we can't wait for that.

>>Now, does anyone have and BOURNE SHELL (ie: /bin/sh) routines to do math?
>Well, the shell running as /bin/sh on my machine is bash, and it has
>$[] to do arithmetic substitution.

I am looking for ART styles here... not shell substitutions!  Arrraugh!

--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------

meissner@osf.org (Michael Meissner) (09/06/90)

In article <1990Sep6.141133.3391@usenet.ins.cwru.edu>
chet@cwns1.CWRU.EDU (Chet Ramey) writes:

| In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov>
| scott@tab29.larc.nasa.gov (Scott Yelich) writes:
| 
| >1) if [ "$1" = "$2" ]; then
| >     echo "Yeas!"
| >   else
| >     echo "No-way!"
| >   fi
| 
| What if $1 = '=' or '-t'?

The standard way of dealing with this is:

if [ x"$1" = x"$2" ]; then
	echo "Yeas!"
else
	echo "No-way!"
fi
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Do apple growers tell their kids money doesn't grow on bushes?

goudreau@dg-rtp.dg.com (Bob Goudreau) (09/08/90)

In article <MEISSNER.90Sep6122432@osf.osf.org>, meissner@osf.org
(Michael Meissner) writes:
> In article <1990Sep6.141133.3391@usenet.ins.cwru.edu>
> chet@cwns1.CWRU.EDU (Chet Ramey) writes:
> 
> | In article <SCOTT.90Sep5124415@tab29.larc.nasa.gov>
> | scott@tab29.larc.nasa.gov (Scott Yelich) writes:
> | 
> | >1) if [ "$1" = "$2" ]; then
> | >     echo "Yeas!"
> | >   else
> | >     echo "No-way!"
> | >   fi
> | 
> | What if $1 = '=' or '-t'?
> 
> The standard way of dealing with this is:
> 
> if [ x"$1" = x"$2" ]; then
> 	echo "Yeas!"
> else
> 	echo "No-way!"
> fi

Why is even the "x" method necessary?  My system's Bourne shell
(a port of V.3.2's sh) doesn't have any problem handling '=' or
'-t' in the example Scott Yelich describes above.  Is this something
AT&T fixed in the last couple of years?

----------------------------------------------------------------------
Bob Goudreau				+1 919 248 6231
Data General Corporation
62 Alexander Drive			goudreau@dg-rtp.dg.com
Research Triangle Park, NC  27709	...!mcnc!rti!xyzzy!goudreau
USA

goudreau@dg-rtp.dg.com (Bob Goudreau) (09/08/90)

In article <SCOTT.90Sep6120011@tab00.larc.nasa.gov>,
scott@tab00.larc.nasa.gov (Scott Yelich) writes:
> 
>    |> Sometimes people program for the ART of it (Oh no! Did I say
that?)
>    |> for instance, which do you prefer?  Which do you consider
better
>
>    My wife and I have for a long time had an inside joke:  "I can't
>    understand
>    it.  It must be Art."  I never thought I'd apply it to a shell
>    script...
> 
> Please read the charter [?] for this group!  
> 
> I believe programming is an art... and if you program in sh, then
> that can be an art too.
 
So let me get this straight:

Making shell scripts needlessly obscure (i.e., no increase in either
functionality or performance, and a downright *decrease* in
readability) is supposed to be a *good thing* ??!!

Call it "art" if you will, and feel free to start a rec.arts.shell
newsgroup.  I prefer to call it "bad software engineering".

----------------------------------------------------------------------
Bob Goudreau				+1 919 248 6231
Data General Corporation
62 Alexander Drive			goudreau@dg-rtp.dg.com
Research Triangle Park, NC  27709	...!mcnc!rti!xyzzy!goudreau
USA

meissner@osf.org (Michael Meissner) (09/10/90)

In article <1990Sep7.193010.26787@dg-rtp.dg.com>
goudreau@dg-rtp.dg.com (Bob Goudreau) writes:

| In article <MEISSNER.90Sep6122432@osf.osf.org>, meissner@osf.org
| (Michael Meissner) writes:
	...
| > The standard way of dealing with this is:
| > 
| > if [ x"$1" = x"$2" ]; then
| > 	echo "Yeas!"
| > else
| > 	echo "No-way!"
| > fi
| 
| Why is even the "x" method necessary?  My system's Bourne shell
| (a port of V.3.2's sh) doesn't have any problem handling '=' or
| '-t' in the example Scott Yelich describes above.  Is this something
| AT&T fixed in the last couple of years?

It depends on whether you are trying to write maximally portable shell
scripts.  V7-based systems required this, because test was just
another command, and not handled by the shell.

BSD can't include the system V.2 shell (or anything else) because it
would mean that current BSD users would have to get a System V.2
license (which I don't think is offered anymore).  A System V.3
license is on the order of $60,000 for commercial sites, and V.4 is
rumored to be $100,000 (I know there are provisions for lower
university prices, but I don't have any data on them).  Also, the
'recent' AT&T licenses have had things various people have objected to
in the past (and to be fair there are people who can't sign the latest
BSD licenses, and I'm sure there are people who have problems with OSF
licenses).
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Do apple growers tell their kids money doesn't grow on bushes?

roger@kymijoki.wrs.com (Roger Rohrbach) (09/11/90)

goudreau@dg-rtp.dg.com (Bob Goudreau) writes:

>So let me get this straight:
>
>Making shell scripts needlessly obscure (i.e., no increase in either
>functionality or performance, and a downright *decrease* in
>readability) is supposed to be a *good thing* ??!!
>
>Call it "art" if you will, and feel free to start a rec.arts.shell
>newsgroup.  I prefer to call it "bad software engineering".


I agree that shell programs should be readable, but "needlessly obscure"
is subjective.  On the one hand, the shell syntax (outside of the control
structures) is pretty obscure to begin with, because it relies so much
on context-sensitive treatment of single characters.  On the other hand,
the shell language per se is small enough that I don't consider certain
combinations to be obscure, even though they are not used by most casual
shell programmers; i.e., I believe it is our responsibility to learn the
language.  I alway try to write in a way that minimizes obscurity intro-
duced by the former.  Thus, I don't like to write complicated pipelines
that go in and out of single- and double-quotes to pass shell variables
and backquote expressions to sed scripts!  But I do use goal-directed
programming (program components that "succeed" or "fail") extensively,
even though people not familiar with the style (including my boss!) call
it "obscure"- I tell them to look at Prolog or Icon, or even the C `?:'
operator (my boss only knows C :-)  If a language provides a succinct
notation, I think it's good to consider the use of same "succinct", not
"obscure".  As an example, I prefer

    grep '^roger:' /etc/passwd > /dev/null ||
    {
        echo "roger is not a user" >&2
        exit 1
    }

to

    if grep '^roger:' /etc/passwd > /dev/null
    then
        :
    else
        echo "roger is not a user" >&2
        exit 1
    fi

and would consider it *more* readable, especially if it were nested within
another "if-then-else" statement or loop.

Roger Rohrbach                                  sun!wrs!roger    roger@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "I was an infinitely hot and dense dot."                            |

fred@maccs.dcss.mcmaster.ca (Fred Whiteside) (09/18/90)

hi,

	I just wanted to say thanks to all those who responded to my
question about the bourne shell.  Almost everyone gently pointed out
that i was improperly quoting the (single) argument to awk and most
tried to help me with the efficiency of the code.  Again, thanks to
one and all, including:

Sam Horrocks <sam@buckaroo.ICS.UCI.EDU>
rossc@extro.ucc.su.OZ.AU (Ross Cartlidge)
tale@turing.cs.rpi.edu (David C Lawrence)
peter@ontmoh
Peter G. Ford <pgf@space.mit.edu>
eirik@theory.TN.CORNELL.EDU (Eirik Fuller)
rtc%westford.ccur.com@RELAY.CS.NET
shirono@gcx1.SSD.CSD.HARRIS.COM (Roberto Shironoshita)
Kenneth Herron <kherron@ms.uky.edu>
Richard Frost <rfrost@spam.ua.oz.au>
Atte Kortekangas <ajk@tik.vtt.fi>
Jeff Beadles <jeff%quark.wv.tek.com@RELAY.CS.NET>
Bob English <renglish@hplabsz.hpl.hp.com>
wdavis@x102c.ess.harris.com (davis william 26373)
chawley%sundiver.esd.sgi.com@SGI.COM (Christopher J. Hawley)
uunet!atexnet!jackal (Phil Hammar)
Topher Eliot <eliot@DG-RTP.DG.COM>
kingdon@ai.mit.edu (Jim Kingdon)
cbp@icc.com (Chris Preston)
Doug Orr <doug@chorus.fr>
wd@samsa.pcs.com (wd)
bernie@DIALix.oz.au (Bernd Felsche)
Mark Brader <msb@sq.com>
contact!smarry (Marc Moorcroft)
bob@wyse.com (Bob McGowen x4312 dept208)
miken@pyrhard2.pyramid.com (Mike Newman)
jtp@ngs.fi (Jukka Partanen)

	now maybe my mailbox will stop eating up all of my quota ...

-Fred Whiteside           whitesid@darwin.CMH.McMaster.CA
 Chedoke-McMaster Hospitals   fred@maccs.DCSS.McMaster.CA
-- 
Fred Whiteside   Chedoke-McMaster Hospitals, Hamilton, Ontario, Canada
whitesid@{mcmaster.ca,darwin.cmh.mcmaster.ca,130.113.2.18}
fred@maccs.DCSS.McMaster.CA   ...!uunet!utai!utgpu!maccs!fred