bash 用于打开终端、导航到文件夹并运行指南针手表的 Ubuntu 桌面脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23103683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:13:28  来源:igfitidea点击:

Ubuntu desktop script to open terminal, navigate to a folder and run compass watch

bashshellubuntudesktop

提问by JarSeal

I have tried to search for a simple answer for running a simple script (myscript.sh) from the Ubuntu (13) desktop that would open the terminal, navigate to a folder (/var/www/myproject) and then run the "compass watch" command (and then stay open). First I thought this would be very simple to accomplish, but after several fails and searches, this particular task/script seems overwhelmingly hard to get to work.

我试图寻找一个简单的答案来从 Ubuntu (13) 桌面运行一个简单的脚本 (myscript.sh),它会打开终端,导航到一个文件夹 (/var/www/myproject),然后运行“compass watch”命令(然后保持打开状态)。首先,我认为这很容易完成,但经过几次失败和搜索后,这个特定的任务/脚本似乎很难开始工作。

To do this manually is not a big task:

手动执行此操作并不是一项大任务:

  • Open terminal
  • navigate "cd /var/www/myproject"
  • run "compass watch"
  • 打开终端
  • 导航“cd /var/www/myproject”
  • 运行“指南针手表”

I thought this would be a walk in the park (although I am quite new to terminal and scripting), but through the searches I got some weird desktop launcher answers and complicated bash scripting functions solutions for sub-shell work-a-rounds. My question is, can this be done with one single (simple) file that is launched from the desktop?

我以为这将是在公园里散步(尽管我对终端和脚本很陌生),但通过搜索我得到了一些奇怪的桌面启动器答案和复杂的 bash 脚本函数解决方案,用于子外壳工作。我的问题是,这可以通过从桌面启动的单个(简单)文件来完成吗?

回答by Biffen

Either you make a shell script (.shfile, don't forget to make it executable) or you make a desktop file (.desktop). Either of those can be double-clicked, although the former might bring up a dialog asking whether you want to execute it (can be configured, but only per user, IIRC).

要么制作一个 shell 脚本(.sh文件,不要忘记让它可执行),要么制作一个桌面文件(.desktop)。可以双击其中任何一个,尽管前者可能会弹出一个对话框,询问您是否要执行它(可以配置,但仅限每个用户,IIRC)。

myscript.desktop:

myscript.desktop

[Desktop Entry]
Exec=bash -c 'cd /var/www/myproject && compass watch'
Name=myscript
Terminal=true
Type=Application

(Note the Terminal=truepart.)

(注意Terminal=true部分。)

myscript.sh:

myscript.sh

#!/bin/sh
gnome-terminal -e 'cd /var/www/myproject && compass watch'

(Assuming you want to use GNOME Terminal.)

(假设您想使用 GNOME 终端。)

Of course, there are numerous other options.

当然,还有许多其他选择。

回答by smagnan

I didn't get well where is your problem ... If you want to run a script like that (double clic) you should first make it executable:

我不太明白你的问题在哪里......如果你想运行这样的脚本(双击),你应该首先让它可执行:

chmod u+x myscript.sh

Then you just have to place a link to this script on your desktop (or the script itself if you want ...) When double-clicking it, you will have the possibility to run it in a terminal.

然后你只需要在你的桌面上放置一个指向这个脚本的链接(或者脚本本身,如果你想要的话......)双击它时,你将有可能在终端中运行它。

Then put whatever you want in your script ( the navigate "cd /var/www/myproject" , run "compass watch"part) and end the script (myScript.sh) file with $SHELLto keep the terminal open after the script execution (:

然后把你想要的任何东西放在你的脚本中( 导航“cd /var/www/myproject”,运行“compass watch”部分)并结束脚本(myScript.sh)文件以$SHELL在脚本执行后保持终端打开(: