Best to have a file that just contains your routines. It doesn't need a #!/bin/sh at the top or an exit at the bottom and it doesn't need to be executable. I've called my file "subs" and this is the contents:
Code:
my_sub ()
{
echo in sub 1
}
my_other_sub ()
{
echo in sub 2
}
I've now created a script file that can use these subs. It's included in the main file by the "." command and it just includes all the code in the subs file in my main file. Obviously there might be many scripts all using this set of subs. Note I'm calling the procedures (my_sub, my_other_sub) in the main script but not defining them there. I've called my main script "my_script" and this is the contents:
Code:
#!/bin/sh
. subs
echo test a
my_sub
echo test b
my_other_sub
echo test c
exit 0
To run the script I need to make the main script executable and then call it. I've included the output.
Code:
chmod +x my_script
./my_script
test a
in sub 1
test b
in sub 2
test c
Hope that helps. I wouldn't get too worked up about procedural programming while shell scripting - it is powerfull but it's not a high level language.
Mike