Reputation: 61
In a folder i have file1 and file2,
can i generate a file csv (xls) with similar format
i have tried this code but not work
for fullname in *.rar; do
if [[ "$fullname" != *X* ]]; then
name="${fullname%.*}"
echo $name Y check this http://foo.$fullname >> csv.txt
elif [[ "$fullname" == *Z* ]]; then
echo $name Z check this http://foo.$fullname >> csv.txt
else
echo $name X check this http://foo.$fullname >> csv.txt
fi
done
this is the my output;
Why in line1 not show the name file1_X ? Why in line3 not show after name file1_Z the character Z ?
Upvotes: 0
Views: 53
Reputation: 11786
Your if condition is wrong - you are saying "If the file does not have an X in the name, then do this:" which includes both file_Y and file1_Z, so they are both processed by the first condition. You should put the Z condition first, since it is the most specific. Line 1 of your example output does not show the filename as expected because you only set $name
when the Y condition is met. You should instead move the $name
variable assignment outside the if:
for fullname in *.rar; do
name="${fullname%.*}"
if [[ "$fullname" == *Z* ]]; then
echo $name Z check this http://foo.$fullname >> csv.txt
elif [[ "$fullname" != *X* ]]; then
echo $name Y check this http://foo.$fullname >> csv.txt
else
echo $name X check this http://foo.$fullname >> csv.txt
fi
done
A switch statement might be better:
for fullname in *.rar; do
name="${fullname%.*}"
case $name in
*Z* ) match=Z;;
*Y* ) match=Y;;
* ) match=X;;
esac
echo "$name $match check this http://foo.$fullname" >> csv.txt
done
Upvotes: 1