GIDForums  

Go Back   GIDForums > Computer Forums > Computer Software Forum - Linux
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Oct-2009, 10:51
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

bash: use [ test in a for(( loop


Is there a way to get an arithmetic for(( loop
to use a [ or [[ test?
Example:
Code:
#!/bin/sh echo "Enter some strings , terminate with a single dot:"; REPLY=""; I am trying to replace this with a arithmetic for statement: # j=0; # while [ "${REPLY:0:1}" != "." ]; do # ((j++)); # This works: # for((j=1; 1 ; j++ )); do # Shouldn't the test statement in here evaluate to a numeric too? # for((j=1; [ "${REPLY:0:1}" != "." ] ; j++ )); do # That gets this error line: # syntax error: operand expected (error token is "[ != . ] ") # I tried variants of the following and still no luck. # Can it be done at all? # for((j=1; [ [ "${REPLY:0:1}" != "." ] ] ; j++ )); do # for((j=1; ([ "${REPLY:0:1}" != "." ]) ; j++ )); do # for((j=1; (( [ "${REPLY:0:1}" != "." ] )); j++ )); do read echo "${REPLY}"; done; echo "goodbye";
Thanks
  #2  
Old 04-Oct-2009, 21:43
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: bash: use [ test in a for(( loop


Hello Howard,

I tried Googling around for some 'for loop' examples having a string test in the condition, but could not find any...

However, I did have the idea to use a function in the condition to do the same? Maybe this could help with the goal?
Code:
#!/bin/sh echo "Enter some strings, terminate with a single dot:"; REPLY=""; j=0; function check_reply { retVal=1; if [ "${REPLY}" == "." ] then let retVal=0; fi echo ${retVal}; } for((j=0; $(check_reply)==1 ; j++)); do read echo "${REPLY}"; done; echo "goodbye";
Note, though, that there is a 'side affect' of 'j' having +1 more than the input strings given because of the first check of REPLY's value.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 05-Oct-2009, 11:53
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: bash: use [ test in a for(( loop


Ha, so it will take the result of your command there:
CPP / C++ / C Code:
for((j=0; $(check_reply)==1 ; j++)); do
interesting.
But I don't think that would build more efficient code , right?

From what I've heard so far it looks like it can't be done...
dave would give us the old "FULL STOP!" and all that
So I'll have to learn to accept this "while" form which is really that bad:
Code:
REPLY=; j=0; while [ "${REPLY:0:1}" != "." ]; do ((j++)); # <-- make believe it's a for() loop... read; echo $j; done; (Of course the increment can be moved to the bottom of the block the)
But still "personal problem" flares when I see stuff like this. There's got to be a way!:
Code:
~> i=1 ~> [ $i -eq 1 ]; echo $?; 0 ~> [ $i -eq 2 ]; echo $?; 1 ~> [ $i -eq 1 ]; printf "%x \n" $?; 0 ~> [ $i -eq 2 ]; printf "%x \n" $?; 1 ~> $([ $i -eq 1 ]); printf "%x \n" $?; 0 ~> $([ $i -eq 2 ]); printf "%x \n" $?; 1 ~> x= ~> x=$([ $i -eq 1 ]); printf "%x \n" $x; 0 ~> x=$([ $i -eq 2 ]); printf "%x \n" $x; 0 ~> #hmmm ~> x=1; x=$([ $i -eq 2 ]); printf "%x \n" $x; 0 ~> #ouch! , They win , I quit... let's move on (yeah right)
Thanks for taking the time turbo.
Howrd();
  #4  
Old 05-Oct-2009, 12:25
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: bash: use [ test in a for(( loop


Quote:
Originally Posted by Howard_L
But I don't think that would build more efficient code , right?
Since there is a side-affect (though minor) no.
Quote:
Originally Posted by Howard_L
Code:
REPLY=; # make believe it's a for() loop... j=0; while [ "${REPLY:0:1}" != "." ]; do ((j++)); read; echo $j; done;
but darn it, when I see stuff like this I think there's got to be a way
I agree, but I wasn't able to find a way either. While investigating, though, I did find the 'until' syntax to be interesting too. (very similar to the while)
Quote:
Originally Posted by Howard_L
Thanks for taking the time turbo.
Howrd();
No problem. (Things like this make a great refresher -- especially when one gets away from the 'tinkering' for awhile)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Looking for test clients paradoxservers Free Web Hosting 0 04-Jan-2008 13:10
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
CSS Layout question oihjk Web Design Forum 3 28-May-2003 12:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.