Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a foo.sh file in my current directory. If I try to run ./foo.sh, I get:

-bash: ./foo.sh: /bin/sh: bad interpreter: Permission denied

But if I run /bin/sh ./foo.sh then it runs fine.

How can I fix this so I can just run ./foo.sh and it automatically runs it with /bin/sh?

Edit: Okay, this is Chrome OS and this particular folder is mounted with noexec. Apparently that foils the ability to just run ./foo.sh; but why? Why can I still run sh foo.sh to achieve the exact same thing? What security, then, does noexec give?

share|improve this question

3 Answers 3

up vote 9 down vote accepted

The noexec flag will appropriately apply to scripts, because that would be the "expected" behavior.

However, setting noexec only stops people who don't know enough about what they're doing. When you run sh foo.sh you're actually running sh from its default location (probably /bin) which is not on a filesystem mounted with noexec.

You can even get around noexec for regular binary files by invoking ld directly.

cp /bin/bash $HOME
/lib/ld-2.7.so $HOME/bash

This will run bash, regardless of wether or not it's on a filesystem mounted with noexec.

share|improve this answer
1  
+1 for mentioning ld.so (clever) –  amphetamachine Feb 3 '11 at 3:59
    
I tried your two commands; "cannot open shared object file: No such file or directory" - due to sh being copied but bash being run. So then I tried /lib/ld-2.10.1.so $HOME/sh and it returned another error while loading shared libraries: /home/chronos/user/sh: failed to map segment from shared object: Operation not permitted. I'm not sure if what you said was untrue, or if something else is interfering. For example, / is mounted as read-only. –  Ricket Feb 3 '11 at 4:06
    
Well, I couldn't say for certain because I don't have a copy of ChromeOS to try with. I'm fairly confident that it can work with some modification but without being able to try it myself I don't know what that might be. –  bahamat Feb 3 '11 at 15:21
    
Oh well, I'd like to think that it's because Chrome OS is properly locked down. It does seem to be pretty secure but I guess we'll see over time! –  Ricket Feb 4 '11 at 4:09

You can also get this error (or a very, very similar message) if you try to execute a file with MS-DOS 2-byte (carriage-return linefeed) line endings.

Vim is so smart these days, that it doesn't neccessarily show you the carriage returns as '^M'. So you can get fooled if you don't check what Vim thinks the "file format" is and just rely on the on-screen-appearance.

In this case the "#!/bin/sh^M" causes the kernel to try to find "/bin/sh^M", which it can't. Bad interpreter, indeed.

share|improve this answer

If you have the option to run the script or program from a USB stick (or other removable media), you can try unmounting and manually re-mounting it:

  1. Plug in USB stick

  2. Find USB stick device with $ mount

  3. Take note of it; let's assume it is /dev/sdb1

  4. Unmount USB stick:

    $ cd /media/removable
    
    $ sudo umount mountpoint
    

Finally, re-mount USB stick:

$ sudo mount /dev/sdb1 mountpoint

With mountpoint the USB stick's mount name

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.