Reputation: 821
I have a bunch of Bash scripts and they each make use of the following:
BIN_CHATTR="/usr/bin/chattr"
BIN_CHKCONFIG="/sbin/chkconfig";
BIN_WGET="/usr/bin/wget";
BIN_TOUCH="/bin/touch";
BIN_TAR="/bin/tar";
BIN_CHMOD="/bin/chmod";
BIN_CHOWN="/bin/chown";
BIN_ECHO="/bin/echo";
BIN_GUNZIP="/usr/bin/gunzip";
BIN_PATCH="/usr/bin/patch";
BIN_FIND="/usr/bin/find";
BIN_RM="/bin/rm";
BIN_USERDEL="/usr/sbin/userdel";
BIN_GROUPDEL="/usr/sbin/groupdel";
BIN_MOUNT="/bin/mount";
Is there a way I could just wget a Bash script with global settings like that and then include them in the script I want to run?
Upvotes: 3
Views: 3770
Reputation: 1160
Yes you can. Just add your variables and functions to a file, make it executable and "execute" it at the top of any script that needs to access them. Here's an example:
$ pwd
/Users/joe/shell
$ cat lib.sh
#!/bin/bash
BIN_WGET="/usr/bin/wget"
BIN_MOUNT="/bin/mount"
function test() {
echo "This is a test"
}
$ cat script.sh
#!/bin/bash
. /Users/joe/shell/lib.sh
echo "wget=$BIN_WGET"
test
$ ./script.sh
wget=/usr/bin/wget
This is a test
Upvotes: 2
Reputation: 1837
are you looking for the source command?
mylib.sh:
#!/bin/bash
JAIL_ROOT=/www/httpd
is_root(){
[ $(id -u) -eq 0 ] && return $TRUE || return $FALSE
}
test.sh
#!/bin/bash
# Load the mylib.sh using source comamnd
source mylib.sh
echo "JAIL_ROOT is set to $JAIL_ROOT"
# Invoke the is_root() and show message to user
is_root && echo "You are logged in as root." || echo "You are not logged in as root."
btw - use rsync -a to mirror scripts with +x flag.
Upvotes: 2
Reputation: 6784
You can keep your variables in a shell script and then source that file:
source /path/to/variables.sh
You should actually use .
which in bash is the same thing as source but will offer better portability:
. /path/to/variables.sh
Upvotes: 3
Reputation: 249253
Yes, you can put all those variables in a file like "settings.sh" and then do this in your scripts:
source settings.sh
Upvotes: 7