Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Functions csh

2,741 views
Skip to first unread message

Anthony Perot

unread,
Apr 24, 2002, 10:09:27 AM4/24/02
to
Hi,

How to use functions in csh scripts ?


test1 ()
{
echo "Hi"
}

=> Badly placed ()'s

Anthony PEROT

j...@invalid.address

unread,
Apr 24, 2002, 11:02:40 AM4/24/02
to
Anthony Perot <anthon...@alcatel.fr> writes:

> How to use functions in csh scripts ?

Add the line

#!/usr/bin/ksh

to the top of the script, and adjust syntax accordingly.

Otherwise, put each "function" in a separate script and call that,
putting up with the overhead of starting a new process everytime you
call a subroutine. The first option is better though.

Ben Altman

unread,
Apr 24, 2002, 11:10:01 AM4/24/02
to
"Anthony Perot" <anthon...@alcatel.fr> wrote in message
news:3CC6BC97...@alcatel.fr...

> Hi,
>
> How to use functions in csh scripts ?

From a while ago, a post from Brian Hiles:

----- Original Message -----
From: "Brian Hiles" <b...@rawbw.com>
Newsgroups: comp.unix.shell
Sent: Tuesday, August 07, 2001 6:48 PM
Subject: Re: csh function syntax?


kim <kim.w...@compaq.com> wrote:
> nos...@please.thankyou wrote:
> > functions in csh...
> I can't really say why, but yes I think it is by some uninformed
> people. I've studiously avoided learning anything about csh, only to
> have to learn it after all.

Well, just so we know that you understand that you know that we
understand....

If you _really_ have to use csh here is a kludge to simulate functions
in csh.... I did not come up with this and merely forward this information.

From: kla...@informatik.rwth-aachen.de (Klaus Frank)
Newsgroups: comp.unix.shell
Subject: Re: function in c-shell
Date: 17 Jan 2001 18:45:09 GMT

In comp.unix.shell you write:
>Can I write a function in .cshrc that can accept an argument? I yes,
>can someone give an example. I would like to be able to call the
>function in the command line at anytime I like.

Yes. It can be done with a little helper function... see usage example at
end.
!!:1 means first argument, !!:15 the fifteenth argument, !!:* all arguments.

-- Klaus Frank <kla...@Pool.Informatik.RWTH-Aachen.DE>

alias make_alias '\\
<<"end_alias" sed -e '"'"'\\
#\\!/bin/sed -f\\
#\\
## Make the eval command\\
# quote quotes\\
s/'"'"'"'"'"'"'"'"'/'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'/g;
\\
# enclose with quotes\\
1s/^/eval '"'"'"'"'"'"'"'"'/;\\
$s/$/'"'"'"'"'"'"'"'"'/;\\
#\\
## Make the alias command\\
# escape exclamation marks and newlines\\
s/\\!/\\\\!/g;\\
s/$/\\\\/g\\
# quote quotes\\
s/'"'"'"'"'"'"'"'"'/'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'/g;
\\
# enclose with quotes\\
1s/^/alias @alias_name@ '"'"'"'"'"'"'"'"'/;\\
$a\\\
;echo >/dev/null'"'"'"'"'"'"'"'"'\\
#^^^^^^^^^^^^^^^ (workaround against printing of arguments)\\
'"'"' | \\
sed -e s/@alias_name@/\!\!:1/ >/tmp/make_alias$$; \\
source /tmp/make_alias$$; \\
rm /tmp/make_alias$$; \\
'

make_alias example
echo -n 'starting command "!!:1" .....'
if ( { !!:1 } ) then
echo ok
else
echo fail
endif
"end_alias"

example true
example false

=Brian


David Thompson

unread,
Apr 24, 2002, 8:21:05 PM4/24/02
to
"Anthony Perot" <anthon...@alcatel.fr> wrote

> How to use functions in csh scripts ?
>
>
> test1 ()
> {
> echo "Hi"
> }
>
> => Badly placed ()'s

Why are you programming in csh? Can you switch to another shell?

Read these,

http://www.grymoire.com/Unix/Csh.html
http://www.grymoire.com/Unix/CshTop10.txt
http://www.perl.com/pub/a/language/versus/csh.html

--
David Thompson
dat...@yahoo.com

Bruce Barnett

unread,
Apr 26, 2002, 6:59:52 AM4/26/02
to

Besides Brian's suggestion (which I think was inspired by aliases from
Dan Bernstein) the only other way to do it is to use separate
files/scripts for each function. But if you do that, you can also use
/bin/sh


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

swa...@gmail.com

unread,
Jul 26, 2019, 4:01:03 PM7/26/19
to
#!/bin/tcsh -f

set RETURN = HERE_1
set input = "HELLO WORLD"
goto FUNCTION
HERE_1:
echo "input=$input, output=$output"

echo ""

set RETURN = HERE_2
set input = "GOOD DAY"
goto FUNCTION
HERE_2:
echo "input=$input, output=$output"



#-------------------------------------------------------------------------------

exit
FUNCTION:
set output = (`echo $input | awk '{print $2,$1}'`)
goto $RETURN
exit

#-------------------------------------------------------------------------------

# This will produce the following:

input=HELLO WORLD, output=WORLD HELLO

input=GOOD DAY, output=DAY GOOD


0 new messages