用于 。在 bash

Ble*_*ley 3 bash

#!/usr/bin/env bash

scriptdir="$HOME"
touch $scriptdir/foo.txt
. "$scriptdir/foo.txt" ||
{ echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}
echo "$scriptdir/foo.txt is present"
echo
rm "$scriptdir/foo.txt"
. "$scriptdir/foo.txt" ||
{ echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}
Run Code Online (Sandbox Code Playgroud)

我不明白.in的用法. "$scriptdir/foo.txt" ||

它的功能似乎与 类似if [ -f "$scriptdir/foo.txt ],是吗?

这样的

scriptdir="$HOME"
touch $scriptdir/foo.txt
 if [ -f "$scriptdir/foo.txt" ] ; then
   echo
 else
 { echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}
 fi
Run Code Online (Sandbox Code Playgroud)

产生类似的结果。

有人可以详细说明.这里的用法吗?

如果我在 foo.txt 中编写脚本,那么该脚本可能会运行,因为.会导致文件执行,而不仅仅是查看它是否存在?而且,只要$scriptdir/foo.txt存在且可执行,那么右半部分||将永远不会运行,因为左半部分返回零退出状态?

Spa*_*sle 6

.是 bash 内置的,与 相同source,它的作用是从当前 shell 中的 FILENAME 读取和执行命令。键入help .help 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.
Run Code Online (Sandbox Code Playgroud)