简体   繁体   中英

How to invoke functions between two shell scripts?

I have functions to logInfo()/logError() in a shell script (logger.sh). There are other shell scripts (example: createuser.sh) which require logging. How to invoke functions like logInfo() from createuser.sh

Without function invocations, these logInfo/logError functions are getting copied to every shell script that requires logging.

Put your logger functions in a separate file (that only contains functions, no commands), say myfuncs.sh. Then in any other script that needs these functions, somewhere near the top of that script add a line:

. myfuncs.sh

or, equivalently:

source myfuncs.sh

The functions in myfuncs.sh will then be available in that script.

If the only thing in the logger.sh script is the functions (ie: nothing runsfid you execute it from the command line, then you can source the shell script by including the line:

. logger.sh

See: https://ss64.com/bash/source.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM