Linux shell scripting: /bin/sh : bad interpreter: No such file or directory

January 6, 2012 at 11:57 am 2 comments


I usually get the below message when creating a Linux shell script with text editors like Notepad or Wordpad on Windows PC and executing it on Linux box:
 
“: /path/to/scriptfile:/bin/sh^M: bad interpreter: No such file or directory”
 
The top of the script is a line specifying the shell program by which the script is interpreted: 
 
#!/bin/sh
 
Double-check if there is any typo in the script file or the /bin/sh exists on the Linux machine. It sounds like everything is OK. Now, I am sure the error caused by the text editor used to create the script file. In fact, text editors of different platforms like Mac, PC, Linux can create text files with different formats and this is the reason of the error.  In our case, line ending characters by the text editor on Windows are different from the ones on Linux which cause the shell scripted not be interpreted in a right way. To fix the error, do following steps at Linux command line:
  • Open the script using vi program

vi /path/to/scriptfile

  • When file is opened, change the format of the script to the right one by entering

: set fileformat=unix

or just simply

: set ff=unix

  • Finally, save the script for the new format taking effect and exit the vi program by entering

: x

Now, the script is working fine without any error. An easy way this avoid this problem is creating scripts directly on Linux with vi program.
 
Please note that using text files of another operating system on Linux may result in similar problems. For example, some errors when compiling your source code files or a program can not read data text files. These problems can be solved using the solution in this post.
 
Advertisements

Entry filed under: Linux. Tags: , , , , , , , , , .

How to fix: locate: can not open `/var/lib/mlocate/mlocate.db’: No such file or directory Facebook Shakes Hardware World With Own Storage Gear

2 Comments Add your own

  • 1. AbdulHafeez  |  January 5, 2013 at 12:45 am

    thanks very much for the info, the problem i have with this is solved with the dos2unix command

    Reply
  • 2. poker online  |  July 2, 2015 at 9:10 pm

    i cant use dos2unix command, have new solusion?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

/openmp AMD app fixing dead pixel iphone C++ cmd program CPU C sharp dead pixels directory download ibm synthetic data generator Dynamic Dynamic Memory Allocation Edit Environment Variables in Windows Environment Variables error lnk2019: unresolved external symbol _getprocessmemoryinfo@12 referenced in function error lnk2019: unresolved external symbol _ getprocessmemoryinfo@12 referenced in function "void __cdecl printmemoryinfo(unsigned long example code Fixing Dead Pixels and Gray Lines on the iPhone Screen Fixing Dead Pixels on the iPhone Screen Fixing Gray Lines on the iPhone Screen Fix iPhone getprocessmemoryinfo GPU Gray Line iPhone Screen gray lines gray pixels green pixels GUI how to "new" a two-dimension array in C++ how to use ibm quest synthetic data generator ibm data generator ibm quest data generator ibm quest data generator exe ibm quest data mining project ibm quest market-basket synthetic data generator ibm quest market basket market-basket synthetic data generator ibm quest synthetic data generator ibm quest synthetic data generator linux ibm synthetic data generator ibm synthetic generator Intel iPhone Iphone 3G iPhone 3GS iPhone 4 iphone gray lines on startup iphone pixel damage iPhone Screen iPhone screen damage Linux market-basket synthetic data generator Memory Allocation Multicore multithread multi thread multi threaded multithreading mysql extract data into file new OpenCL Path processor quest data generator quest synthetic data generator R SAS Set Environment Variables Set Environment Variables in Windows souce code source code stuck pixels system file two dimension array Windows 7 Windows Vista

%d bloggers like this: