Duelling shells (Re: shell architecture (to glob or not to glob))

Kris Stephens [Hail Eris!] krs at uts.amdahl.com
Wed Mar 20 05:37:16 AEST 1991


In article <+13AGZ8 at xds13.ferranti.com> peter at ficc.ferranti.com (Peter da Silva) writes:
>In article <669304840 at juliet.cs.duke.edu> drh at duke.cs.duke.edu (D. Richard Hipp) writes:
>> In article <1407 at sheol.UUCP> throopw at sheol.UUCP (Wayne Throop) writes:
>> >...[R]enaming groups of files is a common
>> >that *some* tool (whather mv or not) ought to be around to
>> >handle it.
>
>> ls *.x | sed -e 's/\.x//' | while read name; do mv $name.x $name.y; done;
>
>ls *.x | sed 's/\(.*\).x/mv & \1.y/' | sh

This needed to be anchored and to have the real dot escaped:

	ls *.x | sed 's/\(.*\)\.x$/mv & \1.y/' | sh
	                      ^  ^

to avoid errors on, say     junk.x11.x      which would otherwise create
the command      mv junk.x junk.y11.x    .

Note that in both of the cases above, any "hidden" file ending .x won't
be found and moved (renamed), and any directory with a name ending '.x'
will produce extremely odd results (needs the    -d    flag added and
a filter for directories with names ending .x).

--- start ksh version ---
:
# use in ksh only
#
# This script moves all *plain* files ending with ".$1" to the ending ".$2".
#
# The test-filter can be modified so that just directories are filtered
# or just directories and pipes or whatever, but for now, if a file isn't
# a plain file, we don't rename it.
#
# Quoting of the arguments inside the loop is important in case there
# are shell meta-characters in any of the filenames.
#

# (Insert all your argument testing stuff here so the following
# two lines hold correct info, without the dot)
from_suffix=$1
to_suffix=$2

for file in ./*.$from_suffix ./.*.$from_suffix
do
	[ ! -f "$file" ] && continue	# skip this other-than-plain file
	mv "$file" "${file%.$from_suffix}.$to_suffix"
done
--- end ksh version ---

...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs at uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]



More information about the Comp.unix.shell mailing list