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

会有一股神秘感。 提交于 2019-12-04 10:13:01

问题


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

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

回答1:


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



回答2:


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

I found another solution here. Change

#!/bin/bash

for

#!/usr/bin/bash




回答3:


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

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...

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.

Solution

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

bash myScript.sh

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

source myScript.sh
. myScript.sh

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




回答4:


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 sed and my script works now.

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



回答5:


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.

type pwd

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

file /usr/bin/pwd

If it is a script, I bet it starts with

#!echo



回答6:


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



回答7:


If you did use Homebrew to install BASH,

Removing the #!/bin/bash will be suffice.




回答8:


You can find where bash is located using command

whereis bash

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




回答9:


I have followed the steps from the following link and issue has been resolved.

Ref link: script error - bad interpreter Actually, there was an extra ^M symbol at the end of each line. So, after the removal of that, it worked fine for me.

Hope it helps!



来源:https://stackoverflow.com/questions/15735541/shell-script-bad-interpreter-no-such-file-or-directory-when-using-pwd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!