* Problems with for loops and : modifiers
@ 2004-05-26 8:55 pleb
2004-05-26 9:22 ` Peter Stephenson
0 siblings, 1 reply; 2+ messages in thread
From: pleb @ 2004-05-26 8:55 UTC (permalink / raw)
To: zsh-users
I'm having problems doing something that should be simple. I want to iterate over a set of files in a directory and run some commands on those files. In the command I would like to strip the suffix of a filename away (ie basename) and add a new suffix.
I can do this interactively in zsh, using several of the different builtin "for" commands, but not in the following shell script.
Q:
1. Why do the "for" loops work interactively and get a syntax error in a script? To me a newline terminator is a newline terminator.
2. Maybe I have the sense of SHORT_LOOPS backward?
3. Why does the :r modifier work interactively and not in a script?
4. What are the suggested ways to do what I want to do?
zsh script follows.
Thanks in advance.
leb
#/bin/zsh
echo "one"
#files get echoed but :r is broken
for i in *.h; { echo $i:r ; }
echo "two"
#file get echoed, but :r is broken
for i in *.h
do
echo ${i}:r
done
echo "three"
# works perfectly when run interactively but gets an error in a script
#for i (*.h) echo $i:r
echo "four"
# works perfectly when run interactively but gets an error in a script
#foreach i ( *.h)
#echo $i:r
#end
#
--
Lawrence E. Bakst
pleb@iridescent.org
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Problems with for loops and : modifiers
2004-05-26 8:55 Problems with for loops and : modifiers pleb
@ 2004-05-26 9:22 ` Peter Stephenson
0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2004-05-26 9:22 UTC (permalink / raw)
To: zsh-users
pleb@iridescent.org wrote:
> I'm having problems doing something that should be simple. I want to iterate
> over a set of files in a directory and run some commands on those files. In t
> he command I would like to strip the suffix of a filename away (ie basename)
> and add a new suffix.
>
> I can do this interactively in zsh, using several of the different builtin "f
> or" commands, but not in the following shell script.
>
> Q:
>
> 1. Why do the "for" loops work interactively and get a syntax error in a scri
> pt? To me a newline terminator is a newline terminator.
>
> 2. Maybe I have the sense of SHORT_LOOPS backward?
>
> 3. Why does the :r modifier work interactively and not in a script?
>
> 4. What are the suggested ways to do what I want to do?
It's possible you have different options set in .zshrc or /etc/zshrc.
Those won't be used in a script, only .zshenv or /etc/zshenv. (I tend
to prefer to write scripts beginning `emulate zsh', which put the shell
into a known mode, and set any necessary options explicitly.)
> echo "two"
> #file get echoed, but :r is broken
> for i in *.h
> do
> echo ${i}:r
> done
Change the substitution to ${i:r}. The ${...} insulates the parameter
substitution from other stuff around, so :r is not active.
You say $i:r is broken in the other example: the only thing I can find
that turns off modifiers is the ksh_arrays option. Try adding `setopt
no_ksh_arrays' at the start of the script. If that works, something in
/etc/zshenv or ~/.zshenv is setting it.
I'd recommend sticking with the form of `for' shown above in a script,
since it's standard and unambiguous.
--
Peter Stephenson <pws@csr.com> Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK Tel: +44 (0)1223 692070
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.
www.mimesweeper.com
**********************************************************************
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-05-26 9:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-26 8:55 Problems with for loops and : modifiers pleb
2004-05-26 9:22 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).