Results 1 to 3 of 3

Thread: Source with Functions from init.d script

Hybrid View

  1. #1
    Join Date
    May 2012
    Beans
    3

    Question Source with Functions from init.d script

    I have created a file with a number of bash functions defined. When I "source" it from another bash script that I execute while logged on, everythinng runs as expected. However, if I attempt to source it in a script that is linked as part of my startup (say from /etc/rc2.d/S99test) the attempt to source fails.

    For example, if my functions file, "test_functions", contains:

    Code:
     
    #!/bin/bash
     
    function test ()
    {
            echo "Test function output..." >> /tmp/test.out
    }
    And I source in an init script, "/etc/init.d/test_init.sh":

    Code:
     
    #!/bin/sh
     
    ### BEGIN INIT INFO
    # Provides:
    # Required-Start:    $remote_fs $syslog $all
    # Required-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:
    # Short-Description: Run /etc/rc.local if it exist
    ### END INIT INFO
     
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
     
    . /lib/init/vars.sh
    . /lib/lsb/init-functions
    . /usr/local/repo/test_functions
     
    do_start() {
    .
    .
    .
    If I execute the script while logged in everything is fine. But when the system is rebooted, the attempt to source fails. The error that I managed to trap is as follows:

    Code:
     
    /usr/local/repo/test: 3: Syntax error: "(" unexpected
    I also noticed that when I am logaged on I can use the command "source" in the script but during reboot a "command not found error" occurs leading me to believe that there is some inherit difference in the execution environment.

    How can I workaround this issue?

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Source with Functions from init.d script

    Code:
    #!/bin/sh
    
    ### BEGIN INIT INFO
    This is a sh script, not a bash script. You cannot source bash functions in sh because sh does not support that syntax.

  3. #3
    Join Date
    May 2012
    Beans
    3

    Lightbulb Re: Source with Functions from init.d script



    Exhaustion has finally taken its toll. Thank you and apologies for not spotting that.

Tags for this Thread

Bookmarks

Posting Permissions

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