使用。 bashで[複製]

使用。 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 ;}

.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

同様の結果が出ます。

.誰かがここで何が使われたかを詳しく説明してもらえますか?

foo.txtにスクリプトを書くと、.単にファイルが存在することを確認するのではなく、ファイルが実行されるため、そのスクリプトを実行できますか?そして$scriptdir/foo.txtそれが存在し、実行可能な限り、||左半分は終了状態0を返すので、右半分は絶対に実行されません。

ベストアンサー1

.と同じbash組み込みコマンドであり、sourceその機能は現在のシェルの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.

おすすめ記事