Say that I want to make a script that extracts all tar.gz and tar.bz2 files that are listed as arguments. What I've done so far is something like:
if [[ $@ =~ .*"tar.gz".* ]] || [[ $@ =~ .*"tar.bz2".* ]]; then
for i in $@ =~ .*"tar.gz".*; do
tar -xvzf "$i"
done
for p in $@ =~ .*"tar.bz2".*; do
tar -xvjf "$p"
done
else
echo "tar.gz or tar.bz2 files required."
fi
The first line is successful at evaluating whether or not a tar.gz or tar.bz2 file exist, but my problem is the rest. The variables aren't set properly, and the script ends up trying to extract each variable listed with both extraction commands. How can I separate arguments that end with tar.gz and tar.bz2 to perform separate extractions?
[[ $* =~ ... ]](running a regex comparison against a string created by combining all arguments) is much clearer in terms of its behavior than[[ $@ =~ ... ]](trying to run a regex against an array; there's no documented guarantees in terms of what behavior will be in this case, or that it will continue to work in the future at all).for i in $@ =~ ...; dojust isn't defined behavior at all.