i was just looking into a script i find this line. can anyone tell me what this line do. this is the code line.
[ -r "$PosConfigDir/posconfig.sh" ] && . "$PosConfigDir/posconfig.sh"
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIt checks whether you have permission to r
ead the file $PosConfigDir/posconfig.sh
, after symlink resolution, then sources that file.
It does not guarantee that you can read the file. For example, on most systems, [ -r / ]
would return true but you cannot read /
.
-r
is a standard test operator.
.
is a special builtin command of Bourne-like shells that tells the shell to read and interpret the code in the file given as argument. source
is the equivalent command in csh
. Many modern Bourne-like shells understand source
as an alias for .
.
With some shell implementations, the failure of .
to open or read the file would cause the shell to abort (after having output an error message). The -r
check is presumably meant to prevent that in that case.
To prevent the shell from aborting, in POSIX shells, an alternative could be
command . "$PosConfigDir/posconfig.sh"
(Though the above construct won't work in zsh
unless in sh
or ksh
emulation)
To avoid the error message, you'd need to redirect the stderr output to /dev/null
, but that would have the unwanted side-effect of also silencing the errors of the commands in postconfig.sh
.
An example:
$ [ -r /etc/os-release ] && . /etc/os-release
$ printf '%s\n' "$PRETTY_NAME"
Debian GNU/Linux 8 (jessie)
PRETTY_NAME
was defined in /etc/os-release
file:
$ head -n 1 /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
-r
only checks that you have permission to read the file (after symlink resolution). That doesn't guarantee that you'll be able to read it. For instance, [ -r / ]
returns true, but on most systems, you won't be able to read(2)
/
(.
would still fail).
Mar 7, 2016 at 13:00
command .
does not work with zsh
unless in sh
or ksh
emulation.