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"
It checks whether you have permission to
read 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
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
$ [ -r /etc/os-release ] && . /etc/os-release $ printf '%s\n' "$PRETTY_NAME" Debian GNU/Linux 8 (jessie)
PRETTY_NAME was defined in
$ head -n 1 /etc/os-release PRETTY_NAME="Debian GNU/Linux 8 (jessie)"