问题
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
回答1:
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.
回答2:
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 "*\!{^}*"'
来源:https://stackoverflow.com/questions/13187987/aliasing-find-directory-in-unix