Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prompt a user for a name to create a folder (bash | shell script)?

First off, this is my first go at shell scripting. I have only experience in HTML and CSS :]

What I want to do is setup a simple folder structure and prompt the user in a dialog box to set a name for the root folder. I'll trigger this shell script via an OS X Service or a Keyboard Maestro hotkey.

This is what I've come up with so far:

#!/bin/sh

echo -n "Enter the project name:"
read -e NAME  

mkdir -p ~/Desktop/$NAME
mkdir -p ~/Desktop/$NAME/subfolder1
mkdir -p ~/Desktop/$NAME/subfolder2

Obviously there's some error - the variable won't get passed on and the root folder isn't created. I also read that I should use "dialog" to ask for the input, but I wasn't capable of writing something that works.

Your help is greatly appreciated. Thanks.

like image 566
patrick Avatar asked Feb 07 '12 17:02

patrick


People also ask

How do I create a folder in bash shell?

Create a New Directory ( mkdir ) The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.


2 Answers

The echo and read commands both work via a text interface, such as the Terminal window the script is running in. If you run a script in an OS X Service, there's no Terminal-like interface, so neither command does anything useful. (I don't know about Keyboard Maestro, but I assume it's similar.) The simplest way to interact from a situation like this is generally to use AppleScript via the osascript command:

name="$(osascript -e 'Tell application "System Events" to display dialog "Enter the project name:" default answer ""' -e 'text returned of result' 2>/dev/null)"
if [ $? -ne 0 ]; then
    # The user pressed Cancel
    exit 1 # exit with an error status
elif [ -z "$name" ]; then
    # The user left the project name blank
    osascript -e 'Tell application "System Events" to display alert "You must enter a project name; cancelling..." as warning'
    exit 1 # exit with an error status
fi

mkdir -p ~/Desktop/$name
mkdir -p ~/Desktop/$name/subfolder1
mkdir -p ~/Desktop/$name/subfolder2

(Note: I prefer to use lowercase variable names in the shell to avoid possible conflicts with special variables like PATH etc...)

like image 82
Gordon Davisson Avatar answered Oct 12 '22 23:10

Gordon Davisson


Note: @Gordon Davisson's answer deserves the credit, but here are some improvements that make the code more robust (I tried to edit the original post, but it was rejected).

Improvements:

  • leading and trailing whitespace is trimmed from the input
  • whitespace-only input is prevented
  • empty or whitespace-only input returns user to prompt after warning

Code:

while :; do # Loop until valid input is entered or Cancel is pressed.
    name=$(osascript -e 'Tell application "System Events" to display dialog "Enter the project name:" default answer ""' -e 'text returned of result' 2>/dev/null)
    if (( $? )); then exit 1; fi  # Abort, if user pressed Cancel.
    name=$(echo -n "$name" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.
    if [[ -z "$name" ]]; then
        # The user left the project name blank.
        osascript -e 'Tell application "System Events" to display alert "You must enter a non-blank project name; please try again." as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
        break
    fi
done
like image 39
Cole Maddux Avatar answered Oct 13 '22 01:10

Cole Maddux