home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!lanl!beta.lanl.gov!hall
- From: hall@beta.lanl.gov (Michael L. Hall)
- Subject: Escaping brackets in csh - SUMMARY
- Message-ID: <1992Aug12.234645.2589@newshost.lanl.gov>
- Sender: news@newshost.lanl.gov
- Organization: Los Alamos National Laboratory
- Date: Wed, 12 Aug 1992 23:46:45 GMT
- Lines: 79
-
- I posted a question yesterday about renaming some VMS files that
- had the [dir1.dir2.dir3]filename.ext format into a unix format
- which retained the directory structure. I was wanting to do it
- with a foreach (csh) or for (sh) command, but was having trouble
- escaping (or quoting) the brackets ([]). The best (or at least
- easiest for me to implement) answer was from Arne H. Juul -
- <arnej@lise.unit.no>:
-
- ------------------------------------
- This is my csh solution - 'do it with sh'!
-
- csh% ls
- [asd.efg]gruff [asd.hij]spiff [asd.xcv]qwerty [bfd.zxc]foo [jkl.bnm]bar
- csh%
- csh% cat > rename.sh
- #!/bin/sh
-
- /bin/ls \[* | while read filename; do
- dir=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . /`
- dirs=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . ' '`
- file=`echo $filename | sed 's/^\[.*\]//'`
- set $dirs
- ( for x do mkdir $x 2>/dev/null; cd $x; done )
- echo making $dir/$file
- cp $filename $dir/$file
- done
- csh% sh rename.sh
- csh% ls
- asd/ bfd/ jkl/
- csh%
- csh% find . -print
- .
- ./asd
- ./asd/efg
- ./asd/efg/gruff
- ./asd/hij
- ./asd/hij/spiff
- ./asd/xcv
- ./asd/xcv/qwerty
- ./bfd
- ./bfd/zxc
- ./bfd/zxc/foo
- ./jkl
- ./jkl/bnm
- ./jkl/bnm/bar
-
-
- There is a reason why csh isn't very well-liked in comp.unix.shell - and
- this is an example of the reason. Or to put it another way: I tried
- to make csh's foreach do it, but no way.
-
- You could do the above in sh with
- for filename in \[* ; do
- instead of the while, but the "ls | while read" is nicer.
- You could also do mv instead of cp in the shell-script above, of course.
- ---------------------------
-
- Icarus Sparry gave me a similar answer, but I used the first one
- because it was more easily deciphered. Many others suggested that
- I use the bourne shell, as brackets and fors mix much better than
- brackets and foreaches.
-
- I am continually amazed at how people out in netland are willing to
- help you solve your problems.
-
- Hearty thanks to:
-
- Arne Henrik Juul <arnej@lise.unit.no>
- Icarus Sparry <I.Sparry@sunlab1.bath.ac.uk>
- Watanabe Maki <maki@jit.dec.com>
- Georg Wittig <Georg.Wittig@gmd.de>
- Leo <leo@ine.philips.nl>
- Tom Christiansen <tchrist@pixel.convex.com>
-
- --
- +----------------------------------------------------------------------+
- | Michael L. Hall There are times, sir, when men of good conscience |
- | hall@lanl.gov cannot blindly follow orders. - Jean-Luc Picard |
- +----------------------------------------------------------------------+
-