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

5,720

Solution 1

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?

Solution 2

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.

Share:
5,720
pRAShANT
Author by

pRAShANT

Updated on September 18, 2022

Comments

  • pRAShANT
    pRAShANT about 1 year

    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?

  • pRAShANT
    pRAShANT over 10 years
    It gives following error : lsattr: Inappropriate ioctl for device While reading flags on Script.sh
  • pRAShANT
    pRAShANT over 10 years
    I got success status (i.e 0) on performing echo $?
  • pRAShANT
    pRAShANT over 10 years
    I tried it but its still the same, no change
  • pRAShANT
    pRAShANT over 10 years
    Well, I have placed it on a flash drive (type vfat) and mounted on /mnt partition. And the rootfs I am using is UBIFS. I am running it directly from the flash drive.On other system it runs fine without any issue
  • ssssteffff
    ssssteffff over 10 years
    This subject has been discussed here and here. It seems impossible to execute script from such a device. Perhaps take a look at the post mentioning mount with exec in the second thread.
  • Costin Gușă
    Costin Gușă over 10 years
    what is the output of "mount" command?
  • Costin Gușă
    Costin Gușă over 10 years
    I was able to directly run a .sh script by mounting with umask=000 (as described in the second thread posted by @ssssteffff) - ie "mount -o umask=000 -t vfat [...]" Can you do the same and re-run ls?
  • pRAShANT
    pRAShANT over 10 years
    @Costin Gușă: O/P of mount command rootfs on / type rootfs (rw) ubi0:gpmi-nfc-general-use on / type ubifs (rw,relatime) sys on /sys type sysfs (rw,relatime) tmpfs on /dev type tmpfs (rw,relatime,mode=755) ... /dev/mtdblock4 on /mnt/mtdblock4 type vfat (ro,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=‌​ascii,shortname=mixe‌​d,errors=remount-ro) /dev/sda1 on /mnt/usb/sda1 type vfat (rw,nosuid,nodev,noexec,noatime,fmask=0111,dmask=0000,allow_‌​utime=0022,codepage=‌​cp437,iocharset=asci‌​i,shortname=mixed,qu‌​iet,flush,errors=rem‌​ount-ro)
  • Costin Gușă
    Costin Gușă over 10 years
    are you able mount it as described above?
  • pRAShANT
    pRAShANT over 10 years
    @are you able mount it as described above? – Costin Gușă : Yes, it was self detected and mounted in a location /mnt/usb/sda1/<flashdrive content>
  • Costin Gușă
    Costin Gușă over 10 years
    you can run it with sh because sh is in fact something like /bin/sh and Script.sh is the argument to /bin/sh which is already executable. so either run via sh or mount with exec (which is among default options) and umask=000 or whatever mask you need as long as it preserves the executable flag
  • pRAShANT
    pRAShANT over 10 years
    yes, i haven't thougt of it! Anyways Thanks! to both Costin Gușă and ssssteffff
  • Shayan
    Shayan about 4 years
    chmod 755 ./Script.sh did the job for me