Bash - How to make dynamic menu selection without eval
1
vote
1
answer
105
views
I'm making a script for Docker environments, and I'm a bit stuck with a pigeonhole I've gotten myself into.
#!/bin/bash
set -euo pipefail
# Variables
gituser="modem7"
gitrepo="docker-devenv"
gitfolder="Environments"
buildername="DockerDevBuilder"
# Colours
RED="\e[31m"
GREEN="\e[32m"
END="\e[0m"
echo "========================================="
printf " Checking Dependencies\n"
echo "========================================="
printf "Checking if dependencies are installed...\n"
pkg_list=(docker jq)
tc() { set ${*,,} ; echo ${*^} ; }
for pkg in "${pkg_list[@]}"
do
titlecase=$(tc $pkg)
isinstalled=$(dpkg-query -l $pkg > /dev/null 2>&1)
if [ $? -eq 0 ];
then
printf "~ $titlecase is...${GREEN}installed${END}\n"
else
printf "~ $titlecase is...${RED}not installed${END}\n"
printf "Exiting Script. Install $pkg.\n"
echo "========================================="
exit
fi
done
echo "========================================="
cat /dev/null 2>&1; then
echo ""
echo "Builder $buildername created"
else
echo "Builder already created, using $buildername"
docker buildx use "DockerDevBuilder"
echo ""
fi
echo "Creating $dev_name Environment..."
docker buildx build --rm=true --build-arg BUILDKIT_INLINE_CACHE=1 --load -t $lowerdev:dev https://github.com/$gituser/$gitrepo.git#:$gitfolder/$dev_name \
&& clear \
&& echo "=========================================" \
&& echo "Activating $dev_name Dev Environment..." \
&& echo "Press CTRL + D or type exit to leave the container" \
&& docker run --rm -it --name "$dev_name"Dev"$RANDOM" --hostname "$dev_name"Dev"$RANDOM" "$lowerdev:dev"
break
;;
"Prune")
echo "Clearing Docker cache..."
docker system prune -af
echo ""
echo "Removing Docker buildx builder..."
if docker buildx rm "$buildername" > /dev/null 2>&1; then
echo ""
echo "Builder $buildername removed"
else
echo "Builder already removed, no action performed"
echo ""
fi
exec bash $0
;;
"Quit")
echo "Exiting script"
exit
;;
*)
echo "invalid option $REPLY"
;;
esac"
done
exit 0
I'm currently using "
eval "case \"$dev_name\" in
" but that seems problematic from what I've read.
It works, but I'm not sure if there is a better way to achieve what the results.
The choices are created from the folder names in the repo , but I'm not quite sure how to get out of using eval. Am I worrying about something pointless?
Asked by Modem7
(11 rep)
Nov 3, 2022, 06:56 PM
Last activity: Nov 4, 2022, 01:17 AM
Last activity: Nov 4, 2022, 01:17 AM