>我在本主题中看到,如果要使用参数,可以在 shell 登录脚本中添加函数而不是别名。但是,我将以下代码放在带有别名的部分中的 .cshrc 文件中:
function gf()
{
grep -n $1 `find .` | grep -v "can't open"
}
但是当我键入源.cshrc时,我收到错误消息:放置不当()。C 外壳的语法与 Bash 外壳的语法不同吗?如果是这样,正确的语法是什么?
不幸的是,你不能在csh
中定义函数,就像在大多数其他 shell 中一样。此功能在csh
中不存在。
唯一的选择是创建一个脚本并将其放在PATH
的目录中,例如 ~/bin
.
如果您正在寻找只传递一个参数,只需!$
您希望参数所在的位置即可。
例如:
如果你把alias mygrep grep -i !$ myfile.txt
放在~/.cshrc
,并运行mygrep mykeyword
,csh
将运行alias mygrep grep -i mykeyword myfile.txt
。
有关详细信息,请参阅此处。
这是我的解决方案:
#!/bin/csh
if ("$1" == "run") goto $2
echo "Now in default mode"
echo "Calling myself"
csh -f dummy run sub1
csh -f dummy run sub2
exit
sub1:
echo "In sub1"
exit
sub2:
echo "In sub2"
exit
在.cshrc
或.tcshrc
中设置以下内容:
alias function '( set argv = ( !* ) ; source ~/.cshfuncs )'
在.cshfuncs
中定义函数:
# Functions for C Shell.
switch ("$1")
case "myfunc":
grep -n "$2" "`find .`" | grep -v "can't open"
exit
case "myfunc2":
echo "Another function."
exit
default:
echo "Function $1 not set."
exit -1
endsw