zsh-users
 help / color / mirror / code / Atom feed
* Script breaking on cd *param
@ 2006-12-06 12:11 zzapper
  2006-12-06 12:26 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: zzapper @ 2006-12-06 12:11 UTC (permalink / raw)
  To: zsh-users

Hi
The following should try to cd to a subdirectory in the 4 following ways.

cd param
cd param*
cd *param
cd *param*

However if it fails on the second the script breaks
with "no matches found: *param "

sometimes putting quotes round ${1}* seems to help

alias if_param='
   if [ $# -gt 0 ]
      then
      if builtin cd $1 &> /dev/null ;
         then
         shift
      elif builtin cd ${1}* &> /dev/null;
         then
         shift
      elif builtin cd *$1 &> /dev/null;
         then
         shift
      elif builtin cd *${1}* &> /dev/null;
         then
         shift
      else
         echo $1 not found
         shift
      fi
   fi
'

-- 
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips


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

* Re: Script breaking on cd *param
  2006-12-06 12:11 Script breaking on cd *param zzapper
@ 2006-12-06 12:26 ` Peter Stephenson
  2006-12-06 13:42   ` zzapper
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2006-12-06 12:26 UTC (permalink / raw)
  To: zsh-users

zzapper wrote:
> Hi
> The following should try to cd to a subdirectory in the 4 following ways.
> 
> cd param
> cd param*
> cd *param
> cd *param*
> 
> However if it fails on the second the script breaks
> with "no matches found: *param "

You are likely to get some sort of error, possibly from cd, possibly
highly unexpected (try it if param* expands to two files, or not a
directory, for example) unless you actually test the expansion is a
single directory.  So you really need to be more careful.

local str
local -a dirs
for str in param param\* \*param \*param\*; do
  # Expands to all matching directories, else nothing.
  dirs=(${~str}(N/))
  if (( ${#dirs} == 1 )); then
    cd $dirs
    break
  fi
done

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: Script breaking on cd *param
  2006-12-06 12:26 ` Peter Stephenson
@ 2006-12-06 13:42   ` zzapper
  2006-12-06 13:52     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: zzapper @ 2006-12-06 13:42 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson <pws@csr.com> wrote in news:200612061226.kB6CQnpu022275
@news01.csr.com:

> local str
> local -a dirs
> for str in param param\* \*param \*param\*; do
>   # Expands to all matching directories, else nothing.
>   dirs=(${~str}(N/))
>   if (( ${#dirs} == 1 )); then
>     cd $dirs
>     break
>   fi
> done
PWS
Thanx JWTDO

but did you forget the dollars on $param?

function cdc()
{
local str
local param=$1
local -a dirs
echo $param
for str in $param $param\* \*$param \*$param\*; do
  # Expands to all matching directories, else nothing.
     dirs=(${~str}(N/))
       if (( ${#dirs} == 1 )); then
           cd $dirs
               break
                 fi
                 done
  
}


-- 
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips


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

* Re: Script breaking on cd *param
  2006-12-06 13:42   ` zzapper
@ 2006-12-06 13:52     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2006-12-06 13:52 UTC (permalink / raw)
  To: zsh-users

zzapper wrote:
> but did you forget the dollars on $param?

I was translating

cd param
cd param*
cd *param
cd *param*

literally, but obviously you can replace where it says "param" with
anything you like.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

end of thread, other threads:[~2006-12-06 13:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-06 12:11 Script breaking on cd *param zzapper
2006-12-06 12:26 ` Peter Stephenson
2006-12-06 13:42   ` zzapper
2006-12-06 13:52     ` 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).