Exercism/bash/secret-handshake/secret_handshake.sh
2023-06-11 18:44:46 +02:00

41 lines
898 B
Bash

#!/usr/bin/env bash
# Read the number from the user
number=$1
binary=""
while ((number > 0)); do
remainder=$((number % 2))
binary="$remainder$binary"
number=$((number / 2))
done
binary=$(printf "%05d" "$binary")
# Initialize an empty array to store the actions
actions=()
# Check the rightmost five digits of the binary number
# and add corresponding actions to the array
if [[ ${binary:4:1} == "1" ]]; then
actions+=("wink")
fi
if [[ ${binary:3:1} == "1" ]]; then
actions+=("double blink")
fi
if [[ ${binary:2:1} == "1" ]]; then
actions+=("close your eyes")
fi
if [[ ${binary:1:1} == "1" ]]; then
actions+=("jump")
fi
if [[ ${binary:0:1} == "1" ]]; then
# reverse actions
for ((i = ${#actions[@]} - 1; i >= 0; i--)); do
rev+=("${actions[i]}")
done
actions=("${rev[@]}")
fi
# Add a comma between actions
echo "$(IFS=, ; echo "${actions[*]}")"