replace ugly if statement with case

This commit is contained in:
Heavybullets8 2022-08-10 17:46:01 -06:00
parent c6d0926fc2
commit 665db11d8c

View File

@ -30,41 +30,82 @@ do
containers+=("$(echo "$search" | grep "$pod" | awk '{print $4}')")
done
if [[ "${#containers[@]}" == 0 ]]; then
echo -e "No containers available\nAre you sure the application in running?"
exit
elif [[ "${#containers[@]}" == 1 ]]; then
container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}')
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
else
while true
do
clear -x
title
cont_search=$(
for i in "${containers[@]}"
case "${#containers[@]}" in
0)
echo -e "No containers available\nAre you sure the application in running?"
exit
;;
1)
container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}')
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
;;
*)
while true
do
echo "$i"
done | nl -s ") " | column -t
)
echo "$cont_search"
echo
echo "0) Exit"
read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
if [[ $selection == 0 ]]; then
echo "Exiting.."
exit
elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then
echo "Error: \"$selection\" was not an option.. Try again"
sleep 3
continue
else
break
fi
done
container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}')
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
fi
clear -x
title
cont_search=$(
for i in "${containers[@]}"
do
echo "$i"
done | nl -s ") " | column -t
)
echo "$cont_search"
echo
echo "0) Exit"
read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
if [[ $selection == 0 ]]; then
echo "Exiting.."
exit
elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then
echo "Error: \"$selection\" was not an option.. Try again"
sleep 3
continue
else
break
fi
done
container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}')
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
;;
esac
# if [[ "${#containers[@]}" == 0 ]]; then
# echo -e "No containers available\nAre you sure the application in running?"
# exit
# elif [[ "${#containers[@]}" == 1 ]]; then
# container=$(echo "$search" | grep "${pod_id[*]}" | awk '{print $4}')
# container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
# else
# while true
# do
# clear -x
# title
# cont_search=$(
# for i in "${containers[@]}"
# do
# echo "$i"
# done | nl -s ") " | column -t
# )
# echo "$cont_search"
# echo
# echo "0) Exit"
# read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
# if [[ $selection == 0 ]]; then
# echo "Exiting.."
# exit
# elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then
# echo "Error: \"$selection\" was not an option.. Try again"
# sleep 3
# continue
# else
# break
# fi
# done
# container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}')
# container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
# fi
while true
do
clear -x