home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- 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
- From: mikeb@cad5.cray.com (Mike J. Buchmann)
- Subject: Re: sh functions
- Message-ID: <1992Nov5.121122.8830@walter.cray.com>
- Originator: mikeb@cad5
- Lines: 77
- Sender: mikeb@cad5 (Mike J. Buchmann)
- Nntp-Posting-Host: cad5.cray.com
- Organization: Cray Research, Inc.
- References: <5724@esf.esf.de>
- Date: 5 Nov 92 12:11:22 CST
-
-
- In article <5724@esf.esf.de>, klaus@helsinkiesf.de (Klaus Wicovsky) writes:
- >
- > I would like my sh script to use funtions. The only way I found up to now is this:
- >
- > ------------------------------
- > #!/bin/sh
- > cat << 'EOF' > /tmp/toto
- > my_func()
- > {
- > echo HERE
- > }
- > EOF
- > . /tmp/toto
- >
- > my_func
- > ------------------------------
- >
- > Is there a way to do it without tmp files ????
- >
- >
- >
- > Klaus Wicovsky
- >
- > ESF Headquarters Tel : ++49+30-820 903 47
- > Hohenzollerndamm 152 Fax : ++49+30-820 903 19
- > 1000 Berlin 33 UUCP: klaus@esf.de
- > Germany
-
-
- The following should work the same way:
- -----------------------------
- #!/bin/sh
-
- my_func()
- {
- echo HERE
- }
-
- my_func
- ----------------------------
-
- You can also receive parameters and return values with functions:
-
- ------------------------------
- #!/bin/sh
-
- # The function getit echos a string passed to it (argv[1]), reads input
- # from the user and returns it to the calling function in the variable $input
-
- getit () {
- echo "$1"
- read input
- export input
- }
-
- # MAIN Part of script
-
- echo "Start of script"
- getit "Please enter your name"
- echo "Hello $input"
- exit 0
-
- ---------------------
-
- These functions only work for Bourne Shell scripts,
- as far as I know. If any one knows of doing this in
- C-Shell scripts, please let me know how.
-
-
- ------------------
-
- MIKE BUCHMANN
- Cray Research, Inc.
- Chippewa Falls, WI 54729
-
- email: michael.buchmann@cray.com OR mikeb@ted.cray.com
-