home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / question / 12961 < prev    next >
Encoding:
Text File  |  1992-11-06  |  2.1 KB  |  91 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!darwin.sura.net!spool.mu.edu!umn.edu!mmm.serc.3m.com!mmc.mmmg.com!timbuk.cray.com!walter.cray.com!mikeb
  3. From: mikeb@cad5.cray.com (Mike J. Buchmann)
  4. Subject: Re: sh functions
  5. Message-ID: <1992Nov5.121122.8830@walter.cray.com>
  6. Originator: mikeb@cad5
  7. Lines: 77
  8. Sender: mikeb@cad5 (Mike J. Buchmann)
  9. Nntp-Posting-Host: cad5.cray.com
  10. Organization: Cray Research, Inc.
  11. References: <5724@esf.esf.de>
  12. Date: 5 Nov 92 12:11:22 CST
  13.  
  14.  
  15. In article <5724@esf.esf.de>, klaus@helsinkiesf.de (Klaus Wicovsky) writes:
  16. > I would like my sh script to use funtions. The only way I found up to now is this:
  17. > ------------------------------
  18. > #!/bin/sh
  19. > cat << 'EOF' > /tmp/toto
  20. > my_func()
  21. > {
  22. > echo HERE
  23. > }
  24. > EOF
  25. > . /tmp/toto
  26. > my_func
  27. > ------------------------------
  28. > Is there a way to do it without tmp files ????
  29. > Klaus Wicovsky                                                   
  30. >                                                                     
  31. >    ESF Headquarters                    Tel : ++49+30-820 903 47      
  32. >    Hohenzollerndamm 152                Fax : ++49+30-820 903 19      
  33. >    1000 Berlin 33                      UUCP: klaus@esf.de        
  34. >    Germany             
  35.  
  36.  
  37. The following should work the same way:
  38. -----------------------------
  39. #!/bin/sh
  40.  
  41. my_func()
  42. {
  43.   echo HERE
  44. }
  45.  
  46. my_func
  47. ----------------------------
  48.  
  49. You can also receive parameters and return values with functions:
  50.  
  51. ------------------------------
  52. #!/bin/sh
  53.  
  54. # The function getit echos a string passed to it (argv[1]), reads input
  55. # from the user and returns it to the calling function in the variable $input
  56.  
  57. getit () {
  58.   echo "$1"
  59.   read input
  60.   export input
  61. }
  62.  
  63. # MAIN Part of script
  64.  
  65. echo "Start of script"
  66. getit "Please enter your name"
  67. echo "Hello $input"
  68. exit 0
  69.  
  70. ---------------------
  71.  
  72. These functions only work for Bourne Shell scripts,
  73. as far as I know.  If any one knows of doing this in
  74. C-Shell scripts, please let me know how.
  75.  
  76.  
  77. ------------------
  78.  
  79. MIKE BUCHMANN
  80. Cray Research, Inc.
  81. Chippewa Falls, WI  54729
  82.  
  83. email: michael.buchmann@cray.com  OR  mikeb@ted.cray.com
  84.