编写脚本来执行命令?

时间:2015-02-18 17:08:15

标签: linux bash shell executable

我从未写过剧本,所以请耐心等待。我需要做的是制作两个我可以从桌面点击的脚本,它们都会打开自己的终端(并保持打开直到我手动关闭它)并运行给定的行。 对于第一个,我必须手动运行:

cd home/pi/PiBits/ServoBlaster/user
sudo ./servod

第二个:

cd ~/scratchClient
python crs/scratchClient.py -c servoblaster

我该怎么做?我读了一些关于把xterm -e放在它前面的东西,但这些对我来说都不适用...... 顺便说一句,这将用于Raspbian Linux。 编辑,这对我有用:
链接:ubuntuforums.org/showthread.php?t = 1336228使用的行:gnome-terminal --execute bash -c "/path/scriptname ; bash"

3 个答案:

答案 0 :(得分:1)

你只需要添加一个shebang,这意味着将它放在脚本的第一行:

#!/bin/sh

这会导致使用bourne shell来解释脚本,这可能是(可能)当您在终端时运行的解释器。然后,您应该使脚本可执行chmod +x <script>

答案 1 :(得分:0)

试试这个。

xterm -hold -e 'cd /home/pi/PiBits/ServoBlaster/user
    sudo ./servod' &

xterm -hold -e 'cd /home/pi/scratchClient
    python crs/scratchClient.py -c servoblaster' &

如果它不起作用,也许你应该解释它失败的方式。如果它有效,你可以在前面添加一个shebang,将它们保存在文件中,chmod +x这些文件,然后点击你心中的内容(或者你可以获得一个更复杂的味道,你只需将它们作为没有任何xterm或其他焦虑的GUI的后台工作。

答案 2 :(得分:0)

解决方案是使用gnome-terminal ...找到an UbuntuForums post类似的问题,例如我的。

gnome-terminal --execute bash -c "/path/scriptname ; bash"
相关问题