Shell Calling functions in a script

Greetings all,

I have developed two functions: function_find () and function_replace (), and use them in a following script find_replace.sh:
Code:
#!/usr/bin/env sh

# Functions definitions

function_find()
{
  commands...
  commands...
}

function_replace ()
{
  commands...
  commands...
}

# Main

function_find () > my_file
if [ -s my_file ]
then
  function_replace () < my_file
else
  printf %s\\n "File "$my_file" does not exist."
fi
However the script did not work as expected due to an error in the function_find () . So I modified the function_find () , which worked stand alone, but I have forgotten to propagate the modification to the find_replace.sh.

Is there a way to call the functions from the script without having to literally include them?

Kindest regards,

M
 
You can use the sh "dot" command to "include" functions from some other file.

Your main script:
Code:
. ~/myShFunctionsScript.sh
# Main
function_find() > my_file
...

The myShFunctions.sh file:
Code:
# Functions definitions

function_find()
{
  commands...
  commands...
}

function_replace ()
{
  commands...
  commands...
}
 
As was pointed out above:

. script.sh or source script.sh is a way to run script.sh in the current shell, so that its results or characteristics become part of the calling shell environment, as opposed to invocation in a child shell by calling the script by name as script.sh.
 
Greetings all,

thank you for the replies. Just to make certain that I understand correctly, if i place the functions' definitions in e.g., /opt/functions/, the script will call them like this:
Code:
#!/usr/bin/env sh

# Functions call
./opt/functions/function_find ()
./opt/functions/function replace ()

# Main
function_find() > my_file
...

Correct?

BTW, is /opt a good location, or is there a better one? hier(7) does not seem to specify this.

Kindest regards,

M
 
Well isn't the first thing you do when building a new system, is to replace sh with bash?....LOL! just kidding. I know...pure treason!
Sad but true. FBSD lacks a BSD-licensed modern Shell that can be comparable to Bash (GPL-ed).
 
no...you don't name a script as you would a function, with parenthesis.

include.sh
Code:
#! /usr/local/bin/bash

fn1 () {
    echo $1 " --- " $2;
}

fn2 () {
    echo ">>> $1 <<<";
}

main.sh
Code:
#! /usr/local/bin/bash

. include.sh        # includes functions

fn1 a b
fn2 what

output
Code:
$ main.sh
a --- b
>>> what <<<
$
 
Well isn't the first thing you do when building a new system, is to replace sh with bash?....LOL! just kidding. I know...pure treason!
Well, the reason I pointed it out is that I believe implicit dependencies should be uncovered and made explicit. It is fine if you are using bash but please be upfront about it when teaching others about shell scripting.

Ignorance that /bin/sh on $insert_random_os_here might not have all of the features of bash leads to all sorts of fun for all downstream consumers.
 
Hi @tobik,

that is exactly the reason I use /bin/sh and consult POSIX, since I have learnt that even /bin/sh has non-POSIX compliant variants.

Kindest regards,

M
 
Sad but true. FBSD lacks a BSD-licensed modern Shell that can be comparable to Bash (GPL-ed).
Well, there is shells/pdksh, which is my personal favorite. BSD3 licensed and you can do quite a lot with this critter. I've even started targeting it explicitly myself in some of my scripts, I honestly think that you might end up surprised.

Fair warning though: bias is involved here. For me FreeBSD is a direct replacement for Sun Solaris which was quite a passion of mine, and right now ksh (and ZFS I suppose) is pretty much the only thing which is directly left of that experience ;) As such I cannot rule out some favoritism on my end.
 
ShelLuser: Bash is too popular so it documents are everywhere. About ksh, I'm a bit afraid because I can't find much about it. People also said about MirBSD Korn Shell and said it's being used by Android, but, I can't even find any document for it :confused:
 
About ksh, I'm a bit afraid because I can't find much about it.
The Posix shell is ksh. So just grab a copy of the Posix standard. There are also lots of good books about the korn shell. The O'Riley book has a turtle on it; but I think the best book about ksh is David Korn's book (duh, obviously).

People also said about MirBSD Korn Shell and said it's being used by Android, but, I can't even find any document for it :confused:
http://www.mirbsd.org/mksh.htm
 
Back
Top