bash Mac OSX - 允许用户通过 GUI 或提示在 shell 脚本中输入

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

Mac OSX - Allow for user input in shell script via GUI or Prompt

macosbashshell

提问by SpacemanTrip

I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmoded into an executable script and run from terminal. But, I would like to deploy it and have it be a double click kind of solution instead of instructing people to run it from terminal.

我创建了一个 shell 脚本,它需要一个人输入他们的名字,然后生成一个报告。当chmoded 进入可执行脚本并从终端运行时,该脚本会根据需要工作。但是,我想部署它并让它成为一种双击解决方案,而不是指示人们从终端运行它。

I tried wrapping the script in Platypus, which makes it easy to launch. But it doesn't allow for input from the user, which is critical.

我尝试将脚本包装在 Platypus 中,这使其易于启动。但它不允许来自用户的输入,这很关键。

I just found cocoaDialog, but my main concern is whether it will provide the functionality I need and do so without having everyone else install it.

我刚刚发现cocoaDialog,但我主要关心的是它是否会提供我需要的功能,并且不需要其他人安装它。

Has anyone ever been in this situation or can offer any pointers?

有没有人遇到过这种情况或可以提供任何指示?

回答by scottgwald

For the record, I tested (on OS X Yosemite) the following script (namescript), which uses the readcommand to accept user input. After chmod +x namescript, double clicking from Finder properly launched the script and accepted user input.

作为记录,我测试了(在 OS X Yosemite 上)以下脚本 ( namescript),它使用read命令来接受用户输入。之后chmod +x namescript,从 Finder 双击正确启动脚本并接受用户输入。

    #! /bin/bash

    echo "Please enter your name"
    read name
    echo "Your name is $name"

It is important to choose a name for the script with either no extension (as in namescript) or a .commandextension (namescript.command). By default, using .sh(namescript.sh) causes double clicking to open the script in a text editor, as noted in the OP.

为脚本选择一个不带扩展名(如namescript)或带.command扩展名 ( namescript.command)的名称很重要。默认情况下,使用.sh( namescript.sh) 会导致双击以在文本编辑器中打开脚本,如 OP 中所述。

回答by jm666

OS X has (mostly) all batteries included, just use it. :)

OS X 包含(大部分)所有电池,只需使用它。:)

For this, you could use the Automator.app. With the Automator, you could create executable application (e.g. your.app) what will contains your shell script and also asks for the user inputs.

为此,您可以使用Automator.app. 使用 Automator,您可以创建可执行应用程序(例如your.app),其中包含您的 shell 脚本并要求用户输入。

Example, asking for two inputs: "name" and "year" you should do the following:

例如,要求两个输入:“name”和“year”,您应该执行以下操作:

  • Launch Automator
  • Choose "Application"
  • Click the "Library" button on the toolbar, if the Library is hidden
  • Drag the following actions from the Library to the workflow
    • "Ask for text"
      • Enter the question: "First and Last name:"
      • click "Require an answer"
    • "Set value of variable"
      • Create new variable "name"
    • "Ask for text"
      • "Enter the second question, e.g. "Enter year:"
      • Add the default e.g. 2015 (if want)
      • click the "Require an answer" checkbox
      • check the "Ignore this actions input" in the "Options"
    • "Get value of variable
      • Select "name"
    • "Run Shell script"
      • select "Pass inputs" -> "as arguments"
      • copy & paste your shell-script into the window, like:
  • 启动 Automator
  • 选择“申请”
  • 如果库被隐藏,请单击工具栏上的“库”按钮
  • 将以下操作从库中拖到工作流中
    • “索取文本”
      • 输入问题:“名字和姓氏:”
      • 点击“需要答案”
    • “设置变量值”
      • 创建新变量“名称”
    • “索取文本”
      • “输入第二个问题,例如“输入年份:”
      • 添加默认值,例如 2015(如果需要)
      • 单击“需要答案”复选框
      • 检查“选项”中的“忽略此操作输入”
    • "获取变量值
      • 选择“姓名”
    • “运行Shell脚本”
      • 选择“传递输入”->“作为参数”
      • 将您的 shell 脚本复制并粘贴到窗口中,例如:
year=""
name=""
echo "Report for name: $name year: $year"
  • also, you can add the final action "copy to clipboard", e.g. the output from the shell script will go into the clipboard.
  • 此外,您可以添加最终操作“复制到剪贴板”,例如,shell 脚本的输出将进入剪贴板。

Save the script (you will get an name.app- standard OS X .appapplication), just add it into .dmgor create a .zipfrom it and you could deploy it.

保存脚本(您将获得一个name.app- 标准 OS X.app应用程序),只需将其添加到其中.dmg.zip从中创建一个,您就可以部署它。

Everything is much faster to do, as read this answer. ;)

正如阅读this answer一样,一切都做得更快。;)

回答by Ashley Primo

From what I understand I would recommend you look in to Applescript as this will allow you to have a GUI Interface as well as executing 'SHELL' commands.

据我了解,我建议您查看 Applescript,因为这将允许您拥有 GUI 界面以及执行“SHELL”命令。

First of all I would open 'Script Editor' program that comes preinstalled on Mac's

首先,我会打开预装在 Mac 上的“脚本编辑器”程序

This is an example script which asks for the user's name then says it via executing a shell command "say name"

这是一个示例脚本,它要求输入用户名,然后通过执行 shell 命令“say name”来说出它

display dialog "What is you name? " default answer "" buttons {"Say It"} default button 1
text returned of the result
do shell script "say " & result

You may also append with administrator privilegesto the do command which will make it run with administrator privileges (Ask for administrators username and password)

您还可以附加with administrator privileges到 do 命令,使其以管理员权限运行(询问管理员用户名和密码)

Example:

例子:

display dialog "What is you name? " default answer "" buttons {"Say It"} default button 1
text returned of the result
do shell script "say " & result with administrator privileges

Hope this helped.

希望这有帮助。