创建接受拖放的可执行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10393164/
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 02:09:54  来源:igfitidea点击:

Create executable bash script that accepts drag & drop

bashubuntudrag-and-dropexe

提问by HitomiTenshi

I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?

我在 Ubuntu 上从源代码编译了 mplayer。我不想使用 GUI,但我想制作一个可执行的 bash 文件,该文件从一个文件中获取路径,该文件被放到 bash 文件中。我如何使这样的事情成为可能?

I wanted to make it look something like this: mplayer <get full path to file.file-ending>

我想使它看起来像这样: mplayer <get full path to filefile-ending>

I want the executable bash file to sit on my desktop ;)

我希望可执行 bash 文件位于我的桌面上;)



If possible, I'd just like an rightclick -> start with mplayerfunction, but I don't know how to make one.

如果可能,我只想要一个rightclick -> start with mplayer函数,但我不知道如何制作。

采纳答案by PhoneixS

You can access arguments passed to the script with $1(for the first argument). And also you should make a .desktopfile so Nautilus (or your desktop manager) know what to do and use %uto pass the dropped path to the script.

您可以使用$1(对于第一个参数)访问传递给脚本的参数。并且您还应该制作一个.desktop文件,以便 Nautilus(或您的桌面管理器)知道该做什么并使用它%u来将放置的路径传递给脚本。

For example you can create a file named DropOverMe.desktop:

例如,您可以创建一个名为 的文件DropOverMe.desktop

[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application

I use gnome-terminalas I have Ubuntu on my PC, use your preferred terminal application.

我使用gnome-terminal我的 PC 上有 Ubuntu,请使用您首选的终端应用程序。

And the script could be something like:

脚本可能是这样的:

#! /bin/bash

echo "Launched with " >> /tmp/history.log

回答by David Shih

Try:

尝试:

#!/bin/bash
mplayer ""

The file path of the dropped file will be passed to the script file as the 1th command line argument.

删除文件的文件路径将作为第 1 个命令行参数传递给脚本文件。