Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Does the C-shell have functions?

Posted on 1997-08-22
6
2,108 Views
Last Modified: 2013-12-26
I am trying to write a C-shell script and need to define some standalone functions that I can call from within the script. Does the C-shell support functions at all? If not, how do I accomplish the same task. I need to pass parameters to the function and the results back to the calling script.
0
Comment
Question by:eliseo080397
  • 3
  • 3
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1294541
If you can't put the function in a file to be called from the script,
could you put it in a variable?

  set f = '@ x = $1 + $2 ; echo $x'

  set y = `csh -c "$f" 3 5`

  echo $y

0
 

Author Comment

by:eliseo080397
ID: 1294542
I still don't know if the C-shell supports functions. I know that other shells do but I find no documentation about it on the C-shell. Your answer is a bit criptic for me (I am fairly new at this). If you could, please clarify how I would apply your answer with a sample input/output line.

Thanks!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1294543
I thought I did give a sample input/output line.
It declares a function to return the sum of two numbers
and then a call of that function to add 3+5 to set $y to 8
(although it may be a little difficult to see the
difference between the different kinds of quotes the
way the display is formatted)
For a function like that, I'd probaly call one some other
utility rather than doing it in csh, but it was an illustration.
Perhaps if you could give me an example of what kind of function
you'd want to write, I could come up with an example of
how to do it in csh?
   
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:eliseo080397
ID: 1294544
Thanks for explaining better what you meant. My specific intent is to create a function that supplies the tangent of an angle. Here's what I have so far:

set degrees = 40
set angle = `echo "scale=8; $degrees*.017453293" | bc`
set sin   = `echo "scale=8; s($angle)" | bc -l`
set cos   = `echo "scale=8; c($angle)" | bc -l`
set tan   = `echo "scale=8; $sin/$cos" | bc`
echo "Tangent of angle is: "$tan

For illustration purposes, this example produces the tangent of 40 degrees using the bc utility in csh. It works fine as it is, but I had hoped I could make it a standalone function that could be accessed from within my script. Even if it could be used as a subroutine within the script, that would also be acceptable.
Could you comment please?
Thanks again for your assistance!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1294545
Well, I presume you've already considered creating a file, e.g.:
cat >tan.csh <<EOF
#!/sbin/csh
set degrees = $1
set angle = `echo "scale=8; $degrees*.017453293" | bc`
...
EOF
and that you want your script to "stand alone" without requireing
any other extenal files (other than standard system programs)
In which case, since I don't see any explicit functions in the
manner of ksh in the csh documentation either,
I thought perhaps you might be able to get the same effect using
the features which are there.
In this case, you might do:

set tan='echo "scale=8;d=$1*.017453293;s(d)/c(d)"|bc  -l'

then invoke it like

echo "Tangent of angle is: " `csh -c "$tan" 40`

which isn't very pretty, but it works.
or maybe in this case, a slightly better notation could be:

alias tan 'echo "scale=8;d=.017453293*\!*;s(d)/c(d)"|bc  -l'

then

echo "Tangent of angle is: " `tan 40`

Is that the kind of thing you were looking for?
0
 

Author Comment

by:eliseo080397
ID: 1294546
Yes. Thanks for the specific details you gave. Now I need to do some coding and testing.

Thanks again!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 73
Replacement selected text 2 63
windows 10 how make picture as desktop background 2 65
List out all word 7 316
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question