I have found many answers about this on StackOverflow but I can't apply them to my code.
I used this command to get last day of current month:
LASTDAY=`cal $(date +"%m %Y") | grep . | fmt -1 | tail -1`
then I use this code:
for i in {1..${LASTDAY}}
do
# code for processing here!
done
But always get this warning: line 12: [: {1..31}: integer expression expected
and i is {1..31} but I expected i is a number in range [1,31]
I have tried this:
LASTDAY=$((LASTDAY+0))
LASTDAY=$( echo "$LASTDAY - 0" | bc )
LASTDAY=$(printf "%d" "$LASTDAY")
but it can't solve this problem. What's wrong in my code? and how to fix it?
Thanks in advanced.
bashhead, but I think the range expression like{1..31}maybe a new feature to bash. What version are you using?bash --version, should output your version. Add that bit of information to your question above. Good luck.bashfor a long time. However, it occurs prior to parameter expansion; since the braces do not contain two integer literals, the expression is treated literally as the string{1..${LASTDAY}}. Then parameter expansion is applied to produce{1..31}. That string is rejected by the code that receives$i, since that string is not an integer.