bash shell 脚本:错误的解释器:使用密码时没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15735541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:31:54  来源:igfitidea点击:

shell script: bad interpreter: No such file or directory when using pwd

bashunixpwd

提问by Alek

I want to go through the files in a directory with a for loop but this comes up.

我想用 for 循环遍历目录中的文件,但出现了这个问题。

echo: bad interpreter: No such file or directory

code:

代码:

#!/bin/bash
count=0
dir=`pwd`
echo "$dir"
FILES=`ls $dir`
for file in $FILES
do
 if [ -f $file ]
 then
  count=$(($count + 1))
 fi
done
echo $count

采纳答案by Gilles Quenot

Better do :

最好这样做:

#!/bin/bash
count=0
dir="$PWD"
echo "$dir"

for file in "$dir"/*
do
 if [[ -f $file ]]
 then
  ((count++))
 fi
done
echo $count

or a simplest/shortest solution :

或最简单/最短的解决方案:

#!/bin/bash

echo "$PWD"

for file; do
 [[ -f $file ]] && ((count++))
done

echo $count

回答by Marco Navarro

I had the same problem. Removing #!/bin/bashdid the trick for me. It seems that is not necessary to add where bash is located, since it is on the system path.

我有同样的问题。删除#!/bin/bash对我来说很有效。似乎没有必要添加bash所在的位置,因为它在系统路径上。

I found another solution here. Change

我在这里找到了另一个解决方案。改变

#!/bin/bash

#!/bin/bash

for

为了

#!/usr/bin/bash

#!/usr/bin/bash

回答by Ondra ?i?ka

The echo: bad interpreter: No such file or directoryis most likely coming from the first line, #!...which is called shebang line.

echo: bad interpreter: No such file or directory是最有可能从第一行,来#!...这就是所谓家当线

About the #!...line

关于#!...线路

This line hints the shell what interpreter to use to run the file. That can be e.g. bash, or sh(which is (roughly) a subset so a lot of things won't work), or basically anything that can execute the file content - Perl, Python, Ruby, Groovy...

这一行提示 shell 使用什么解释器来运行文件。这可以是例如bash,或者sh(它(大致)是一个子集,所以很多东西都不起作用),或者基本上任何可以执行文件内容的东西——Perl、Python、Ruby、Groovy……

The line points the system in cases like calling the script directly when it's executable:

在脚本可执行时直接调用脚本的情况下,该行指向系统:

./myScript.sh

It is also often used by editors to recognize the right syntax highlighting when the file has no suffix - for instance, Gedit does that.

当文件没有后缀时,编辑器也经常使用它来识别正确的语法突出显示 - 例如,Gedit 会这样做。

Solution

解决方案

To override the line, feed the script to Bash as a parameter:

要覆盖该行,请将脚本作为参数提供给 Bash:

bash myScript.sh

Or, you can 'source' it, which means, from within a Bash shell, do either of

或者,您可以“获取”它,这意味着,从 Bash shell 中,执行以下任一操作

source myScript.sh
. myScript.sh

which will work (roughly) as if you pasted the commands yourself.

这将(大致)工作,就像您自己粘贴命令一样。

回答by fahadash

In my case the bash script was created on a Windows PC which added a carriage return character in front of every line feed. \x0D\x0A instead of just \x0A. I replaced all the CRLF with just LF using the sedand my script works now.

在我的例子中,bash 脚本是在 Windows PC 上创建的,它在每个换行符前面添加了一个回车符。\x0D\x0A 而不仅仅是 \x0A。我用 LF 替换了所有的 CRLF,sed我的脚本现在可以工作了。

sed -i 's//\r/\n//\n/g' /path/to/file.sh

回答by David

I have just come across the same issue and found that my error was in my first line, having

我刚刚遇到了同样的问题,发现我的错误在我的第一行,有

#!bin/bash

instead of

代替

#!/bin/bash

回答by ash

That's a strange error to be getting. I recommend trying to find the source of the error.

这是一个奇怪的错误。我建议尝试找到错误的来源。

One thing is to check the pwd command.

一件事是检查 pwd 命令。

type pwd

Make sure it's /usr/bin/pwd or /bin/pwd, and verify it's not a script:

确保它是 /usr/bin/pwd 或 /bin/pwd,并验证它不是脚本:

file /usr/bin/pwd

If it is a script, I bet it starts with

如果它是一个脚本,我敢打赌它以

#!echo

回答by Mohamed Anees A

If you did use Homebrew to install BASH,

如果您确实使用 Homebrew 安装 BASH,

Removing the #!/bin/bashwill be suffice.

删除 #!/bin/bash将就足够了。

回答by Jyoti Dhiman

You can find where bash is located using command

您可以使用命令找到 bash 所在的位置

whereis bash

and you can copy the bash path to the path where you are seeing bad-interpreter error.

并且您可以将 bash 路径复制到您看到错误解释器错误的路径。

回答by Ren Roz

This issue could also occur when the file is not in unix format.. try running dos2unix againts the file and try again.

当文件不是 unix 格式时也可能发生此问题。尝试再次运行 dos2unix 文件并重试。

回答by Rommi

so, if you change your username group priority from username to root, you should change

因此,如果您将用户名组优先级从 username 更改为 root,则应该更改

#!/user/bin/bash

to

#!/bin/bash

check your user group (my username is rommi)

检查您的用户组(我的用户名是 rommi)

$ groups rommi

this command will output:

此命令将输出:

rommi : root adm cdrom sudo dip plugdev lxd lpadmin sambashare

since my username group priority is set to root, i should change my script to

由于我的用户名组优先级设置为 root,我应该将我的脚本更改为

#!/bin/bash

i change the priority group using:

我使用以下方法更改优先级组:

sudo usermod -g root rommi

if groups rommicommand outputs:

如果groups rommi命令输出:

rommi : rommi adm cdrom sudo dip plugdev lxd lpadmin sambashare

then my script should use #!/usr/bin/bash

那么我的脚本应该使用 #!/usr/bin/bash

fail make this changes will resutl in bad interpreter: No such file or directoryerror

失败进行此更改将导致bad interpreter: No such file or directory错误