Thread View: comp.unix.shell
27 messages
27 total messages
Started by Fred Whiteside
Thu, 30 Aug 1990 00:56
bourne shell query
Author: Fred Whiteside
Date: Thu, 30 Aug 1990 00:56
Date: Thu, 30 Aug 1990 00:56
31 lines
1818 bytes
1818 bytes
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
Re: bourne shell query
Author: Jukka Partanen
Date: Thu, 30 Aug 1990 12:17
Date: Thu, 30 Aug 1990 12:17
8 lines
868 bytes
868 bytes
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.
Re: bourne shell query
Author: Jukka Partanen
Date: Thu, 30 Aug 1990 12:19
Date: Thu, 30 Aug 1990 12:19
10 lines
919 bytes
919 bytes
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.
Re: bourne shell query
Author: Jeff Beadles
Date: Thu, 30 Aug 1990 14:55
Date: Thu, 30 Aug 1990 14:55
73 lines
3250 bytes
3250 bytes
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.
Re: bourne shell query
Author: Scott Yelich
Date: Thu, 30 Aug 1990 16:49
Date: Thu, 30 Aug 1990 16:49
28 lines
1955 bytes
1955 bytes
>:>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/
-----------------------------------------------------------------------------
Re: bourne shell query
Author: James Brister
Date: Thu, 30 Aug 1990 16:59
Date: Thu, 30 Aug 1990 16:59
23 lines
1335 bytes
1335 bytes
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
Re: bourne shell query
Author: Jeff Beadles
Date: Thu, 30 Aug 1990 20:34
Date: Thu, 30 Aug 1990 20:34
26 lines
1120 bytes
1120 bytes
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.
Re: bourne shell query
Author: andrew.d.hay
Date: Fri, 31 Aug 1990 12:06
Date: Fri, 31 Aug 1990 12:06
35 lines
1805 bytes
1805 bytes
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 +------------------------------------------------------+
Re: bourne shell query
Author: Dan Mercer
Date: Fri, 31 Aug 1990 19:25
Date: Fri, 31 Aug 1990 19:25
81 lines
2393 bytes
2393 bytes
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"
Re: bourne shell query
Author: Neil Smithline
Date: Fri, 31 Aug 1990 19:28
Date: Fri, 31 Aug 1990 19:28
41 lines
2177 bytes
2177 bytes
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
Re: bourne shell query
Author: Roger Rohrbach
Date: Fri, 31 Aug 1990 21:37
Date: Fri, 31 Aug 1990 21:37
55 lines
2494 bytes
2494 bytes
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." |
Re: bourne shell query
Author: William R. Pringle
Date: Sat, 01 Sep 1990 03:11
Date: Sat, 01 Sep 1990 03:11
29 lines
1335 bytes
1335 bytes
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!
Re: bourne shell query
Author: Matt Day
Date: Sat, 01 Sep 1990 23:44
Date: Sat, 01 Sep 1990 23:44
11 lines
1070 bytes
1070 bytes
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
Math routines (was bourne shell query)
Author: Jeffrey Gleixner
Date: Sun, 02 Sep 1990 14:16
Date: Sun, 02 Sep 1990 14:16
38 lines
1754 bytes
1754 bytes
In article <75@iconsys>, mday@iconsys (Matt Day) writes:
> 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"
> ... 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
If you're going to do a lot of math write a {your favorite language besides
*sh here} program to do the math for you. Even if you're doing a small
amount of math it will really speed it up.
i.e.
#!/bin/sh
count 1 6 | while read value ; do
some commands...
done
=========
/*
* Count.c
*
* print succesive numbers from start to end.
* Used in scripts instead of `expr $i +1`
*/
#include <stdio.h>
main(argc,argv)
int argc; char *argv[];
{
register int i,j;
i=atoi(argv[1]);
j=atoi(argv[2]);
while (i <= j)
printf(" %d",i++);
}
--
glex@msc.umn.edu === " I don't go out with girls that say bitch'n " ===
Re: Math routines (was bourne shell query)
Author: Scott Yelich
Date: Sun, 02 Sep 1990 18:14
Date: Sun, 02 Sep 1990 18:14
73 lines
2367 bytes
2367 bytes
>#include <stdio.h>
>int argc; char *argv[];
>while (i <= j)
> printf(" %d",i++);
Hmmm.. that's the STRANGEST sh program I have ever seen...
It must be that CSH that I keep hearing about! :-)
This next routine isn't supposed to be sophisticated... but it works--
just give it two SMALL NUMBERS.
ADDITION ()
{
NUM1="$1"
NUM2="$2"
shift $#
while test $# -lt $NUM1
do
set - x $@
done
NUM1="$@"
shift $#
while test $# -lt $NUM2
do
set - x $@
done
NUM2="$@"
set - $NUM1 $NUM2
TOTAL=$#
}
I am working on a sort of LIBRARY of SHroutines where all I have to do is
include the clode ``. COUNT'' for the next... and use the code.
COUNT ()
{
test $# = 0 &&\
{
echo "$0: Usage: $0 value [...]"
exit 1
} ||\
{
for i
{
set ""
while :
do
set $* " x"
echo $#
if test $# = $i; then
break
fi
done
}
}
}
Of course, ``COUNT x'' is a great counter! :-)
The main problem with the two loops is the ``set " $*"'' command.
This is what takes so much time when the argument list grows.
--
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/
-----------------------------------------------------------------------------
"Modern" sh (was Math routines)
Author: Brian Glendenning
Date: Sun, 02 Sep 1990 21:24
Date: Sun, 02 Sep 1990 21:24
16 lines
1237 bytes
1237 bytes
All this reminds me of questions I've been meaning to ask:
1) Are there any versions of sh that don't understand # comments any
more?
2) How common are sh's that don't understand shell functions?
I'm particularly interested in the answer for machines that scientists
are apt to buy and use (so that old PDP-11 in your closet doesn't
count!)
Brian
--
Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu bglenden@nrao.bitnet (804) 296-0286
Re: "Modern" sh (was Math routines)
Author: Michael Meissner
Date: Sun, 02 Sep 1990 23:46
Date: Sun, 02 Sep 1990 23:46
36 lines
2323 bytes
2323 bytes
In article <BGLENDEN.90Sep2172441@mandrill.cv.nrao.edu> bglenden@mandrill.cv.nrao.edu (Brian Glendenning) writes: | All this reminds me of questions I've been meaning to ask: | | 1) Are there any versions of sh that don't understand # comments any | more? It depends on when the last time you installed the system. If it was 1979 or such, then your shell probably doesn't understand # comments (I don't remember whether it was in the v7 shell, or not). But seriously, there are probably people running ancient systems out there that are stuck because their vendor went belly up years ago (of course you could probably buy a 386 running UNIX nowadays for a fraction of the original cost, but if you don't have the $5-10K at the present time, it doesn't matter). | 2) How common are sh's that don't understand shell functions? Anything that is BSD based without adding from System V. For example, the Ultrix DECstation that I'm posting from has the musty BSD shell as /bin/sh and the System V.2 shell with shell functions as /bin/sh5. Another problem with the BSD shell Ultrix uses, is that test (aka '[') is a separate command that you have to fork/exec to get to -- I suspect echo is too..... | I'm particularly interested in the answer for machines that scientists | are apt to buy and use (so that old PDP-11 in your closet doesn't | count!) You could always recomend bash and/or ksh. -- 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?
Re: Math routines (was bourne shell query)
Author: richard.a.stewart
Date: Mon, 03 Sep 1990 22:36
Date: Mon, 03 Sep 1990 22:36
24 lines
1751 bytes
1751 bytes
In article <2494@uc.msc.umn.edu>, glex@uh.msc.umn.edu (Jeffrey Gleixner) writes:
> > In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov> writes:
> > >As a side question, does ANYONE have any bourne shell routines which do
> > >math... reasonably effeciently? (For numbers > 1000?)
> If you're going to do a lot of math write a {your favorite language besides
> *sh here} program to do the math for you. Even if you're doing a small
> amount of math it will really speed it up.
Generally concur except for prior suggestion re "expr".
-----
Not my favorite language(:-) but one might also consider
a (Bourne) shell counting function using dc:
cnt() { echo "[p1+d${2}!<0]s0${1}l0xq"|dc; }
This counts at a rate of about 100/second (on a 3B2) which may be adequate
for many shell programming purposes although MUCH slower than "count".
The function saves 99.6+% in "diskspace" over a private a.out from count.c;
"count" of course might well be shared by all users as /usr/lbin/count.
-----
Other possibilities for consideration could include bc, awk, awkcc,
various public domain calculators and "Hoc" (SEE Appendices 2&3 of
"The UNIX Programming Environment" by Kernighan & Pike)
Re: bourne shell query
Author: George Turczynski
Date: Wed, 05 Sep 1990 06:00
Date: Wed, 05 Sep 1990 06:00
34 lines
1605 bytes
1605 bytes
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 |----------------------------------------------------
Re: bourne shell query
Author: Scott Yelich
Date: Wed, 05 Sep 1990 16:44
Date: Wed, 05 Sep 1990 16:44
53 lines
2438 bytes
2438 bytes
>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/
-----------------------------------------------------------------------------
Re: bourne shell query
Author: Dave Sill
Date: Wed, 05 Sep 1990 17:24
Date: Wed, 05 Sep 1990 17:24
14 lines
948 bytes
948 bytes
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
Re: bourne shell query
Author: Harald Fuchs
Date: Thu, 06 Sep 1990 11:11
Date: Thu, 06 Sep 1990 11:11
10 lines
795 bytes
795 bytes
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>
Re: bourne shell query
Author: Topher Eliot
Date: Thu, 06 Sep 1990 14:08
Date: Thu, 06 Sep 1990 14:08
24 lines
1355 bytes
1355 bytes
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.
Re: bourne shell query
Author: Scott Yelich
Date: Thu, 06 Sep 1990 16:00
Date: Thu, 06 Sep 1990 16:00
25 lines
1651 bytes
1651 bytes
|> 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/
-----------------------------------------------------------------------------
Re: bourne shell query
Author: Bob Goudreau
Date: Fri, 07 Sep 1990 19:43
Date: Fri, 07 Sep 1990 19:43
33 lines
1833 bytes
1833 bytes
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
Re: bourne shell query
Author: Roger Rohrbach
Date: Mon, 10 Sep 1990 19:19
Date: Mon, 10 Sep 1990 19:19
53 lines
2813 bytes
2813 bytes
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." |
Re: bourne shell query
Author: Fred Whiteside
Date: Tue, 18 Sep 1990 11:58
Date: Tue, 18 Sep 1990 11:58
44 lines
2128 bytes
2128 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads