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
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.
/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.
/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.
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 ...
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With