# Execute command with loop

**URL:** <https://community.unix.com/t/execute-command-with-loop/331457>\
**Category:** Shell Programming and Scripting\
**Created:** [July 7, 2013, 11:00am UTC](https://community.unix.com/t/execute-command-with-loop/331457 "2013-07-07T11:00:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushik02018](https://community.unix.com/letter_avatar/kaushik02018/32/6_449c533f586939b6e5f246632a46421f.png) [@kaushik02018](https://community.unix.com/u/kaushik02018)\
**Post date:** [July 7, 2013, 11:00am UTC](https://community.unix.com/t/execute-command-with-loop/331457/1 "2013-07-07T11:00:01Z")

</div>

[/CODE]I have a below command

```nohighlight
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

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

```

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [July 7, 2013, 11:14am UTC](https://community.unix.com/t/execute-command-with-loop/331457/2 "2013-07-07T11:14:05Z")

</div>

Try:

```nohighlight
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
}'

```

---

<div class="post-metadata">

**Author:** ![kaushik02018](https://community.unix.com/letter_avatar/kaushik02018/32/6_449c533f586939b6e5f246632a46421f.png) [@kaushik02018](https://community.unix.com/u/kaushik02018)\
**Post date:** [July 7, 2013, 11:24am UTC](https://community.unix.com/t/execute-command-with-loop/331457/3 "2013-07-07T11:24:55Z")

</div>

Thanks,

But I want this to be done by using Shell scripting

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 7, 2013, 11:36am UTC](https://community.unix.com/t/execute-command-with-loop/331457/4 "2013-07-07T11:36:34Z")

</div>

What have you tried and where are you stucK?

---

<div class="post-metadata">

**Author:** ![kaushik02018](https://community.unix.com/letter_avatar/kaushik02018/32/6_449c533f586939b6e5f246632a46421f.png) [@kaushik02018](https://community.unix.com/u/kaushik02018)\
**Post date:** [July 7, 2013, 11:43am UTC](https://community.unix.com/t/execute-command-with-loop/331457/5 "2013-07-07T11:43:38Z")

</div>

> [@scrutinizer](#):
>
> 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

```plaintext
#!/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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 7, 2013, 12:11pm UTC](https://community.unix.com/t/execute-command-with-loop/331457/6 "2013-07-07T12:11:32Z")

</div>

Try:

```nohighlight
c=$(expr $c + 1)

```

or better:

```nohighlight
c=$((c+1))

```

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

---

<div class="post-metadata">

**Author:** ![kaushik02018](https://community.unix.com/letter_avatar/kaushik02018/32/6_449c533f586939b6e5f246632a46421f.png) [@kaushik02018](https://community.unix.com/u/kaushik02018)\
**Post date:** [July 7, 2013, 12:24pm UTC](https://community.unix.com/t/execute-command-with-loop/331457/7 "2013-07-07T12:24:59Z")

</div>

> [@scrutinizer](#):
>
> Try:
> 
> ```plaintext
> c=$(expr $c + 1)
> 
> ```
> 
> or better:
> 
> ```plaintext
> 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

```plaintext
$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

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 7, 2013, 12:40pm UTC](https://community.unix.com/t/execute-command-with-loop/331457/8 "2013-07-07T12:40:33Z")

</div>

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

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

```

If you can use bash, then you could use:

```nohighlight
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`

---

<div class="post-metadata">

**Author:** ![kaushik02018](https://community.unix.com/letter_avatar/kaushik02018/32/6_449c533f586939b6e5f246632a46421f.png) [@kaushik02018](https://community.unix.com/u/kaushik02018)\
**Post date:** [July 7, 2013, 1:40pm UTC](https://community.unix.com/t/execute-command-with-loop/331457/9 "2013-07-07T13:40:27Z")

</div>

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

Thanks for the Help 🙂

```nohighlight
#!/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

```
