bash 将文件拖放到 .sh 文件上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257884/
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
Dragging and Dropping a File onto a .sh file
提问by DaGamer12345
I have a fairly good amount of knowledge with Batch. I'm trying to port a batch script over to Mac/UNIX, but my batch file has a drag-and-drop thing to it. Through countless Google searches, I have came up with nothing. They all say that you can drag-and-drop into a Terminal Window - not good for no-input-required scripts.
我对 Batch 有相当多的了解。我正在尝试将批处理脚本移植到 Mac/UNIX,但我的批处理文件具有拖放功能。通过无数次谷歌搜索,我一无所获。他们都说您可以拖放到终端窗口中——这对于不需要输入的脚本来说并不好。
Here is the code for Batch I have:
这是我拥有的批处理代码:
cd %USERPROFILE%
7za x %* -o%USERPROFILE%\Desktop\Temp
7za a %1 %USERPROFILE%\Desktop\Temp\*
cd %USERPROFILE%\Desktop
rmdir /q /s Temp\
Not particularly worried about 7za commands (because of Archive Utility),cd %USERPROFILE%(because Terminal starts in the user's profile), rmdir, cd, and such, as they are only basic file commands. But I don't know the code to reference to the file dropped/opened with the .sh script.
不是特别担心 7za 命令(因为 Archive Utility)、cd %USERPROFILE%(因为终端在用户的配置文件中启动)、rmdir、cd等,因为它们只是基本的文件命令。但我不知道引用使用 .sh 脚本删除/打开的文件的代码。
So, if someone knows that code, please tell me. I know this is kind-of a simple thing, but you can't know every command, especially when dealing with unfamiliar programming languages.
所以,如果有人知道那个代码,请告诉我。我知道这是一件很简单的事情,但是您不可能知道每个命令,尤其是在处理不熟悉的编程语言时。
回答by Gordon Davisson
I don't know of a way to have the raw script accept drag-and-dropped files, but there are a number of options to wrap it in something else that accepts drag-and-drop, and passes the files to a script. IMO the quickest option is to wrap the script in an Automator application. Run /Applications/Automator.app, select Application as the type for your new project, drag the "Run Shell Script" action from the actions list (second column) into the workflow (right column), set its "Pass input" to "as arguments", paste in the shell script, then save the app. Done.
我不知道有什么方法可以让原始脚本接受拖放文件,但是有很多选项可以将它包装在接受拖放的其他东西中,并将文件传递给脚本。IMO 最快的选择是将脚本包装在 Automator 应用程序中。运行/Applications/Automator.app,选择Application作为新项目的类型,将“Run Shell Script”动作从动作列表(第二列)拖到工作流(右列)中,将它的“Pass input”设置为“作为参数”,粘贴到 shell 脚本中,然后保存应用程序。完毕。
Other options for wrapping a shell script include: AppleScript, Platypus, and DropScript.
包装 shell 脚本的其他选项包括:AppleScript、Platypus和DropScript。
回答by mivk
I'm late to this, and don't even have the answer to your drag-and-drop question. But I notice this looks like a Batch script for Windows' cmd.exewhich will not work at all in any Unix shell. So you really need to completely rewrite it for Bash first!
我迟到了,甚至没有答案你的拖放问题。但我注意到这看起来像一个 Windows 的批处理脚本,cmd.exe它在任何 Unix shell 中都不起作用。所以你真的需要先为 Bash 完全重写它!
- "\" is an escape character in Unix. The path separator is "/".
- Shell variables begin with "$", not "%". And the Windows
%USERPROFILE%variable would be$HOMEin Unix. - Options start with "-", not "/".
rmdironly deletes empty directories. Seeman rmdirandman rmwith the-f -roptions for an equivalent of yourrmdir /q /s TempWindows command.
- "\" 是 Unix 中的转义字符。路径分隔符是“/”。
- Shell 变量以“$”开头,而不是“%”。Windows
%USERPROFILE%变量将$HOME在 Unix 中。 - 选项以“-”开头,而不是“/”。
rmdir只删除空目录。查看man rmdir和man rm使用与Windows 命令-f -r等效的选项rmdir /q /s Temp。
But beware that once you learn some Bash, you will start to really hate that horrible cmd.exe:-)
但要注意,一旦你学会了一些 Bash,你就会开始真正讨厌那可怕的cmd.exe:-)

