452

I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error

[root@localhost lpng142]# ./configure
bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory
[root@localhost lpng142]# 

How do I fix this? The /etc/fstab file:

#
# /etc/fstab
# Created by anaconda on Wed May 26 18:12:05 2010
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=ce67cf79-22c3-45d4-8374-bd0075617cc8 /boot                   ext4    
defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
Henke
  • 4,445
  • 3
  • 31
  • 44
Vineeth Pradhan
  • 8,201
  • 7
  • 33
  • 34
  • 3
    thanks, dos2unix save my day. for osx user, it's getting easier. brew install dos2unix [dos2unix](http://i.stack.imgur.com/VgHzz.png) – Arejae Oct 10 '15 at 02:54
  • 6
    VIM :set fileformat=unix – PodTech.io Jun 22 '16 at 12:41
  • @tripleee Help me understand why you're marking this question as duplicate. This question was asked 7 years ago and the linked question was asked a year ago. It should be the other way around – Vineeth Pradhan Sep 25 '17 at 09:55
  • 2
    Age of the question is generally not a factor when deciding duplicates. A general, focused question which is easy to find in Google is usually a better duplicate target than an obscure, specialized question where the answer happens to be fairly general. – tripleee Sep 25 '17 at 10:12
  • 2
    @tripleee Not convincing enough because it's much easier for any user to copy-paste the error log onto web search – Vineeth Pradhan Sep 26 '17 at 03:50
  • 3
    If you genuinely think the duplicate should be the other way around, please bring it up on [meta]. For the record, the "bad interpreter" symptom is one of many possible symptoms, and the details about `configure` and `fstab` in this question are distracting. If merging questions were easier, I would be happy to merge a couple of the answers here to the master duplicate, though. – tripleee Sep 26 '17 at 04:43
  • 1
    Just wanted to say "thank you" for this post. I typed the error into Google and this came up instantly. Without it, I would have never guessed what was wrong. I would never in a million years expect that the Unix Makefiles provided by ILJ would have Windows line characters in them when no prior versions did. So I'd never even consider that was the issue. – user2465201 Mar 28 '22 at 02:01

15 Answers15

871

To fix, open your script with vi or vim and enter in vi command mode (key Esc), then type this:

:set fileformat=unix

Finally save it

:x! or :wq!

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
polymame
  • 8,726
  • 1
  • 15
  • 2
384

Looks like you have a dos line ending file. The clue is the ^M.

You need to re-save the file using Unix line endings.

You might have a dos2unix command line utility that will also do this for you.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Richard
  • 9,740
  • 2
  • 23
  • 15
  • 1
    Yes, the ^M kinda seemed weird. I did see this dos2unix configure suggestion somewhere in the net, but didn't wanna run into other problems. Okay, fingers crossed, i'll give it a try :) Thanks for the quick replies, Konerak and Richard – Vineeth Pradhan May 27 '10 at 12:30
  • Thank you! I couldn't figure out why my cron job stopped working... That fixed it, but I don't remember editing it in Windows. – Alex K Oct 26 '12 at 19:24
  • 15
    If the project you are trying to build has many scripts that require conversion (this is the case with Qt) applying dos2unix recursively may be a good idea. Running "find . -type f | xargs dos2unix" from the top level directory will do it for you. – Jeff Trull Jan 04 '14 at 16:17
  • 2
    Freakin Qt configuration files (building from source here) have this garbage. *sigh* Really useful tool that saves a lot of manual labour. Then again why one should do that when downloading the sources of a library that is shipped with "everywhere" in the name of the ZIP file is beyond me. – rbaleksandar Aug 20 '15 at 23:51
  • 11
    `dos2unix` fixed it... ps: `yum install dos2unix` if required – Sebas Sep 16 '15 at 21:22
  • 4
    Excellent ! For Sublime Text 3 users, this is fixed by choosing View -> Line Endings -> Unix. – weezilla Mar 11 '16 at 01:36
  • Install with `sudo apt-get update` then `sudo apt-get install dos2unix` on Debian systems, such as Ubuntu, Xubuntu, Mint, Raspbian, etc. – Gabriel Staples Mar 27 '17 at 17:15
130

Or if you want to do this with a script:

sed -i 's/\r//' filename
Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
  • 8
    +1, worked for me on debian linux – dbjohn Dec 17 '12 at 20:13
  • 4
    This is simple, but only works with _GNU_ `sed`. With BSD/macOS `sed`, you need to use `sed -i '' $'s/\r//' filename` (from `bash`), or, slightly more robust: `sed -i '' $'s/\r$//' filename` – mklement0 Dec 12 '16 at 14:53
51

Your configure file contains CRLF line endings (windows style) instead of simple LF line endings (unix style). Did you transfer it using FTP mode ASCII from Windows?

You can use

dos2unix configure

to fix this, or open it in vi and use :%s/^M//g; to substitute them all (use CTRL+V, CTRL+M to get the ^M)

mklement0
  • 382,024
  • 64
  • 607
  • 775
Konerak
  • 39,272
  • 12
  • 98
  • 118
  • 2
    Nope, I did use linux to download the .gz file. No idea how CRLF got there. – Vineeth Pradhan May 27 '10 at 12:33
  • 2
    vim search and replace that's easier to type: `%s/\r$//g` – glenn jackman May 27 '10 at 15:35
  • 1
    On some system (e.g. recent Debian / Ubuntu) is the binary called fromdos, not dos2unix. – pevik Jan 17 '14 at 18:12
  • Just wanted to comment that I just downloaded the original jpeg9-e zip direct from the JPEG website, and it had all the ^Ms in it, rendering the configure and makefiles useless without conversion. The error wasn't on the OP's end. Someone at IJG must have edited the files and not noticed they became incompatible with Unix. This is not the case with the jpeg 8x files. – user2465201 Mar 28 '22 at 01:55
30

You can use following command to fix

cat file_name.sh | tr -d '\r' > file_name.sh.new
wu liang
  • 1,993
  • 18
  • 12
16

If you could not found run the command,

CentOS:

# yum install dos2unix*

# dos2unix filename.sh
dos2unix: converting file filename.sh to Unix format ...

Ubuntu / Debian:

# apt-get install dos2unix
Lakshmikandan
  • 4,301
  • 3
  • 28
  • 37
13

This usually happens when you have edited a file from Windows and now trying to execute that from some unix based machine.

The solution presented on Linux Forum worked for me (many times):

perl -i -pe's/\r$//;' <file name here>

Hope this helps.

PS: you need to have perl installed on your unix/linux machine.

AUR
  • 623
  • 7
  • 14
7

Thanks to pwc101's comment on this post, this command worked in Kali Linux .

sed -i s/{ctrl+v}{ctrl+m}// {filename}

Make sure you replace the bits in brackets, {}. I.e. {ctrl+m} means press Ctrl key and the M key together.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
br3nt
  • 9,017
  • 3
  • 42
  • 63
6

Following on from Richard's comment. Here's the easy way to convert your file to UNIX line endings. If you're like me you created it in Windows Notepad and then tried to run it in Linux - bad idea.

  1. Download and install yourself a copy of Notepad++ (free).
  2. Open your script file in Notepad++.
  3. File menu -> Save As ->
  4. Save as type: Unix script file (*.sh;*.bsh)
  5. Copy the new .sh file to your Linux system
  6. Maxe it executable with: chmod 755 the_script_filename
  7. Run it with: ./the_script_filename

Any other problems try this link.

zuallauz
  • 4,328
  • 11
  • 43
  • 54
  • 3
    There's also a setting in Notepad++ to set the default line ending, goto: "Preferences->New Document->Format" and select Unix. One caveat though: that is only the default for new files, it won't change the format if it detects something else on existing files. – Matthew Wilcoxson Jan 27 '14 at 14:17
  • 1
    You can switch line endings with the Replace functionality in NOtepad++. Switch on "Exteneded" search mode and enter "\r\n" in the find and "\n" in the replace. Click Replace All! – Matthew Wilcoxson Jan 27 '14 at 14:19
  • 3
    To fix in **Notepad++** just right click **DOS\Windows** on the bottom right, and choose **UNIX/OSX Format** – Alaa M. Jul 05 '18 at 05:34
6

When you write your script on windows environment and you want to run it on unix environnement you need to be careful about the encodage :

dos2unix $filePath

Youssouf Maiga
  • 6,701
  • 7
  • 26
  • 42
6

Just adding sh before script name make it work in my case.

SkorpEN
  • 2,491
  • 1
  • 22
  • 28
  • 1
    Thank you so much for posting this. None of all these dos2unix / line-ending-fix solutions worked for me. I finally got it to run by doing `sudo sh ./MyScript.sh`. Done on RedHat 6 / RHEL 6 – velkoon Oct 11 '17 at 20:36
6

If you're on OS X, you can change line endings in XCode by opening the file and selecting the

View -> Text -> Line Endings -> Unix

menu item, then Save. This is for XCode 3.x. Probably something similar in XCode 4.

Vern Jensen
  • 3,449
  • 6
  • 42
  • 59
  • Or use an editor like TextWrangler (Dropdown in status bar) or Sublime (View > Line Endings > Unix) to do the same thing. – Al. Apr 18 '19 at 12:34
5

If you are using TextMate or a similar programme, do save as, and then in encodings choose LF instead of CRLF.

patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
4

Use the dos2unix command in linux to convert the saved file. example :

dos2unix file_name
lxg
  • 12,375
  • 12
  • 51
  • 73
srpatch
  • 407
  • 5
  • 4
3

You can also do this in Kate.

  1. Open the file
  2. Open the Tools menu
  3. Expand the End Of Line submenu
  4. Select UNIX
  5. Save the file.
Tjaart
  • 3,912
  • 2
  • 37
  • 61