Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

macos

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.

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.

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.

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

like image 592
SpacemanTrip Avatar asked Jun 15 '15 20:06

SpacemanTrip


People also ask

How do I prompt input in shell?

Shell Script to Prompt User InputWrite a sample shell script to prompt for the user input and store it in a variable. Make sure to print a proper message on the screen before prompting for input. Store the input value in a variable and then print it in with a welcome message. echo "Welcome ${x}!"

How do I prompt a user input in Bash?

Ask the User for Input. If we would like to ask the user for input then we use a command called read. This command takes the input and will save it into a variable.

How do you take user input in a script?

We have used the read command to get the user input in the variable name. The echo command is an optional command to just verify that we have stored the input in the name variable. We use $ in front of the variable command so as to fetch and parse the literal value of the variable.

Does Mac support command line shells?

Every Mac comes with a Unix shell that provides a command line interface. Macs running macOS 10.15 and later use Zsh by default. Before that, Macs used the Bash shell by default. Of course, no matter what version of macOS you're using, you can change the shell your Mac is using.


1 Answers

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

    #! /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 .command extension (namescript.command). By default, using .sh (namescript.sh) causes double clicking to open the script in a text editor, as noted in the OP.

like image 147
scottgwald Avatar answered Sep 23 '22 02:09

scottgwald