Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

unix

pwd

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
like image 295
Alek Avatar asked Mar 31 '13 22:03

Alek


People also ask

How do you resolve a bad interpreter with no such file or directory?

If your system shows a bad interpreter, no such file or directory is an error message probably it is caused by having a file with different line termination or a carriage return character of an operating system on another operating system.

What is bad interpreter error in Linux?

/bin/bash^M: bad interpreter: No such file or directory Basically, the error message says that there is no file named /bin/bash^M. Ok, but there is no ^M in your script! ^M is a character used by Windows to mark the end of a line (so it is a carriage return) and that matches the CR character.

What is !/ Bin bash?

/bin/bash is the most common shell used as default shell for user login of the linux system. The shell's name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax.

What is bad interpreter?

bad interpreter no such file or directory. It is caused by the presence of the Window return character (^M) that is ending the line. This mostly occurs when copying and pasting an unknown source file into the operating system. The window return can be removed by simply executing the command: sed -i -e 's/r$//' filename ...


2 Answers

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

like image 199
Marco Navarro Avatar answered Oct 10 '22 02:10

Marco Navarro


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
like image 26
Gilles Quenot Avatar answered Oct 10 '22 03:10

Gilles Quenot