C-Shell Function

Y

YZXIA

Guest
I am pretty sure C-Shell does not have the ability to create or call
function/method/subroutine. (Correct me if I am wrong)

Is there way for C-Shell to mimic the function ability? I am trying to
avoid writing seperate C-Shell files for each function.

 


On 14 Jan 2007 18:18:44 -0800, "YZXIA" <yzx27@hotmail.com> wrote:

>I am pretty sure C-Shell does not have the ability to create or call
>function/method/subroutine. (Correct me if I am wrong)
>
>Is there way for C-Shell to mimic the function ability? I am trying to
>avoid writing seperate C-Shell files for each function.


Offhand:

if ( $#argv >= 2 && "$1" == "--function" && "$2" == myfun1 ) then
shift
shift
...
endif

The less obvious part is to have the script re-call itself *reliably*,
because of path and cd issues. At the very beginning of the
non-function part of the script, you have to do

setenv MYSCRIPT `which $0`

and you invoke functions with

$MYSCRIPT --function myfun1 foo bar

HTH,
 
"YZXIA" <yzx27@hotmail.com> writes:
> I am pretty sure C-Shell does not have the ability to create or call
> function/method/subroutine. (Correct me if I am wrong)
>
> Is there way for C-Shell to mimic the function ability? I am trying to
> avoid writing seperate C-Shell files for each function.


Very small functions can be implemented as aliases.

If you're sufficiently desperate, I suppose you can set up an array
variable to be used as a call stack, using goto statements to simulate
both calls and returns. (The label in a goto statement can be a
variable reference.)

But I really don't think it's worth the effort. I'm a tcsh user
myself, but I wouldn't recommend using it for scripting; consider
learning and using Bourne shell, or bash, or ksh, or even something
like Perl or Python.

You should read <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
before making a decision.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
In article <lnzm8i8w5c.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:

> "YZXIA" <yzx27@hotmail.com> writes:
> > I am pretty sure C-Shell does not have the ability to create or call
> > function/method/subroutine. (Correct me if I am wrong)
> >
> > Is there way for C-Shell to mimic the function ability? I am trying to
> > avoid writing seperate C-Shell files for each function.

>
> Very small functions can be implemented as aliases.
>
> If you're sufficiently desperate, I suppose you can set up an array
> variable to be used as a call stack, using goto statements to simulate
> both calls and returns. (The label in a goto statement can be a
> variable reference.)
>
> But I really don't think it's worth the effort. I'm a tcsh user
> myself, but I wouldn't recommend using it for scripting; consider
> learning and using Bourne shell, or bash, or ksh, or even something
> like Perl or Python.
>
> You should read <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
> before making a decision.


I agree with yzxia. I use perl for that ability, and sometimes create
aliases in csh to call a perl "one liner". For example, to print out
the string representing a unix timestamp, I have:

alias loct "perl -e 'print ( scalar localtime( !$ ),qq(\n) )'"

which takes a single parameter and prints out the localtime string.

I am also reading a lot about zsh as a great replacement for all the
above shells.

Boyd
 
In article <lnzm8i8w5c.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:

> "YZXIA" <yzx27@hotmail.com> writes:
> > I am pretty sure C-Shell does not have the ability to create or call
> > function/method/subroutine. (Correct me if I am wrong)
> >
> > Is there way for C-Shell to mimic the function ability? I am trying to
> > avoid writing seperate C-Shell files for each function.

>
> Very small functions can be implemented as aliases.
>
> If you're sufficiently desperate, I suppose you can set up an array
> variable to be used as a call stack, using goto statements to simulate
> both calls and returns. (The label in a goto statement can be a
> variable reference.)
>
> But I really don't think it's worth the effort. I'm a tcsh user
> myself, but I wouldn't recommend using it for scripting; consider
> learning and using Bourne shell, or bash, or ksh, or even something
> like Perl or Python.
>
> You should read <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>
> before making a decision.


I agree with Keith. I use perl for that ability, and sometimes create
aliases in csh to call a perl "one liner". For example, to print out
the string representing a unix timestamp, I have:

alias loct "perl -e 'print ( scalar localtime( !$ ),qq(\n) )'"

which takes a single parameter and prints out the localtime string.

I am also reading a lot about zsh as a great replacement for all the
above shells.

Boyd
 

Back
Top