Please support VectorLinux!

Author Topic: calling functions in other files  (Read 4090 times)

Daniel

  • Packager
  • Vectorian
  • ****
  • Posts: 704
    • TuxToys - Packages for VectorLinux 6.0
calling functions in other files
« on: August 06, 2009, 04:07:07 pm »
(I guess this is the right place to put this)

How do you call functions from a shell script that are in files other than the shell script itself? I saw an example of this in the vasm scripts in /sbin. There is a vasm script and a vasm-functions script. At different points, vasm or a script that vasm executes call functions from the vasm-functions script such as check_root. I tried to call a function in a "functions" script (for a script that I am writing) in the (I think) the same way that the vasm script did it but I couldn't get it to work. Help?
The following sentence is true. The previous sentence is false.

VL 6.0 SOHO KDE-Classic on 2.3 Ghz Dual-core AMD with 3 Gigs of RAM

brokndodge

  • Member
  • *
  • Posts: 83
  • Linux is sooo HOT
    • My Blog
Re: calling functions in other files
« Reply #1 on: August 06, 2009, 05:24:47 pm »
post your scripts so we can take a look. 

according to http://ubuntuforums.org/showthread.php?t=664657 bash has an equivalent to c's include statement.  it's simply a . so to include functions from another file you would just do:
Code: [Select]
#! /bin/bash

# here is the include statement
. somefunctionfile.sh   #don't forget the space between the . and the filename

functioncall

VL 7.0 Standard

brokndodge
- OSS is not a religion, it's the solution to buggy irresponsible coding -
Linux User# 494720

Daniel

  • Packager
  • Vectorian
  • ****
  • Posts: 704
    • TuxToys - Packages for VectorLinux 6.0
Re: calling functions in other files
« Reply #2 on: August 06, 2009, 05:41:33 pm »
Thanks! Using the include statement thing worked!
The following sentence is true. The previous sentence is false.

VL 6.0 SOHO KDE-Classic on 2.3 Ghz Dual-core AMD with 3 Gigs of RAM

M0E-lnx

  • Administrator
  • Vectorian
  • *****
  • Posts: 3208
Re: calling functions in other files
« Reply #3 on: August 07, 2009, 03:44:53 am »
The actual commamd is 'source' in bash. The . Is a shortcut for it