ソースが「バイナリファイルを実行できません」というエラーを表示するのはなぜですか


10

tmuxセッションを初期化し、いくつかのウィンドウを作成する小さなファイルがあります。いくつかのデバッグと微調整の後、テキストファイルの名前を(tmuxコマンドを使用して)からに変更spamするまで問題なく動作しましたxset

$ source xset
bash: source: /usr/bin/xset: cannot execute binary file

これでファイルの名前が変更され、source spam再び機能しましたが、これがなぜなのか疑問に思っています。ファイルは、ではなく、ホームディレクトリにあり/usr/binます。


と呼ばれるバイナリがありxsetます。お試しくださいsource ./xset
Faheem Mitha、2014

回答:


11

bashスラッシュ(存在しない限り、内部コマンドソースは、まず、PATHでファイル名を探し/、ファイル名では)。xsetPATH内の実行可能ファイルなので、問題があります。

以下を使用してsource ./xset、sourcepathオプションを実行またはオフに変更できます。

shopt -u sourcepath

bashマンページ:

      source filename [arguments]
          Read and execute commands from filename  in  the  current  shell
          environment  and return the exit status of the last command exe
          cuted from filename.  If filename does not contain a slash, file
          names  in  PATH  are used to find the directory containing file
          name.  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  posi
          tional  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.

5

sourceコマンド意志

現在のシェルコンテキストのfilename引数からコマンドを読み取り、実行します。filenameにスラッシュが含まれていない場合、PATH変数はfilenameの検索に使用されます

この動作.、POSIXによって(のエイリアスとして)定義されています。どうして?ええと、ソース可能な構成スクリプトを内部に配置してPATH、修飾パスなしでそれらにアクセスできます。必要なファイルにアクセスするには、代わりに絶対パスまたは相対パスを指定します。

source ./xset
source ~/xset
source /home/shawn/xset

上記のすべてが当初の期待どおりに機能します。また、無効にすることができsourcepathshopt

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.