I'm trying to get all jar files in a directory and store them in an array. Currently, it's not working as expected and I think it's because it's an array inside an array? Let me know if it is and how would I fix it. When I echo the length of the array, it's always 1 yet when I loop over it, it can have more than 1 file.
Code:
#!/bin/bash
cd "/home/ubuntu/RLCraft" || exit 1
jars=("$(ls *.jar)")
echo "${#jars[@]}" -gt "1"
if (("${#jars[@]}" > "1")); then
echo "yes"
else
echo "no"
fi
for i in "${jars[@]}"; do
echo "$i"
done
if [ "${#jars[@]}" -gt 1 ]; then
echo "Choose a server jar:"
for i in "${jars[@]}"; do
echo "$i"
done
read -r serverJar
else
serverJar="${jars[0]}"
fi
Expected output:
yes
magma-server.jar
minecraft_server.1.12.2.jar
Resulting output:
no
magma-server.jar
minecraft_server.1.12.2.jar
typeset -p jarsto see what's in the array; does the array contain what you think it should contain?