You are not logged in.
This simple script:
!/bin/bash
CONFIG="config.conf"
[ -f "$CONFIG" ] && source "$CONFIG"
runs fine when ran like
$ ./t.sh
or
$ bash t.sh
but borks when ran like so:
$ ls -l
-rw-r--r-- 1 oscar oscar 31 Jun 20 11:21 config.conf
-rwxr-xr-x 1 oscar oscar 72 Jun 20 11:21 t.sh
$ sh t.sh
t.sh: line 4: source: config.conf: file not found
As you can see from the ls output above, the config.conf file exists and is readable. Furthermore, I get the same result when using "." instead of "source". What intrigues is that /bin/sh is a sym link to /bin/bash (and /bin/bash is a binary.), so both bash and sh should produce the same result. Why is that not the case is the issue that puzzles me.
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May 8 02:46 /bin/sh -> bash
Last edited by gauthma (2011-06-20 10:55:52)
Offline
From the manpage:
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe‐
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file‐
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi‐
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.
combined with:
If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well.
aka, bash invoked as sh puts it in POSIX mode, and in POSIX mode the current path isn't searched
Last edited by Spider.007 (2011-06-20 10:37:48)
Offline
Thanks for the quick reply. I had no manpage for "source", and I didn't remember to look it up on the bash manpage :$
Offline
Thanks for the quick reply. I had no manpage for "source"
[karol@black ~]$ help source
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
Offline
gauthma wrote:Thanks for the quick reply. I had no manpage for "source"
[karol@black ~]$ help source source: source filename [arguments]
I even did source --help, and I didn't remember this one. Ouch.
Last edited by gauthma (2011-06-20 11:43:48)
Offline