Subroutine in shell script

Subroutine in shell script

Post by John Poltora » Wed, 08 Jan 2003 18:20:11



Is there any way to incorporate a subroutine into a shell script?

--
John

 
 
 

Subroutine in shell script

Post by Chris F.A. Johnso » Wed, 08 Jan 2003 18:47:03



> Is there any way to incorporate a subroutine into a shell script?

   You can write a function, or you can source a file. Or both.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Subroutine in shell script

Post by John Poltora » Wed, 08 Jan 2003 18:48:34




>>Is there any way to incorporate a subroutine into a shell script?

>   You can write a function, or you can source a file. Or both.

How do I write/call a function?

--
John

 
 
 

Subroutine in shell script

Post by Chris F.A. Johnso » Wed, 08 Jan 2003 18:54:31





>>>Is there any way to incorporate a subroutine into a shell script?

>>   You can write a function, or you can source a file. Or both.

> How do I write/call a function?

## define the function:
show() {

Quote:}

## call the function:
show and tell

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Subroutine in shell script

Post by Marty » Wed, 08 Jan 2003 19:03:53





>>> Is there any way to incorporate a subroutine into a shell script?

>>   You can write a function, or you can source a file. Or both.

> How do I write/call a function?

Well in Bash it's something like this, I believe the subroutine must be
defined before you call it (i.e. you can't stick all the subroutines at
the end of the main script:

#!/bin/bash

testsub()
{
         echo $1

Quote:}

testsub "testing"

--
Speak to the Penguin, he is your friend.

Remove WINDOZE before replying by Email

 
 
 

Subroutine in shell script

Post by Chris F.A. Johnso » Wed, 08 Jan 2003 19:22:16






>>>> Is there any way to incorporate a subroutine into a shell script?

>>>   You can write a function, or you can source a file. Or both.

>> How do I write/call a function?

> Well in Bash it's something like this,

    It's like this in any Bourne-type shell.

Quote:> I believe the subroutine must be defined before you call it

    Right.

Quote:> (i.e. you can't stick all the subroutines at the end of the main
> script:

    Generally, yes, but you could put your script in the first
    function (e.g., main()), then define all your other functions,
    then call:


Quote:> #!/bin/bash

> testsub()
> {
>          echo $1
> }

> testsub "testing"

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

Subroutine in shell script

Post by Barry Kimelma » Wed, 08 Jan 2003 23:34:24


[This followup was posted to comp.unix.shell and a copy was sent to the
cited author.]



Quote:> Is there any way to incorporate a subroutine into a shell script?

Here is an example of a shell script that implements a "debuygging print"
function (i.e. the function will only print the indicate strings if a "-
d" parameter was passed in to the script.

#!/usr/bin/ksh

function debug_print
{
        if [ $debug_flag == 1 ]
        then
                echo $*
        fi

Quote:}

errors=0
debug_flag=0
opt_a=0
while getopts ado: opt
do
        case $opt in
        a)
                FLAG=$opt
                ;;
        d)
                debug_flag=1
                ;;
        o)
                OARG=$OPTARG
                ;;
        \?)
                errors=1
        esac
done

shift `expr $OPTIND - 1`

echo "$0 : All args after parameters are : $*"
if [ $errors == 1 ]
then
        echo "Aborted due to parameter errors."
        exit 1
fi
debug_print "Test 1"

for var in $*
do
        echo "-- $var --"
done

exit 0

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada

 
 
 

Subroutine in shell script

Post by Rikard Bosnjakovi » Sun, 12 Jan 2003 08:37:58


Quote:> How do I write/call a function?

You read the manual for your shell, under the heading "FUNCTIONS".

--
Friendly,

Rikard

 
 
 

1. Need shell script to cut a file into subroutines


On our system we use a system command called 'fsplit' to do this.  Here is a bit
of the man page:

FSPLIT(1)                USER COMMANDS                  FSPLIT(1)

NAME
     fsplit - split a multi-routine FORTRAN file into  individual
     files

SYNOPSIS
     fsplit [ -e efile ] ... [ file ]

DESCRIPTION
     fsplit takes as input either a file or standard  input  con-
     taining FORTRAN source code.  It attempts to split the input
     into separate routine files of the form name.f,  where  name
     is the name of the program unit (function, subroutine, block
     data or program).

-- Tom
--
+--------------------------------------------------------------------+
| Tom Parker             |  National Center for Atmospheric Research |

+--------------------------------------------------------------------+

2. kppp doing strange things to resolv.conf

3. handling SIGINT in shell scripts when executing another shell script.

4. xfree86 configuration for Dell XPS R 400Mhz

5. Q: How can I have a shell script call another shell script...

6. Anyone with experience with the DG Aviion??

7. Shell script invoking other shell scripts

8. HELP: slow serial write? SiGIO & serial drivers

9. Convert Bash shell script to Korn shell script

10. Shell Script Help (C-Shell Script)

11. How to pass a variable from a shell script to another shell script...

12. Shell script acting as interactive shell - $0 not script name??

13. shell script within shell script (general question)