从linux运行存储在windows共享上的bash脚本


3

我想直接从Windows共享运行脚本。Windows机器运行Windows 7,我尝试在同一局域网上运行Ubuntu 13.10的脚本。我尝试使用bash脚本和python脚本,当我运行它时,我有以下错误:

zsh: permission denied: ./bc.sh

包含脚本的共享安装如下:

sudo mount -t cifs -o username=user,passwordpass=,exec,rw,users,file_mode=0777,dir_mode=0777 //192.168.0.3/folder /mnt/Win7

该脚本具有以下权限:

-rwxrwxrwx 0 root root       24 Dec 10 20:50 bc.sh

并且只包含一个简单的命令:

#!/bin/bash
echo "Test"

我尝试用sudo运行它但是也没有用。

sudo: unable to execute ./bc.sh: Permission denied

我也尝试使用bash而不是zsh,但没有成功。

关于我缺少什么的任何想法?

谢谢。

Answers:


0

你必须执行脚本

       sh path/to/bc.sh

不,这不是必要的:脚本已经可执行,因此它可以像OP一样运行
MariusMatutiae 2013年

如果这确实答案(如标记)那么理解为什么会很好。正如@MariusMatutiae所说,你通常不需要运行它,sh但有一些关于网络共享的东西会改变它。
Leonard Challis 2016年

0

据我所知,有三种可能的原因导致脚本在直接调用时可能无法运行。

  1. 电子X ecute位尚未确定。

    [liveuser@localhost-live ~]$ ls -l ~/*.sh
    -rw-rw-rw-. 1 liveuser liveuser 28 Mar 25 07:55 /home/liveuser/testnox.sh
    -rwxrwxrwx. 1 liveuser liveuser 28 Mar 25 07:23 /home/liveuser/test.sh
    [liveuser@localhost-live ~]$ ~/test.sh
    Hello world.
    [liveuser@localhost-live ~]$ ~/testnox.sh
    bash: /home/liveuser/testnox.sh: Permission denied
    [liveuser@localhost-live ~]$ sh ~/testnox.sh
    Hello world.
    

    请注意,sh只要您具有读取权限,您仍然可以通过直接调用来运行它。另外,检查x位是否适用于您。如果该文件由root拥有并且它显示为-rwx------.那么您必须使用sudo它来运行它。

  2. 应用SELinux规则会阻止您执行该文件。我对此并不太了解,但sudo setenforce 0事先使用可以帮助解决这个问题。完成后不要忘记使用sudo setenforce 1

    [liveuser@localhost-live ~]$ sestatus
    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   enforcing
    Mode from config file:          enforcing
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      30
    [liveuser@localhost-live ~]$ sudo setenforce 0
    [liveuser@localhost-live ~]$ sestatus
    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   permissive
    Mode from config file:          enforcing
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      30
    [liveuser@localhost-live ~]$ sudo setenforce 1
    [liveuser@localhost-live ~]$ sestatus
    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   enforcing
    Mode from config file:          enforcing
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      30
    
  3. 脚本所在的树是使用安装的noexec,在许多情况下这是默认的。注意重要的是挂载(no)exec状态,有时既不复制挂载点的父节点的状态,也不复制原始卷的状态。mount --bind

    [liveuser@localhost-live ~]$ ls -l original
    total 4
    -rwxrwxr-x. 1 liveuser liveuser 28 Mar 25 08:14 test.sh
    [liveuser@localhost-live ~]$ ls -l alias
    total 4
    -rwxrwxr-x. 1 liveuser liveuser 28 Mar 25 08:14 test.sh
    [liveuser@localhost-live ~]$ original/test.sh
    Hello world.
    [liveuser@localhost-live ~]$ alias/test.sh
    bash: alias/test.sh: Permission denied
    [liveuser@localhost-live ~]$ sh alias/test.sh
    Hello world.
    [liveuser@localhost-live ~]$ sudo mount -o remount,exec --bind original alias
    [liveuser@localhost-live ~]$ alias/test.sh
    Hello world.
    

Linux令人沮丧的原因之一是因为错误消息并没有真正为您提供找到解决方案所需的详细信息。仅仅Permission denied是在我看来还不够。它应该说实际原因是什么,例如:Permission denied: /home/liveuser/alias was mounted with noexec。然后你就会知道如何解决这个问题。

好吧,无论如何我希望这会有所帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.