Incrementing in while loop

echo "Enter Starting id:"
  echo ""
  read rvst_strt_idxx
  echo ""
  echo "Enter Closing id:"
  echo ""
  read rvst_clsn_idxx  

FIELD1=$rvst_strt_idxx
FIELD2="USER" 
FIELD3="TEST"
FIELD4="12345"
FIELD5="00000"

  echo ""
  echo "INSERT INTO TABLE( FIELD1, FIELD2, FIELD3, FIELD4,
  FIELD5 ) "
  echo " VALUES ( '$FIELD1','$FIELD2','$FIELD3','$FIELD4',$FIELD5') "

Suppose Starting id is 10 and closing Id is 20
I want to write a script which will increment the value of field1 in each loop starting with 10 and increment until 20 is reached

ie I need 20 lines of Insert into scripts which will have field1 values from 10 to 20

I think i need to write a while loop which increments the value of field 1 by 1 and until it reaches the upper limit is (20 here).

Please help

echo "Enter Starting id:"
echo ""                  
read rvst_strt_idxx      
echo ""                  
echo "Enter Closing id:" 
echo ""                  
read rvst_clsn_idxx      

while [[ $rvst_strt_idxx -le $rvst_clsn_idxx  ]]
do

	FIELD1=$rvst_strt_idxx
	FIELD2="USER" 
	FIELD3="TEST"
	FIELD4="12345"
	FIELD5="00000"
	
	echo ""
	echo "INSERT INTO TABLE( FIELD1, FIELD2, FIELD3, FIELD4, FIELD5 ) "
	printf "VALUES ( '%s', '%s', '%s', '%s', '%s' );\n" $FIELD1 $FIELD2 $FIELD3 $FIELD4 $FIELD5
    rvst_strt_idxx=$(( $rvst_strt_idxx  + 1 ))
done > output.sql

try something like this.

This will run in any standard Unix shell:

printf "Enter Starting id: "
read start
printf "Enter Closing id: "
read close

## You should first check that the user has entered valid numbers

FIELD2="USER" 
FIELD3="TEST"
FIELD4="12345"
FIELD5="00000"
n=$start
while [ $n -le $close ]
do
  FIELD1=$n
  echo "INSERT INTO TABLE( FIELD1, FIELD2, FIELD3, FIELD4, FIELD5 ) "
  printf " VALUES ( '%s','%s','%s','%s','%s') \
       "$FIELD1" "$FIELD2" "$FIELD3" "$FIELD4" "$FIELD5"
  n=$(( $n + 1 ))
done > output.sql

Thanks .. It worke fine :slight_smile:

you can get rid of few more lines
1)if your FIELD2,FIELD 3...FIELD5 are fixed
use the values directly in printf i mean no need to assign them to variable and use it
2)there is no need to assingn $start to n
3)n=$(( $n + 1 )) is <===> n=$((n+1))

Who cares? Lines are cheap.

Avoiding redundancy and bloat is one thing; playing golf is best left for amusement.

It is good programming practice to avoid hard-coded values.

Variables should reflect their purpose. The start doesn't change; only the index changes. What if the start variable is needed later in the program?

The spaces are for legibility.

While POSIX allows the omission of the dollar sign on a variable in arithmetic expansion, the original wording of that part of the spec was not clear, and some major shells (dash and the BSD sh) did not allow it.

Therefore, for portability, it is a good idea to use the dollar sign.

It depends on individual
wheather he want to circle around variable like early 80's people used to do
at the end of the day they used to spend more time to give a name to variable rathere than the logic of the program
and I don't think lines are cheap for every one:)
I belive in doing 1+1 as 1+1 not as A=1;B=1 c=A+B

Good variable names help describe the logic of the program. Badly named variables leave the maintainer scratching his head.

Perhaps not -- if you're still using an early '80s computer.

That's fine for a one-off.

If it's a program you have to maintain, it can be the difference between making one change to update a variable and perhaps 20 or 30 changes to update the constants.