Solved

Does the C-shell have functions?

Posted on 1997-08-22
6
2,119 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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