Two scripts that each keep a copy of a function get out of step. Put the function in one file, a library, and load it with source FILE. source runs the file's lines in the current shell, so its functions stay defined:
learner@reef:~$ cat lib/say.sh
warn() { ← only functions: no shebang, no chmod
echo "WARNING: $1"
}
learner@reef:~$ cat fridge-check.sh
#!/bin/bash
source ~/lib/say.sh ← load the library, then use its functions
warn "north fridge at 9C"
learner@reef:~$ ./fridge-check.sh
WARNING: north fridge at 9CSource the full path, ~/lib/say.sh: a path like lib/say.sh only works from your home folder.
~/open.sh and ~/restock.sh each have their own copy of log, which writes a line with the time.
- Make the folder:
mkdir ~/lib. - Move
loginto~/lib/log.sh. - In
open.sh, delete its ownlog() { … }and putsource ~/lib/log.shwhere it was. Run./open.sh north. - Do the same in
restock.sh, and run./restock.sh eggs 60.
Objectives
~/lib/log.shdefineslog.open.shuses the library, and has nologof its own.restock.shdoes the same.