The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Declaring Global Variables in KLD int80h BSD 1 01-21-2008 05:11 AM
ksh functions scriptingmani Shell Programming and Scripting 3 07-06-2007 04:15 AM
functions in Raom Shell Programming and Scripting 6 07-21-2006 12:06 AM
Declaring Local Arrays ksh Shell Programming and Scripting 1 02-22-2005 05:16 AM
Declaring variable environments in .cshrc v_sharda UNIX for Dummies Questions & Answers 2 09-23-2002 07:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2006
Registered User
 

Join Date: Jun 2006
Posts: 35
Thumbs up Declaring functions in CSH

Hi all,
It might seem like a very trivial question but I am new to shell scripting so excuse for that.

I have an existing script in CSH. I need to create a function in the script which takes some parameters and returns a result. Can someone please let me know the syntax for function declaration and how to call the function passing parameters.

Also if someone has a good tutorial with examples on CSH please send me the link.

Thanks in advance.

Regards,
Tipsy
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-02-2007
Registered User
 

Join Date: Jan 2007
Posts: 3
Exclamation Hi Tipsy,

I had a similar problem with declaring the functions in csh.
A simple script as below worked well in sh shell
#! /bin/sh

display()
{
echo "Inside display()"
}

echo "calling display()"
display

however when the shell interpreter was changed to "csh" it failed with following error :-
"Badly placed ()'s"

It looks like csh shell scripting doesn't support functions.

I have been searching over the net now for quite sometime for the function syntax in csh but never found one.

Please let me know if anyone finds the solution for the same.

Best Regards,
brendon
Reply With Quote
  #3 (permalink)  
Old 01-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by tipsy
Hi all,
It might seem like a very trivial question but I am new to shell scripting so excuse for that.

I have an existing script in CSH. I need to create a function in the script which takes some parameters and returns a result. Can someone please let me know the syntax for function declaration and how to call the function passing parameters.

Also if someone has a good tutorial with examples on CSH please send me the link.

Thanks in advance.

Regards,
Tipsy
#!/usr/bin/csh

ThisIsAFunction()
{
echo "$1 $2"
}

#Main

VAR1=Hi
VAR2=there
ThisIsAfunction $VAR1 $VAR2

-------------------

Running this script would result in:

Hi there

on your screen.

Passing parameters to a functions is like passing arguments to a script. They are stored in $1, $2, $3 etc
Running this script would result in the following
Reply With Quote
  #4 (permalink)  
Old 01-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by brendon4_u
I had a similar problem with declaring the functions in csh.
A simple script as below worked well in sh shell
#! /bin/sh

display()
{
echo "Inside display()"
}

echo "calling display()"
display

however when the shell interpreter was changed to "csh" it failed with following error :-
"Badly placed ()'s"

It looks like csh shell scripting doesn't support functions.

I have been searching over the net now for quite sometime for the function syntax in csh but never found one.

Please let me know if anyone finds the solution for the same.

Best Regards,
brendon
Seems to be an issue with your CSH.

You can trying to replace the double quotes (") by single quotes (') in the echo commands.
This would work fine as long as you don't want to echo values of variables to the screen.
Reply With Quote
  #5 (permalink)  
Old 01-22-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,549
Quote:
Originally Posted by brendon4_u
I had a similar problem with declaring the functions in csh.
[...]
however when the shell interpreter was changed to "csh" it failed with following error :-
"Badly placed ()'s"

It looks like csh shell scripting doesn't support functions.

I have been searching over the net now for quite sometime for the function syntax in csh but never found one.

Please let me know if anyone finds the solution for the same.
[...]
It doesn't support functions,
you can use aliases.
See:

http://www.grymoire.com/Unix/CshTop10.txt

Last edited by radoulov; 01-22-2007 at 01:00 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0