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

Firstly, this is my first approach to shell scripts. I only have experience with HTML and CSS:]

What I want to do is set up a simple folder structure and prompt the user in the dialog box to set a name for the root folder. I ran this shell script through an OS X service or Keyboard Maestro hotkey.

This is what I came 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 is some error - the variable will not be transferred and the root folder will not be created. I also read that I should use a "dialog" to request input, but I could not write something that works.

Your help is greatly appreciated. Thanks.

+4
source share
5 answers

The echo and read commands work using a text interface, for example, in the terminal window where the script is running. If you run the script in the OS X service, there is no Terminal-like interface, so no command does anything useful. (I don’t know about Keyboard Maestro, but I suppose this is similar.) The easiest way to deal with this situation is to usually use AppleScript with the osascript :

 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 potential conflicts with special variables such as PATH, etc.)

+9
source

I assume that the script does not run in an interactive shell, which means that you cannot interact with read -e .

I don't have an OSX window to try this, but you can try CocoaDialog for a graphical dialog.

Unconfirmed example:

 #!/bin/bash NAME=`CocoaDialog standard-inputbox --title "Project Name" --no-newline --no-cancel --informative-text "Enter the project name"` mkdir -p ~/Desktop/$NAME/subfolder1 mkdir -p ~/Desktop/$NAME/subfolder2 
+2
source

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

Improvements:

  • leading and trailing spaces are truncated from the input
  • only white list entry is placed
  • empty or empty space returns the user for the request after warning

the 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 
+2
source

Try changing the shebang line to #!/bin/bash . read is a built-in bash, and you are not using your script with bash at present. /bin/sh may actually be a link to /bin/bash , but according to Wikipedia, it depends on the specific version of OS X that you have.

Your script, since it should work correctly in bash.

+1
source

Is NAME empty or just print $ NAME as a string? If this is the last, you should try ~ / Desktop / $ (NAME)

In addition, you should try XDialog (or something else on MacOS). There are many resources associated with its use for input processing.

0
source

Source: https://habr.com/ru/post/1395273/


All Articles