Execute command with loop

[/CODE]I have a below command

ALTER TABLE `abc`  ADD PARTITION ( PARTITION 20130613 VALUES less than (DAY('13')));

Below is requirement

It runs in Loop

as DAY start with 13 and end with 100 along with this of counter "20130613" also increases per command

as the next command should be

ALTER TABLE `abc`  ADD PARTITION ( PARTITION 20130614 VALUES less than (DAY('14')));

Try:

perl -e 'use Date::Calc qw(:all);
for ($i=13;$i<=100;$i++) {
  ($y,$m,$d)=Add_Delta_Days(2013,05,31,$i);
  printf "ALTER TABLE `abc`  ADD PARTITION ( PARTITION %d%02d%02d VALUES less than (DAY(\047$i\047)));\n",$y,$m,$d
}'
1 Like

Thanks,

But I want this to be done by using Shell scripting

What have you tried and where are you stucK?

Please find the code I have wrote but Problem is unable to rotate c variable second thing If i want to use 1000 value for a That means I have write 1000 numbers in for loop
as Now Used just 10 in for loop want to change about 1000

#!/bin/sh
#
#Script to test for loop
#
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
a=$1
c=20130613
for i in 1 2 3 4 5 6 7 8 9 10

do
echo "ALTER TABLE seats  ADD PARTITION ( PARTITION $c VALUES less than (DAY( `expr $a \+ $i`)));"
c = expr $c + 1;
done

Try:

c=$(expr $c + 1)

or better:

c=$((c+1))

--
As to the other part, what OS and shell are you using?

Thanks

As for the Other Part

My Unix Info is below

$uname -a
Linux ndbd1 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

As I want to use for in 1 2 3 4 < 1000 Or any input number, for "FOR" Loop

Or For this I have to Migrate the code to while loop

If you are writing for /bin/sh then it would be best to use a while loop:

i=0
while [ $((i+=1)) -le 1000 ]
do 

If you can use bash, then you could use:

for i in {1..1000}
do

But I am guessing 20130613 is a date, right? You would need to take that into acount when incrementing $c

1 Like

I Used Below code in which "for i in `seq 1 $max`" worked for me

Thanks for the Help :slight_smile:

#!/bin/sh
#
#Script to test for loop
#
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
a=$1
max=$2
c=$3
for i in `seq 1 $max`
do
parti=parti
#echo "ALTER TABLE seats  ADD PARTITION ( PARTITION $c$parti VALUES less than (DAY( `expr $a \+ $i`)));"
#s=expr $a \+ $i
mysql -u root -p'root' test -e "ALTER TABLE seats  ADD PARTITION ( PARTITION $c$parti VALUES less than (`expr $a \+ $i`));"
c=`expr $c \+ 1`
done