Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Unanswered: functions in common file?

    Hi!

    I'm trying to learn shell scripting and have runned into problems with functions.

    Since you have to read all functions into memory before they are used it would be nice to be able to place all your functions in an functions.sh file and start the main script by running the function.sh script i.e. read all functions into memory.

    Is this possible?

    I have tried to place the functions in a script:

    #!/bin/sh -vx

    s_echo_hello()
    {
    echo hello
    }


    And then read it and tried to run the s_echo_hej in another script:

    #!/bin/sh -vx

    /tmp/common.sh

    s_echo_hello


    Here are the result:

    #!/bin/sh -vx

    /tmp/common.sh
    + /tmp/common.sh
    #!/bin/sh -vx

    s_echo_hej()
    {
    echo hej
    }

    s_echo_hej
    + s_echo_hej
    ./testi.sh: s_echo_hej: not found


    Is this possible? And in that case, what am I doing wrong?


    TIA

    Niklas

  2. #2
    Join Date
    Oct 2003
    Posts
    3
    Hi again!

    I just figured it out from http://steve-parker.org/sh/functions.shtml#libraries

    You have to put a . before you execute the script with your functions.

    I have at this time no clue what that . means. Any one?

    /Niklas

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by niklasd
    Hi again!

    I just figured it out from http://steve-parker.org/sh/functions.shtml#libraries

    You have to put a . before you execute the script with your functions.

    I have at this time no clue what that . means. Any one?

    /Niklas
    It means 'source' the script into the current shell (as opposed to 'execute'). In some older shells you might actually have to use the word 'source' rather than the '.'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •