Solved

Does the C-shell have functions?

Posted on 1997-08-22
6
2,102 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 37
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 517
Line meaning 9 81
sumHeights2  challenge 7 87
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

11 Experts available now in Live!

Get 1:1 Help Now