16

I saw in this topic that you can add a function in the shell login script instead of an alias if you want to use parameters. However, I placed the following code inside my .cshrc file in the section with aliasses:

function gf()
{
    grep -n $1 `find .` | grep -v "can't open"
}

But when I type source .cshrc, I get the error message: Badly placed ()'s. Is the syntax different for a C shell than in a Bash shell? If so, what is the correct syntax?

Community
  • 1
  • 1
physicalattraction
  • 6,485
  • 10
  • 63
  • 122

4 Answers4

21

Unfortunately, you can't define functions in csh, like you can in most other shells. This feature does not exist in csh.

The only alternative is to create a script and place it in a directory on your PATH e.g. ~/bin.

dogbane
  • 266,786
  • 75
  • 396
  • 414
4

If you are looking for passing only one argument, just put \!$ where you wish your argument to sit.

For example:

If you put alias mygrep grep -i \!$ myfile.txt in ~/.cshrc, and run mygrep mykeyword, csh will run alias mygrep grep -i mykeyword myfile.txt.

See this for more info.

Keyvan
  • 143
  • 1
  • 8
2

Here is my solution:

#!/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
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
JDS
  • 21
  • 1
0

Set the following in your .cshrc or .tcshrc:

alias function '( set argv = ( \!* ) ; source ~/.cshfuncs )'

Define your functions in .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