Complete Communications Engineering

One solution is to copy and paste the required functions from one shell script to another.  This is probably the most often used solution.  Another solution is to use the source command which allows one script to include code from another script.  This can be helpful when more complicated scripts are required as it allows code re-use by keeping a library of functions in a shared script.

The source command is built in to most Linux shells.  It can be invoked by either typing ‘source’ or ‘.’.  The arguments to the source command are the filename of a shell script followed by any arguments that should be passed to that script.  The source command executes the script in the current shell environment.  It does not create a new shell environment as just running the script would.  Because of this, any environment variables or functions defined by the sourced script remain in the environment after it’s finished.  The filename argument to the source command may be a full or relative path.  If it’s a relative path, the source command will first search the folders in the $PATH environment variable, followed by the current folder.  The following example demonstrates multiple scripts using another as a function library:

File 1: /boot/emmc_common.sh

#!/bin/sh

 

flash_led() {

    # TODO – make the specified LED blink

}

 

read_sdcard_image() {

    # TODO – read filesystem image from the SD card

}

 

read_recovery_image() {

    # TODO – read filesystem image from the recovery section

}

 

write_emmc_image() {

    # TODO – write the loaded image to onboard memory

}

File 2: /boot/flash_emmc.sh

#!/bin/sh

 

source /boot/emmc_common.sh

 

flash_led 0

read_sdcard_image

flash_led 1

write_emmc_image

 

echo “done.”

File 3: /boot/recover_emmc.sh

#!/bin/sh

 

source /boot/emmc_common.sh

 

flash_led 0

read_recovery_image

flash_led 1

write_emmc_image

 

echo “done.”