zsh-users
 help / color / mirror / code / Atom feed
* Ex-bash script for optimisation
@ 2005-03-10 16:43 zzapper
  2005-03-10 18:15 ` Peter Miller
  0 siblings, 1 reply; 8+ messages in thread
From: zzapper @ 2005-03-10 16:43 UTC (permalink / raw)
  To: zsh-users

Hi
Q1) Is there a better way to generate the array filelst
Q2) the line "for x in *$1*" fails is no match, how can i "catch" this

#!/bin/zsh
# v
# description : vi all files containing $1 in name
#               but excluding non-text files
set +x

filebad=''
filelst=()
filenum='1'
for x in *$1*
do
   if [[ "$x" == *.(aux|toc|dvi|aux|exe|obj|zip|pdf|mdb|xls|bak|swp|log|jpg|gif|tiff|jpeg|bmp) ]]
   then
      let filebad=1
   else 
      if [ -f $x ]
      then
      filelst[$filenum]=$x
      let filenum=filenum+1
      fi
   fi
done

if (( ${#filelst} ))
then
echo "file list : " $filelst
gvim.exe ${filelst[*]} &
else
echo "sorry no file matched *$1*"
fi



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-10 16:43 Ex-bash script for optimisation zzapper
@ 2005-03-10 18:15 ` Peter Miller
  2005-03-11  4:48   ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Miller @ 2005-03-10 18:15 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users



zzapper wrote:
> Hi
> Q1) Is there a better way to generate the array filelst
> Q2) the line "for x in *$1*" fails is no match, how can i "catch" this

I use "for x in *$1*(N)" to get null glob semantics (remove pattern with no
matches).

I don't know if it is better, but you can use something like

filelst[$(($#filelst+1))]=$x



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-10 18:15 ` Peter Miller
@ 2005-03-11  4:48   ` Bart Schaefer
  2005-03-11  9:04     ` zzapper
  2005-03-13 18:46     ` zzapper
  0 siblings, 2 replies; 8+ messages in thread
From: Bart Schaefer @ 2005-03-11  4:48 UTC (permalink / raw)
  To: zsh-users

Comments on script style:

None of the variables is declared (with "declare" or "local") so this is
probably not suitable for use as an autoloaded shell function.

"filebad" is set but not used, and there's no reason to use "let" syntax
if you're not doing arithmetic (in the assignment of 1 to filebad).

On Mar 10,  4:43pm, zzapper wrote:
} Subject: Ex-bash script for optimisation
}
} Q1) Is there a better way to generate the array filelst

Sure.  You don't need the loop, just do a glob with an extended pattern.

#--- snip ---
setopt local_options extended_glob null_glob
filelst=( *$1*~*.(aux|toc|dvi|aux|exe|obj|zip|pdf|mdb|xls|bak|swp|log|jpg|gif|tiff|jpeg|bmp) )
#--- snip ---

If you want to allow $1 to be a pattern rather than a fixed string, you
need *${~1}* instead.

If you want to allow multiple arguments to the script, you need *${^*}*
or for multiple patterns *${^~*}*

In the event that you really need to loop, zsh 4.2+ supports array append
with the syntax:	filelst+=($x)

} Q2) the line "for x in *$1*" fails is no match, how can i "catch" this

Peter Miller's *$1*(N) suggestion is equivalent to the "null_glob"
setting in my example above.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-11  4:48   ` Bart Schaefer
@ 2005-03-11  9:04     ` zzapper
  2005-03-13 18:46     ` zzapper
  1 sibling, 0 replies; 8+ messages in thread
From: zzapper @ 2005-03-11  9:04 UTC (permalink / raw)
  To: zsh-users

On Fri, 11 Mar 2005 04:48:46 +0000,  wrote:

>Comments on script style:
>
>None of the variables is declared (with "declare" or "local") so this is
>probably not suitable for use as an autoloaded shell function.
>
>"filebad" is set but not used, and there's no reason to use "let" syntax
>if you're not doing arithmetic (in the assignment of 1 to filebad).
>
>On Mar 10,  4:43pm, zzapper wrote:
>} Subject: Ex-bash script for optimisation
>}
>} Q1) Is there a better way to generate the array filelst
>
>Sure.  You don't need the loop, just do a glob with an extended pattern.
>
>#--- snip ---
>setopt local_options extended_glob null_glob
>filelst=( *$1*~*.(aux|toc|dvi|aux|exe|obj|zip|pdf|mdb|xls|bak|swp|log|jpg|gif|tiff|jpeg|bmp) )
>#--- snip ---
>
>If you want to allow $1 to be a pattern rather than a fixed string, you
>need *${~1}* instead.
>
>If you want to allow multiple arguments to the script, you need *${^*}*
>or for multiple patterns *${^~*}*
>
>In the event that you really need to loop, zsh 4.2+ supports array append
>with the syntax:	filelst+=($x)
>
>} Q2) the line "for x in *$1*" fails is no match, how can i "catch" this
>
>Peter Miller's *$1*(N) suggestion is equivalent to the "null_glob"
>setting in my example above.
Oh gosh!



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-11  4:48   ` Bart Schaefer
  2005-03-11  9:04     ` zzapper
@ 2005-03-13 18:46     ` zzapper
  2005-03-13 19:23       ` Bart Schaefer
  1 sibling, 1 reply; 8+ messages in thread
From: zzapper @ 2005-03-13 18:46 UTC (permalink / raw)
  To: zsh-users

On Fri, 11 Mar 2005 04:48:46 +0000,  wrote:


>
>#--- snip ---
>setopt local_options extended_glob null_glob
>filelst=( *$1*~*.(aux|toc|dvi|aux|exe|obj|zip|pdf|mdb|xls|bak|swp|log|jpg|gif|tiff|jpeg|bmp) )
>#--- snip ---
>
>If you want to allow $1 to be a pattern rather than a fixed string, you
>need *${~1}* instead.
>
>If you want to allow multiple arguments to the script, you need *${^*}*
>or for multiple patterns *${^~*}*
>
Bart
Only the first example worked for me, but I found $1 could be a pattern, and i could set a second
parameter w/o problem

setopt local_options extended_glob null_glob
filelst=( *$1*~*.(aux|toc|dvi|aux|exe|obj|zip|pdf|mdb|xls|bak|swp|log|jpg|gif|tiff|jpeg|bmp) )

if (( ${#filelst} ))
then
echo "file list : " $filelst
gvim.exe ${filelst[*]} &
else
echo "Sorry no file matched *$1*"
fi


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-13 18:46     ` zzapper
@ 2005-03-13 19:23       ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2005-03-13 19:23 UTC (permalink / raw)
  To: zsh-users

On Mar 13,  6:46pm, zzapper wrote:
} Subject: Re: Ex-bash script for optimisation
}
} Only the first example worked for me, but I found $1 could be a pattern

I think you're misunderstanding what happens.

I don't know what the name of your script/function is, so I'll "vit" for
purposes of example (short for "vim text").  Suppose there is a directory
containing the files

	file.aux	file.exe	file.gif	file.txt

(1) If you give the command

	vit file*

then zsh will first expand the pattern to

	vit file.aux file.exe file.gif file.txt

and next call the function "vit" which will do (shortened for simplicity)

	filelst=( *file.aux*~*.(aux|gif|exe) )

and will eventually print "Sorry no file matched *file.aux*".  Note that
vit did nothing with file.exe file.gif file.txt, which are $2 $3 $4.

(2) If instead you tried

	vit f*t

the result is

	vit file.txt

and thus

	filelst=( *file.txt*~*.(aux|gif|exe) )

(3) If you quote the pattern

	vit 'file*'

then vit does

	filelst=( *file\**~*.(aux|gif|exe) )

and you get "Sorry no file matched *file**" which might be confusing.

What I meant about allowing $1 to be a pattern is, with ${~1} in the
filelst assignment expression you can pass the pattern quoted as in (3),
and internally to vit the pattern is expanded and you'd find file.txt
as desired.

What I meant about multiple arguments to the script is, with ${^*} in
the filelst assignment, example (1) becomes

	filelst=( *file.aux*~*.(aux|gif|exe)
		  *file.exe*~*.(aux|gif|exe)
		  *file.gif*~*.(aux|gif|exe)
		  *file.txt*~*.(aux|gif|exe) )

which again finds file.txt as desired.

I'm hoping you can extrapolate from there to what I meant by "multiple
patterns".

} and i could set a second parameter w/o problem

I'm afraid I don't understand what you mean by that.  "Set" how?  And
is anything useful done with the "second parameter"?  If so, what?  Or
is it just silently ignored?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
  2005-03-10 18:42 Meino Christian Cramer
@ 2005-03-11  5:14 ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2005-03-11  5:14 UTC (permalink / raw)
  To: zsh-users

On Mar 10,  7:42pm, Meino Christian Cramer wrote:
} Subject: Re: Ex-bash script for optimisation

Please start a new message with a new subject if you're asking a new
question, rather than replying to an unrelated discussion re-using a
misleading subject.  Thank you.

}  I want to limit the expand function (that one called when pressing
}  TAB after entering for example "cd")

It's important to know whether you really mean "expansion" or whether
(as I suspect) you're asking about "completion".

"Expansion" means you have something like a glob pattern, for example
a*e, and you want the shell to replace it with file names that match
the pattern, for example ankle apple argue.  In general, expansion is
not context-sensitive.

"Completion" means you've typed part of a word -- usually a prefix --
and you want the shell to supply the rest of the word in a way that
fits the context where the word appears.  For example, directory names
in the arguments of cd.

}  to file of the current working directory or in other words I want to
}  switch off the global search for expansion possibilities...

This question is a bit mystifying because there normally is no "global
search."  One possibility is that you need to "unset cdpath", but it
would help if you explained more about (1) whether you use "compinit"
to set up completion or are using the built-in defaults, and (2) what
happens when you press TAB that makes you think a "global" search is
being done.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ex-bash script for optimisation
@ 2005-03-10 18:42 Meino Christian Cramer
  2005-03-11  5:14 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Meino Christian Cramer @ 2005-03-10 18:42 UTC (permalink / raw)
  To: zsh-users

Hi,

 I checked the zshall manpage, but I fear to have overlocked the
 specific line which explains, for what I am using for.

 I want to limit the expand function (that one called when pressing
 TAB after entering for example "cd") to file of the current working
 directory or in other words I want to switch off the global search for
 expansion possibilities...

 How can I achieve this?

 Thank you very much for any help and hints in advance ! :)
 Keep zsh'ing! ;)
 Meino


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-03-13 19:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-10 16:43 Ex-bash script for optimisation zzapper
2005-03-10 18:15 ` Peter Miller
2005-03-11  4:48   ` Bart Schaefer
2005-03-11  9:04     ` zzapper
2005-03-13 18:46     ` zzapper
2005-03-13 19:23       ` Bart Schaefer
2005-03-10 18:42 Meino Christian Cramer
2005-03-11  5:14 ` Bart Schaefer

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).