Declaring functions in CSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Declaring functions in CSH
# 1  
Old 07-26-2006
MySQL 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
# 2  
Old 01-02-2007
Error 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. Smilie

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
# 3  
Old 01-22-2007
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
# 4  
Old 01-22-2007
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. Smilie

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.
# 5  
Old 01-22-2007
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. Smilie

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 05:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Functions on csh

Just want to know whether we have function concept available in csh. Because am trying the below function it showing error. If function concept is not available what is the alternative solution. code: #!/usr/bin/csh great() { echo "inside" } great Error: Badly placed ()'s. (2 Replies)
Discussion started by: rogerben
2 Replies

2. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

3. Shell Programming and Scripting

Declaring arrays in csh

Does anyone know how to declare an array in csh? I don't want to declare any elements in the array because I have a variable that represents the size of the array. For example: the array I want to declare is called sortList and it passes in the variable ARRAYSIZE that stores the value 8. (1 Reply)
Discussion started by: demet8
1 Replies

4. UNIX for Dummies Questions & Answers

PERL: Declaring Array

Is it possible to declare an array in the following way: @tmp = (@f,"String1","String2", "String3",@f); I'm getting the following error message: Array found where operator expected at Program.pl line 181, near "" (Missing semicolon on previous line?) ---------- Post updated at... (1 Reply)
Discussion started by: WongSifu
1 Replies

5. Shell Programming and Scripting

Declaring variables without initialization

I get an error in my shell script that line 1: )unexpected. Line 1 in my script (using sh by the way) is the variable I declared but did not initialize it. result= Is this wrong? How can I fix it? I am using the variable later in the program, so I figured I could just declare it first... (4 Replies)
Discussion started by: itech4814
4 Replies

6. Programming

Declaring variables

Hey guys im facing a problem in declaring variables. i have a few classes like the one below... #ifndef _FINANCE_H #define _FINANCE_H #include <string> #include <iostream> #include <fstream> #include <stdlib.h> using namespace std ; class readStraitsTimesIndex { ... (3 Replies)
Discussion started by: gregarion
3 Replies

7. Shell Programming and Scripting

CSH: Concatenating Strings, how to add new line character and functions?

Hello, I'm trying to run a program on a directory (traverse sub dirs too) through my csh script. Arrays support in CSH is appalling, something like associative arrays would have helped me do this so much easier. Anyway, I want to hold some details extracted from the program and then at the... (0 Replies)
Discussion started by: ragabonds
0 Replies

8. Shell Programming and Scripting

declaring variable with for $(seq)

Hi guys. i have the following script: 1 #!/bin/bash 2 linkcount=$(grep "/portal" tickets | wc -l) 3 grep "/portal" tickets > links 4 for i in $(seq 1 $linkcount); do 5 echo "BLYAT" 6 let link$i=$(sed -n "$i"p links) 7 echo $ 8 done the problem is, that "let" can`t... (1 Reply)
Discussion started by: neverhood
1 Replies

9. Shell Programming and Scripting

csh failing to call an 2 embedded csh script

I have an extraordinary problem with a csh script.....(feel free to berate the use of this but I'm modifying an existing bunch of them) Anyway, I have a master csh script which in turn calls a second csh script. This second csh script is below. Within this second script are two compiled C++... (1 Reply)
Discussion started by: pollsizer
1 Replies

10. Shell Programming and Scripting

Declaring Local Arrays

I have some function function() { fileNamelist=( `find Uploads -name "somePattern" | tr '\n' ' '` ) } but "local fileNamelist" makes it variable. How do I declare fileNameList as a local array in BASH? (1 Reply)
Discussion started by: ksh
1 Replies
Login or Register to Ask a Question