2

I would like to try out a shell script that makes use of whiptail, but I am on a MAC and whiptail doesn't seem to exist there. Does anyone know if there exists a drop-in alternative (like, for example exa for ls)?

This is the script I would like to test out (It's a WP-CLI script that would allow me to select local WordPress installs, using a dialog. Found it at the bottom of this page):

#!/bin/bash

WP_TOP_PATH="/home/"
MENU_TEXT="Choose an installation"
GAUGE_TEXT="Searching for WordPress"

declare -a MENU
WPS="$(wp --allow-root find "$WP_TOP_PATH" --field=version_path)"
WP_TOTAL="$(wc -l <<< "$WPS")"
WP_COUNT="0"

while read -r WP; do
    WP_LOCAL="${WP%wp-includes/version.php}"

    NAME="$(cd "$WP_LOCAL"; sudo -u "$(stat . -c %U)" -- wp --no-debug --quiet option get blogname)"
    if [ -z "$NAME" ]; then
        NAME="(unknown)"
    fi
    MENU+=( "$WP_LOCAL" "$NAME" )

    echo "$((++WP_COUNT * 100 / WP_TOTAL))".
done <<< "$WPS" > >(whiptail --gauge "$GAUGE_TEXT" 7 74 0)

WP_LOCAL="$(whiptail --title "WordPress" --menu "$MENU_TEXT"  $((${#MENU[*]} / 2 + 7)) 74 10 "${MENU[@]}" 3>&1 1>&2 2>&3)"

if [ $? -ne 0 ] || [ ! -d "$WP_LOCAL" ]; then
    echo "Cannot find '${WP_LOCAL}'" 1>&2
    exit 100
fi

echo "cd ${WP_LOCAL}"
1
  • 1
    Though I don't know much about bash, this feels very much like an XY Problem
    – Tetsujin
    Commented Dec 27, 2021 at 12:15

3 Answers 3

4

You can get whiptail on macOS by installing newt brew install newt

reference: https://command-not-found.com/whiptail

1
  • should definitely be the accepted answer
    – ccpizza
    Commented Sep 26, 2023 at 9:56
2

I would start with brew install dialog from https://brew.sh and then

alias whiptail=dialog

Hopefully you don’t have to alter your script other than dropping the alias as a new line 3 in your script. For people not already using Wordpress on other Unix, the Apple specific apps and installers seem like a great option as well due to extensive documentation and native apps.

Lastly, if you find you need whiptail, containers are a great option so you don’t have to fork your tools and edit scripts.

2
  • It probably will require either a symlink (ln -s $(brew --prefix)/dialog /usr/local/bin/whiptail) or changes in the script. Aliases usually are ignored in scripts.
    – nohillside
    Commented Dec 27, 2021 at 13:20
  • 1
    Agreed @nohillside I did not load up that script to test things thoroughly - I figure if the OP gets bash, it’s a non issue and if they don’t the other suggestions are more durable than getting them to the next roadblock using bash to manage Wordpress local installs.
    – bmike
    Commented Dec 27, 2021 at 13:29
1

Thanks to the answer of bmike and the comment from @nohillside I was able to make this work. After running brew install dialog, I put this in my ~/.bashrc:

alias whiptail=dialog

And to make my script actually respect the alias, I followed the instructions found here and put this in my script at the very beginning:

#!/bin/bash

shopt -s expand_aliases
source ~/.bashrc

2
  • Great to see that it works. Relying on an entry in .bashrc makes this non-portable though. To be on the safe side, it probably would make more sense to define the alias directly in the script (instead of sourcing .bashrc). Or even use a shell function :-)
    – nohillside
    Commented Dec 28, 2021 at 14:53
  • Awesome answer. I got stuck between a function and an alias. I might even just change my answer to use a variable for the binary if you want another option.
    – bmike
    Commented Dec 28, 2021 at 23:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .