3

I tried something like:

alias fdi 'find . -type d -iname "*\!^*"' 

but this only looks for exact names passed as argument.

fdi abc will only output:

./d/abc

not this:

./d/greabcyup

I am not just looking for exact name. It should also show ./d/greabcyup

Update: I did

echo $0

tcsh

Lii
  • 11,553
  • 8
  • 64
  • 88
user1793023
  • 117
  • 1
  • 2
  • 6
  • As an aside, `csh` is filled with pitfalls. See http://www.shlomifish.org/open-source/anti/csh/ for more details. – Brian Cain Nov 02 '12 at 02:03

2 Answers2

2

Is this c-shell or tcsh?

I dbl-checked with orielly lunix in a nutshell , !^ is meant to be the first word in the current command line, so I don't think it is doing what you want. You can dbl check that theory for yourself, with echo abc def !^ from cmd-line. (I don't have a csh handy).

But it's clear that when you create the alias, it is not getting the first word (alias) OR the first word of the alias (find) embedded in the alias. It's probably about the order of evaluation for csh.

In general any alias in most shells (not just csh) can't take arguments. It will append whatever was included on the invocation at the end. So your alias expands, with the abc argument as

find . -type d -iname abc

And the results your getting, support that. (You might see something helpful by turning on the csh debugging, by changing your top hash-bang line to #!/bin/csh -vx )

This is why other shells have functions,

 function fdi() {
     find . -type d -iname "$@"
 }

If you can't use another shell because of policy, (bash,ksh,zsh are all powerful programming languages), and you're going to use csh regularly, be sure to pickup a copy of the 'The Unix C shell Field Guide, Gail & Paul Anderson. A really exceptionally well written book.

IHTH.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • Thanks... I have other shells but i have personalized current shell so for now i am using perl script and aliasing it. works out. – user1793023 Nov 02 '12 at 22:34
1

The matching pattern in your alias "*\!^*" is not being interpreted as you expect. The character sequence \!^* is a valid shell history substitution. In other words, the shell is consuming the last asterisk as part of the shell history substitution and not passing it to the "find" command. When you run "fdi abc" what is being executed is

find . -type d -iname "*abc"

The last * is gone. You can confirm that by running "fdi abcyup" and that should return

./d/greabcyup

You might fix it by adding another asterisk to your alias definition

alias fdi 'find . -type d -iname "*\!^**"' 

The problem with that is that if you supply multiple args to your alias the history substitution would consume all of them. That might be what you want if you're looking for directories with spaces in their names. But more correctly would be to isolate the history substitution from the asterisk like so:

alias fdi 'find . -type d -iname "*\!{^}*"'
dado
  • 64
  • 5