linux - Shell script not running as by "./" but it runs by "sh"

06
2013-08
  • pRAShANT

    I am running a script on arm target board as a super user, i.e., as a root user. But the script is not executing if i run it with following syntax

    root@freescale$ ./Script.sh 
    -sh: ./Script.sh: Permission denied
    

    I already gave the read and execute permissions(+rx) by using chmod command. On executing "ls -l" command I get following attributes

    root@freescale$ ls -l | grep Script.sh
    -rw-rw-rw-    1 root     root          362 Jul  2 08:47 Script.sh
    

    "x" is not there in the file attributes column, whereas the chmod command executed successfully. On the other hand, this script runs when I run it as

    root@freescale$ sh Script.sh 
    

    Can anyone help me to answer this situation?

  • Answers
  • Costin Gușă

    even if your "chmod" returns success, the execute bit does not really get turned on, as "ls" shows, therefore I conclude something is transparently preventing you from changing the file mode on the filesystem.

    what does "lsattr Script.sh" shows?

    can you put the script into a ramdrive or another filesystem on the same host and try again?

  • adebaumann

    Your script does not have the "execute" rights set for anybody. Try:

    chmod u+x Script.sh
    

    and the owner of the file should be able to start it with ./Script.sh

    Similarly,

    chmod g+x Script.sh
    chmod o+x Script.sh
    

    work if you want the group (g) or anybody (o) to be allowed to execute it.

  • ssssteffff

    The file has to be "executable" in order to be executed using ./Script.sh.

    Use chmod +x Script.sh to add the "execute" flag for this file.

    EDIT

    Sorry, I missed an important detail in your post, you already did that, but did not work... try executing chmod again, then immediately execute echo $? to get the exit status, it may be help.


  • Related Question

    linux - can't run binaries or shell scripts
  • hyperboreean

    I am running Debian testing and I am not able to run any binary or shell script. I keep getting "No such file or directory" for binaries and "Permission denied" for executable shell scripts. The umask is the default one and I haven't fooled around with the paths. Also, I am aware of this question, but it doesn't work out for me - I compiled my code on this machine and trying to run it on the same machine. Also, all of my shell scripts have the correct shebang.

    Any advices?

    Edit: I am not running any Armor or SELinux kind of application.


  • Related Answers
  • Marnix A. van Ammers

    More information including copy/paste of a terminal session would be helpful. But things to look for right away are that the binaries and scripts have the correct permissions (usually mode 0755). Double check that the shebang line in your scripts points to a valid binary. Run the "file" command on a binary to make sure it is a true binary for your architecture. Surely not all of your binaries get "no such file or directory", otherwise you wouldn't have been able to compile your code. Try the "which" command to see if your shell can find the binary you're trying to run (e.g. "which date" to see which command would run if you typed "date") and to make sure it is finding the one you think you're trying to run (sometimes a command appears earlier in your PATH and you're not aware of it).