Find the answer to your Linux question:
Results 1 to 8 of 8
Hi. I tried it the way cabhan describet it, but there has to be a mitstake in my way. I tried it like this: main script: Code: #!/bin/bash source functions.sh ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    BBX
    BBX is offline
    Just Joined!
    Join Date
    Jun 2009
    Location
    Germany
    Posts
    3

    How to include other functions in the other scripts


    Hi.

    I tried it the way cabhan describet it, but there has to be a mitstake in my way.

    I tried it like this:

    main script:
    Code:
    #!/bin/bash 
    source functions.sh
    
    echo "test: "
    
    _newecho
    
    exit 0
    and the function (function.sh in the same folder)
    Code:
    #!/bin/bash
    
    function _newecho()
    {
    echo "Funktionstest"
    read testvar
    echo "$testvar" 
    }
    
    exit 0
    can anybody tell me what i am doing wrong?

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    You need to use the $include directive.

    Code:
    $include /path/to/functions.sh

  3. #3
    BBX
    BBX is offline
    Just Joined!
    Join Date
    Jun 2009
    Location
    Germany
    Posts
    3
    Thanks, but that doesn't work either.

    I didn't even know you could use $include, i thought in bash one had to use the dot to include. Well, i tried it, doesn't work

    I also tried it like this:

    Code:
    #!/bin/bash 
    . ./functions.sh
    
    echo "test: "
    
    newecho
    
    exit 0
    doesn't work either. funny thing is, i don't even get an error message. it just like the script wouldn't to anythin, i dont eben get the echo "test: " to show in the terminal.

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    The dot (or source command) executes the script in the current shell environment. Since functions.sh only defines a bash function in it that's why nothing happened.

    I tried the $include directive and it worked for me. What did the $include look like in your script?

  5. #5
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    I just figured it out. The 'exit 0' in functions.sh causes the script to terminate. Remove that from functions.sh and use the $include directive in your main script.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,754
    Quote Originally Posted by lomcevak View Post
    The dot (or source command) executes the script in the current shell environment. Since functions.sh only defines a bash function in it that's why nothing happened.

    I tried the $include directive and it worked for me. What did the $include look like in your script?
    No, source only "executes" when used on the command line.

    Code:
    source, . (dot command)
    
        This command, when invoked from the command-line, executes a script. Within a script, a source file-name loads the file file-name. Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This is useful in situations when multiple scripts use a common data file or function library.
    But yes, once inserted, the exit call ends the script prematurely.

    Google "bash scripting guide" => Best Online Bash Guide

  7. #7
    BBX
    BBX is offline
    Just Joined!
    Join Date
    Jun 2009
    Location
    Germany
    Posts
    3
    Thank you, guys. Now it works. Didn't think about the exit command. Now that I know it's pretty obvious.

    I still got a question about the difference between source and the dot.

    When using source its like the code would be physically in the script, the same variables are used etc. and if I use the dot to include it has it's own variables and i have to return them to the main script ?

  8. #8
    Just Joined!
    Join Date
    Jun 2009
    Posts
    1
    The dot (.) and source are bash builtins that are equivalent.

Posting Permissions

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