Solved

Does the C-shell have functions?

Posted on 1997-08-22
6
2,096 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:eliseo080397
Comment Utility
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
Comment Utility
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
Comment Utility
Yes. Thanks for the specific details you gave. Now I need to do some coding and testing.

Thanks again!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
Modbus - whats the maximum I can store in one register? 4 74
mapBully challenge 6 88
Path of Workbook 3 44
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now