I am new to Shell script. Just have a small question about some script I wrote.
pkgs="./cppcheck-1.48.tar.bz2 ./sshpass-1.04.tar.gz"
for pkg in $pkgs ; do
echo ${pkg} | grep -qE "bz2$"
if [ $? ] ; then
echo "here1"
else
echo "here2"
fi
done
It always prints here1, i.e. if condition is always true. I am not able to pin point any reason for it till now. Please point me towards right direction.
I know I can use shell’s switch command. Just want to know what is wrong with this. Shell is /bin/bash
[EDIT] – switch statement works flawlessly. For those who face this problem some other time
case ${pkg} in
*bz2) echo "here1";;
*) echo "here2";;
esac
if [ $? ]does not work as you might expect:It’s easy enough to fix by comparing against
0explicitly:So for your script: