I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmod
ed 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?
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}!"
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With