Lubuntu:使用.sh脚本作为键绑定,代码工作,执行脚本会产生错误

时间:2014-06-02 20:40:40

标签: linux bash shell ubuntu key-bindings

这是我在stackoverflow上的第一篇文章,希望我不违反任何规则。我是一个完整的Linux新手(昨晚安装了Lubuntu 14.04 64bit),所以要及时发出警告。

简而言之,我正试图让我的笔记本电脑触控板切换工作(我的Inspiron5110上的Fn + F3)。我有一个bash脚本:

#!/bin/bash
if [ $(synclient -l | grep TouchpadOff | awk '{print $3}') == 1 ] ; then
synclient touchpadoff=0;
else
synclient touchpadoff=1;
fi

我是从http://crunchbang.org/forums/viewtopic.php?id=10996得到的。如果我将脚本代码粘贴到终端并执行它,它可以工作(触摸板打开/关闭)。但是,我想将它绑定到一个键,所以在我的lubuntu-rc.xml中我添加了以下内容:

<!-- disable touchpad -->
    <keybind key="XF86TouchpadToggle">
      <action name="Execute">
        <command>/usr/local/bin/touchpad.sh</command>
      </action>
    </keybind>

当我按下必要的键组合时,我得到"Failure to execute child process "/usr/local/bin/touchpad.sh" (No such file or directory)"。但是我可以在这个目录中看到,无论是在文件管理器中还是在终端中使用ls时文件都在那里:

/usr/local/bin$ ls -l
total 4
-rwxrwxr-x 1 paspaldzhiev paspaldzhiev 145 юни  2 22:54 touchpad.sh

我使用chmod +x touchpad.sh使其可执行。

现在,这会让人更加困惑:

如果我使用bash /usr/local/bin/touchpad.sh,我会:

paspaldzhiev@areuexperienced:/usr/local/bin$ bash touchpad.sh
touchpad.sh: line 6: syntax error near unexpected token `fi'
touchpad.sh: line 6: `fi'

虽然如上所述我知道如果我只是将它粘贴在终端中,代码就可以运行。

此外,如果我使用./touchpad.sh,我会:

paspaldzhiev@areuexperienced:/usr/local/bin$ ./touchpad.sh
bash: ./touchpad.sh: /bin/bash^M: bad interpreter: No such file or directory

请注意,我不太确定bash touchpad.sh./touchpad.sh在执行方面的区别是什么,只是我更熟悉Linux的朋友告诉我尝试这些:D 。

在任何情况下,我都不知道今后如何进行,有谁能请说明我做错了什么?

非常感谢!

2 个答案:

答案 0 :(得分:1)

你上次错误消息中的^ M是你的大提示; - )。不知何故,你使用了Windows编辑器,文件传输等。试试dos2unix touchpad.sh。它将从行尾删除所有CR(^ M)字符。它应该工作。祝好运。 - 炮手

答案 1 :(得分:0)

不需要脚本,因为不需要if指令。

将这段代码放在lubuntu-rc.xml

<keybind key="XF86TouchpadToggle">
  <action name="Execute">
    <command>synclient TouchpadOff=$((1-$(synclient | grep TouchpadOff | awk '{print $3}')))</command>
  </action>
</keybind>
相关问题