#!/bin/bash - そのファイルやディレクトリはありません。

#!/bin/bash - そのファイルやディレクトリはありません。

bashスクリプトを作成しましたが、実行しようとすると、次のメッセージが表示されます。

#!/bin/bash no such file or directory

bash script.sh動作させるには、次のコマンドを実行する必要があります。

この問題をどのように解決できますか?

ベストアンサー1

これらのメッセージは通常、最初の行の終わりの追加のキャリッジリターン、または最初の行の先頭のBOMなどの誤ったshebang行が原因で発生します。

ランニング:

$ head -1 yourscript | od -c

それがどのように現れるかを見てください。

これは間違っています:

0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n

これも間違っています:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n

これは正しいです:

0000000   #   !   /   b   i   n   /   b   a   s   h  \n

これが問題の場合dos2unix(または、、、、sed... )を使用してスクリプトを修正します。trawkperlpython

BOMとテールCRを削除する方法は次のとおりです。

sed -i '1s/^.*#//;s/\r$//' brokenScript

スクリプトの実行に使用されたシェルは、表示されるエラーメッセージに多少の影響を与えます。

以下は、名前(echo $0)と次の各shebang行でのみ表示される3つのスクリプトです。

正しいスクリプト:

0000000   #   !   /   b   i   n   /   b   a   s   h  \n

スクリプトとBom:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n

CRLFを使用したスクリプト:

0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n

Bashで実行すると、次のメッセージが表示されます。

$ ./correctScript
./correctScript
$ ./scriptWithCRLF
bash: ./scriptWithCRLF: /bin/bash^M: bad interpreter: No such file or directory
$ ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom

インタプリタを明示的に呼び出して問題のスクリプトを実行すると、CRLFスクリプトが問題なく実行される可能性があります。

$ bash ./scriptWithCRLF
./scriptWithCRLF
$ bash ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom

これは次の条件で観察される動作ですksh

$ ./scriptWithCRLF
ksh: ./scriptWithCRLF: not found [No such file or directory]
$ ./scriptWithBom
./scriptWithBom[1]: #!/bin/bash: not found [No such file or directory]
./scriptWithBom

そして以下dash

$ ./scriptWithCRLF
dash: 2: ./scriptWithCRLF: not found
$ ./scriptWithBom
./scriptWithBom: 1: ./scriptWithBom: #!/bin/bash: not found
./scriptWithBom

おすすめ記事