Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script and /bin/bash^M: bad interpreter: No such file or directory [duplicate]

Tags:

bash

I'm learning through this tutorial to learn bash scripts to automate a few tasks for me. I'm connecting to a server using putty.

The script, located in .../Documents/LOG, is:

#!/bin/bash
# My first script
echo "Hello World!"

And I executed the following for read/write/execute permissions

chmod 755 my_script

Then, when I enter ./my_script, I'm getting the error given in the title.

Some similar questions wanted to see these, so I think they might help:

which bash

/bin/bash

and

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh

I tried adding current directory to PATH, but that doesn't work..

like image 457
cartonn Avatar asked Jan 08 '13 16:01

cartonn


People also ask

How do I fix bin bash'm a bad interpreter?

To fix the error in the Windows operating system, open the bash script file in the Notepad++ editor and then go to the preferences tab via the settings menu as below. Close the window after choosing Unix/OSX as the format. Afterwards, save and close the file.

What is M in bash?

Show activity on this post. ^M is a carriage return, and is commonly seen when files are copied from Windows.

What is #!/ Bin bash?

#!/bin/bash Essentially it tells your terminal that when you run the script it should use bash to execute it. It can be vital since you may be using a different shell in your machine ( zsh , fish , sh , etc.), but you designed the script to work specifically with bash.

Why is there no such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.


10 Answers

Run following command in terminal

sed -i -e 's/\r$//' scriptname.sh

Then try

./scriptname.sh

It should work.

like image 124
Nivin V Joseph Avatar answered Oct 16 '22 18:10

Nivin V Joseph


I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.

Try running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just rewrite the script in your Unix env using vi and test.

Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.

If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.

In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows. To change it go to

  • settings->preferences
  • new document / default directory tab
  • select the format as unix and close
  • create a new document
like image 20
cowls Avatar answered Oct 16 '22 18:10

cowls


If you use Sublime Text on Windows or Mac to edit your scripts:

Click on View > Line Endings > Unix and save the file again.

enter image description here

like image 27
Mario Campa Avatar answered Oct 16 '22 16:10

Mario Campa


In notepad++ you can set it for the file specifically by pressing

Edit --> EOL Conversion --> UNIX/OSX Format

enter image description here

like image 20
Urik Avatar answered Oct 16 '22 17:10

Urik


This is caused by editing file in windows and importing and executing in unix.

dos2unix -k -o filename should do the trick.

like image 40
StonecoldIM Avatar answered Oct 16 '22 16:10

StonecoldIM


problem is with dos line ending. Following will convert it for unix

dos2unix file_name

NB: you may need to install dos2unix first with yum install dos2unix

another way to do it is using sed command to search and replace the dos line ending characters to unix format:

$sed -i -e 's/\r$//' your_script.sh
like image 24
Md. Shafiqur Rahman Avatar answered Oct 16 '22 18:10

Md. Shafiqur Rahman


Your file has Windows line endings, which is confusing Linux.

Remove the spurious CR characters. You can do it with the following command:

 $ sed -i -e 's/\r$//' setup.sh
like image 22
kalyani chaudhari Avatar answered Oct 16 '22 18:10

kalyani chaudhari


For Eclipse users, you can either change the file encoding directly from the menu File > Convert Line Delimiters To > Unix (LF, \n, 0Α, ¶):

Eclipse change file encoding

Or change the New text file line delimiter to Other: Unix on Window > Preferences > General > Workspace panel:

Eclipse workspace settings

like image 21
Christos Lytras Avatar answered Oct 16 '22 17:10

Christos Lytras


I was able to resolve the issue by opening the script in gedit and saving it with the proper Line Ending option:

File > Save As...

In the bottom left of the Save As prompt, there are drop-down menus for Character Encoding and Line Ending. Change the Line Ending from Windows to Unix/Linux then Save.

Selecting the "Line Ending" option as "Linux/Unix" in the gedit "Save As" prompt

like image 25
NWRichmond Avatar answered Oct 16 '22 17:10

NWRichmond


Atom has a built-in line ending selector package

More details here: https://github.com/atom/line-ending-selector

like image 35
tuanh118 Avatar answered Oct 16 '22 18:10

tuanh118