After upgrading to 11.2, I found that the shell script will not able to find other included shell scripts unless the path of the included shell scripts are specified.
eg.
[original]
. test.sh
[now]
. ./test.sh
The BASH version in 11.2 is
GNU bash, version 4.0.33(1)-release (i586-suse-linux-gnu)
Is ‘test.sh’ in your path? If it is not then that should not work, but if
it is then I imagine it should work. Compare where it is (post its
location in the directory structure) with your environment’s path:
echo $PATH
Good luck.
knightchang wrote:
> Dear All,
>
> After upgrading to 11.2, I found that the shell script will not able to
> find other included shell scripts unless the path of the included shell
> scripts are specified.
>
> eg.
> [original]
> . test.sh
>
> [now]
> . ./test.sh
>
> The BASH version in 11.2 is
> GNU bash, version 4.0.33(1)-release (i586-suse-linux-gnu)
>
> Does anybody encounter this problem too?
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
Depending on the mode it should find the script in the current directory as well, even if it is not in the PATH. The relevant snippet from the man page:
If filename does not contain a slash, file names in PATH are used to find the directory containing filename. 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.
I just tried this in 10.3 and 11.2. Both work as expected.
The construction
. test
is the same as
source test
From man bash:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing filename. 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 positional 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.
So, is bash in posix mode? I see how to load it that way but haven’t seen
how to verify an existing shell is or isn’t. Either way using at least ./
should probably be used to prevent unexpected behaviors.
Good luck.
vodoo wrote:
> Depending on the mode it should find the script in the current directory
> as well, even if it is not in the PATH. The relevant snippet from the
> man page:
>
>> If filename does not contain a slash, file names in PATH are used to
>> find the directory containing filename. 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.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
Not in my 11.2 install. As said they work the same with me in 10.3 and 11.2, without the ./ .
I only hope the OP can find something with this info. in mind. Is he in Posix mode for some reason? I would not even know how to reach Posix mode
Many thanks for your prompt response to my problem.
Let me detail the script I am testing.
test.sh
#!/bin/sh
echo $PATH
echo pwd
source config.sh
config.sh
BUILD_LLAL=LLAL
I am very sure test.sh and config.sh are existing in the same directory and /bin/sh or /usr/bin/sh both are linked to /bin/bash. And then I execute the following command
./test.sh
It returns me the following messages and error.
/home/abc/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/kde3/bin:/usr/lib/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
/home/abc
./test.sh: line 4: source: config.sh: file not found
No matter what I use “.” or “source” to include another script, I always get the error.
After changing the shebang from #!/bin/sh to #!/bin/bash as hcw mentioned, it works.
After google-ing this problem, I found some explanations.
When invoked as sh, BASH enters POSIX mode after reading
the startup files. I don’t have any idea if it is a bug
in the previous version (likely in version 3.x), BASH will
search the current directory even it is in POSIX mode.
But in the version after 4.0, this behavior is corrected.
The .' and source’ builtins do not search the current
directory for the filename argument if it is not found by
searching `PATH’.
I think this problem is solved.
Many thanks to you.
boven:/bin # ls -l ex vi vim
lrwxrwxrwx 1 root root 3 Mar 11 2009 ex -> vim
lrwxrwxrwx 1 root root 3 Mar 11 2009 vi -> vim
lrwxrwxrwx 1 root root 21 Mar 11 2009 vim -> /etc/alternatives/vim
boven:/bin # l /usr/bin/view
lrwxrwxrwx 1 root root 3 Mar 11 2009 /usr/bin/view -> vim*
boven:/bin #
Let us take the vi/ex example. Apperently when one calls either vi or ex the same program will be started but when you do this you will see a different behaviour. How is this achieved? The program itsef tests on the value of argument 0 (zero) to see how it is provoked. In the case of
vi -e file
the respective arguments are:
0: vi
1: -e
2: file
In a bash script you have access to them using ${0}, ${1} and ${2} (or $0, $1 and $2 when you are in a hurry).
Thus the program knows how it is called and can behave accordingly.
Now we know how it is possible that sh and bash behave different in spite of being the same execitable file.
What did knightchang do? He wrote a sh (Posix shell) script. That is proven by the fact that he started it with
#!/bin/sh
This means of course that he should have written this with all the syntactic and other rules of the Posix shell in mind (who does ). He made a mistake in forgetting about the rules used during ‘sourcing’. And yes, seamingly there is a bug in an earlier version of that shell,: it behaves as if it is the Borne Again shell.
Now there is a change that knightchang in fact tried to write a flawless Borne Again shell cript. In which case his only error was the wrong shebang.
There is also the change that he mixe up the rules for bash and sh in his script, in which case he has to decide what language he wants to use and he has to correct his program so it conforms to the chosen language.
The word ‘shebang’.
I used the word ‘shebang’ because I hoped that everybody writing scripts would know what it is and how important it is. About the etymology of shebang I refer to the links given above (Wikipedia) for what it is worth. (I always thought that it was the #! that made the sound)