On Sun, 16 Mar 2003, Cowles, Steve wrote:
> > -----Original Message-----
> > From: Kleiner Hampel
> > Sent: Sunday, March 16, 2003 10:41 AM
> > Subject: Re: shell script - expert question
> >
> >
> > Hi,
> >
> > now it works, but because of the '*'.
> >
> > now i want to remove the leading abc from all files in my directory.
> > i tried this:
> >
> > for i in *; do mv $i `echo $i | sed s/abc//`; done
> >
> > but it doesn't do that.
> > i always get the error, that the last arguement must be a directory!
> > I guess, the reason are the white spaces in the names.
> > perhaps the expression `echo $i | sed s/abc//` also have to
> > be set in '' or so, but it doesn't work this way.
> >
> > please help
>
> Single quotes ' are treated literally by the shell interpreter. i.e. no
> filename expansion. With double quotes, your variables are expanded prior to
> being used. So...
>
> for i in * ; do
> mv "$i" `echo "$i" | sed -e 's/abc//'`
> done
>
> Note: Your example has not dealt with filenames that do NOT contain spaces.
rather than going to the trouble of firing up "sed", you can use
the pattern-matching operators built into the shell:
${var#string} # discard shortest prefix matching "string"
as in,
${i#abc}
will return the string corresponding to the contents of $i,
with a prefix of "abc" deleted. if the prefix isn't "abc",
no change.
check out:
${var#string}
${var##string}
${var%string}
${var%%string
rday
p.s. just to clarify, you would do:
mv "$i" "${i#abc}"
p.p.s. note that "string" in all of the above can be a
wildcard pattern as well, making it really powerful.
--
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list