Solved

Does the C-shell have functions?

Posted on 1997-08-22
6
2,106 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 91
haveThree challenge 22 118
How to convert MFC::CString to UTF8 wchar_t* 10 215
wordcount challenge 11 122
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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