The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with incrementing date in while loop. Robert W.Mills Shell Programming and Scripting 3 08-05-2008 02:08 PM
No. post not incrementing incredible Post Here to Contact Site Administrators and Moderators 5 07-22-2008 12:59 PM
Port num incrementing coolkid Shell Programming and Scripting 5 05-28-2008 06:51 PM
New iteration of for-loop without incrementing? jeriryan87 Shell Programming and Scripting 0 07-02-2007 03:13 PM
incrementing a for loop run_time_error Shell Programming and Scripting 2 03-28-2005 03:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-09-2009
ultimatix ultimatix is offline
Registered User
  
 

Join Date: Oct 2008
Location: Mumbai
Posts: 55
Incrementing in while loop


Code:
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

Last edited by vidyadhar85; 06-10-2009 at 02:58 PM.. Reason: code tag added
  #2 (permalink)  
Old 06-09-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,813

Code:
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.
  #3 (permalink)  
Old 06-09-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by ultimatix View Post
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

This will run in any standard Unix shell:


Code:
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

  #4 (permalink)  
Old 06-10-2009
ultimatix ultimatix is offline
Registered User
  
 

Join Date: Oct 2008
Location: Mumbai
Posts: 55
Smile

Thanks .. It worke fine :-)
  #5 (permalink)  
Old 06-10-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,423
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))
  #6 (permalink)  
Old 06-10-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by vidyadhar85 View Post
you can get rid of few more lines

Who cares? Lines are cheap.

Avoiding redundancy and bloat is one thing; playing golf is best left for amusement.
Quote:
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

It is good programming practice to avoid hard-coded values.
Quote:
2)there is no need to assign $start to n

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?
Quote:
3)n=$(( $n + 1 )) is <===> n=$((n+1))

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.
  #7 (permalink)  
Old 06-10-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,423
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0