make bash script display system event dialog, then take its result and use it in an if statement

This is my first time posting on StackOverflow after lurking for long time. I am running Mac OS X 10.10.3. I am a bash novice.

I have a script that, after asking for confirmation using read in Terminal, uses grep to check whether /dev/disk1 is mounted, then formats that disk. That’s a dangerous script, hence why asking if it’s okay first is vital.

Eventually, I would like to have this script be an executable that the user can double-click on. But rather than having the user type “y” and Return into a Terminal window, I would rather a display dialog appear with “yes” and “no” buttons, have them choose, then have the script run based on their answer. Is this possible in bash?

I’m working in an environment in which I don’t have administrative access, so while I can write an AppleScript Service to accomplish what I want to do and to integrate this elegantly into the user interface, I can’t integrate that Service into the environment without the admin password (since I can’t edit ~/Library/Services for the user without it). Also, I cannot download or install any new libraries, applications — anything, really — in the environment; I must only use native bash in Mac OS X.

Here is what I have:

read -p "Are you sure you want to partition this disk? " -n 1 -r # Can I make this be a dialog box instead?

if [[ $REPLY =~ ^[Yy]$ ]] # Can this accept the result as a condition?
    if grep -q 'disk1' /dev/ && grep -q 'file.bin' ~/Downloads; then
        echo # redacted actual code
        osascript -e 'tell app "System Events" to display dialog "The disk is not mounted."'
        exit 1
    exit 1

Thanks very much for your help.

Source: shell

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.