home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!glaze
- From: glaze@cs.mu.oz.au (Glaze)
- Subject: Re: Escaping brackets in csh - SUMMARY
- Message-ID: <glaze.093@glaze>
- Sender: news@cs.mu.oz.au
- Organization: Computer Science, University of Melbourne, Australia
- References: <1992Aug12.234645.2589@newshost.lanl.gov>
- Date: Thu, 13 Aug 1992 23:38:13 GMT
- Lines: 47
-
- hall@beta.lanl.gov (Michael L. Hall) writes:
- >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>:
-
- [...delete area...]
- >This is my csh solution - 'do it with 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
-
- One thing you may need to do is change the ``mkdir'' to ``mkdir -p'' above.
- The -p option requesting that mkdir create any missing parent directories.
- However, older mkdir's don't support it, so here's a small script I
- whipped up for you:
-
- #! /bin/sh
- # simulate a "mkdir -p"
- parent()
- {
- if test \! -d $1 ; then
- parent `dirname $1`
- mkdir `echo $1 | sed 's@/$@@'`
- fi
- }
- usage()
- {
- echo "mkdirp: Usage: mkdirp dirname"
- exit 1
- }
- if test $# -ne 1 ; then
- usage
- else
- parent $1
- fi
- exit 0
-