Running shell script

Discussion in 'Mac Programming' started by helpmeman, Mar 11, 2011.

  1. helpmeman macrumors newbie

    Joined:
    Feb 20, 2011
    #1
    I have a file that I am trying to execute, but every time I try I get the following message: -bash: ./compile-scripts.sh: /bin/sh^M: bad interpreter: No such file or directory

    This first line of the script I am trying to run is: #!/bin/sh

    Any suggestions?
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    You many want to make sure that the file uses unix line termination. The ^M seems to imply that it may have been saved on a DOS/Windows PC with CR/LF line termination.

    CR=^M LF=^J.
    http://en.wikipedia.org/wiki/Carriage_return
    http://en.wikipedia.org/wiki/Line_feed

    B
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    What is the source of this file? Looks like it came from a DOS-y system. You can remove them like this:
    tr -d '\r' < mySweetFile.sh > mySweetFileWithoutCRs.sh

    -Lee
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    FWIW a strange habit I have.

    I use zip/unzip to normalize line terminations.

    I always have a copy of info-zip's implementation handy and converting an entire folder tree to the appropriate termination is as easy as

    Code:
    zip -mr zipname folder
    unzip -a zipname 
    B
     
  5. helpmeman thread starter macrumors newbie

    Joined:
    Feb 20, 2011
    #5
    It came from a DOS-y system I believe. I removed the CRs but it still didn't work. So I went through my /usrs/bin and I couldn't find sh (althought I found ssh)
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    As the error message implies it should be in /bin not /usr/bin or /usrs/bin or /users/bin.

    Open terminal and type:

    Code:
    which sh
    EDIT: Also check the contents of the file /etc/shells. Is /bin/sh listed?

    How are you trying to launch your script?

    B
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Didn't work the same way, or didn't work in a different way?

    The same way would mean you're getting the same error message that contains ^M. This suggests that you didn't actually remove the CRs from the file. The command line lee1210 gave produces a new file without CRs. The original file is unchanged.

    A different way would mean you're getting a different error message. Post that error message.
     
  8. helpmeman thread starter macrumors newbie

    Joined:
    Feb 20, 2011
    #8
    Same way.

    I am using terminal and I typed which sh and it returned /bin/sh and it was also listed in /etc/shells
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Post the output of this command sequence:
    Code:
    head -n 5 ./compile-scripts.sh | hexdump -C
    
    Next, post the output of the same command sequence, but substitute the filename you used for mySweetFileWithoutCRs.sh in Lee's example.

    Also post the output of this command:
    Code:
    ls -l
    
    You can copy and paste commands into a Terminal window. Don't type them in manually.

    If there are any errors, post the actual command you used, along with the actual error message.
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    That is actually why I started using the zip/unzip method especially when dealing with an entire folder or tree of mixed source files and binary files. It gives me the backup piece of mind and replaces the file(s) with the same name in the same place.

    helpmeman it's helpful to actually copy and paste the command you entered and the output you receive in a code block. It may look like the same error to you, but not to us.

    B
     
  11. sero macrumors member

    Joined:
    Aug 28, 2008
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    http://www.thefreecountry.com/tofrodos/ FWIW.

    Sure, if you do this much. I do it rarely these days, so rely on other pre-installed or more used tools.

    B
     

Share This Page