I am trying to run the below logic in a bash script
for i in {1..30}:
do
printf $i
if (( $i!=30 )); then
printf ","
fi
done
After hours of google searching, I found that in an if condition, square brackets [ ] are used for string comparisons and circular brackets are used for arithmetic operations (( )). I also found that -ne is used for string and != has to be used for arithmetic operations.
Inspite of my best efforts, I am unable to successfully run this simple logic where I need to run the loop 30 times and print the output with commas but skip the comma in last iteration.
for i in {1..30}; do printf $i ; if [ "$i" != "30" ]; then printf ","; fi; done-neetc are not used for string comparisons. Inside[ ]or[[ ]], you use-eq,-ne, etc for numeric comparisons and=(note: just one equal sign, although bash allows two),!=, etc for string comparisons. In arithmetic contexts like(( )), you use==(note: double-equal),!=, etc for numeric comparisons,=for assignment, and there are no string operators. In other words: the operators change completely depending on context.