I would like to determine an action by the file extension of files in my array. For example, if the array match *.zip then do x. The script will not be located in the archive path.
archive_path="$HOME/Downloads/"
compressed_files=("`find "$achieve_path" -iname "*.zip" -o -iname "*.rar" -o -iname "*.7z"`")
for files in "${compressed_files[@]}"; do
echo "$files" ;
done
Update 1: This is another method that i tried. Returns no error, but also no results.
shopt -s nocasematch
dir="$HOME/Downloads/all/"
for file in "$dir*.@(.zip|.rar|.7z)" ; do
case "$file" in
*.rar)
echo "$file this is a rar file"
;;
*.zip)
echo "$file this is a zip file"
#...
;;
*.7z)
echo "$file this is a 7z file"
#...
;;
esac
done
Solution:
Tested Platforms: macOS Catalina
Notes: Upgrade your bash to the latest version, the latest macOS doesn't ship with the latest version of bash by default.
#!/usr/bin/env bash
shopt -s nocasematch
dir="$HOME/Downloads/all/"
for file in "$dir"* ; do
case "$file" in
*.rar)
echo "$file this is a rar file"
;;
*.zip)
echo "$file this is a zip file"
#...
;;
*.7z)
echo "$file this is a 7z file"
#...
;;
esac
done