howto use a for(( exp1; [ test exp2 ]; exp3 )); construct


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting howto use a for(( exp1; [ test exp2 ]; exp3 )); construct
# 1  
Old 10-04-2009
howto use a for(( exp1; [ test exp2 ]; exp3 )); construct

Hi,
I am fairly novice at bash but not bad at C and so wondered if a for loop could be done as shown below:
Code:
#!/bin/sh

echo "Enter some strings , terminate with a single 'x':";
REPLY=;

#I am trying to replace this with a arithmetic for statement:
j=0;
while [ "${REPLY:0:1}" != "x" ];  do
  ((j++));

# My logic:  This works (1 is true):
# for((j=1; 1 ; j++ )); do  

# So shouldn't the test statement in here evaluate to 1 or 0 and work too?   

# for((j=1; [ "${REPLY:0:1}" != "x" ]; j++ )); do

# That gets this error line:
# syntax error: operand expected (error token is "[  != . ] ")

# I tried variants of the following and still no luck.
 
# for((j=1; [[ "${REPLY:0:1}" != "x" ]] ; j++ )); do
# for((j=1; ([ "${REPLY:0:1}" != "x" ]) ; j++ )); do
# for((j=1; (( [[ "${REPLY:0:1}" != "x" ]] )); j++ )); do

# Can it be done at all?

  read 
  echo "${REPLY}";
done;
echo "goodbye";

Am I nuts?
Thanks, Howard
# 2  
Old 10-04-2009
The only way I could make it work (so far ...):

bash code:
  1. #! /bin/bash
  2.  
  3. echo -n "Input: " ; read INPUT
  4.  
  5. for (( INDEX=0; $INDEX<${#INPUT}; INDEX++ ))
  6. do
  7.   if &#91; "${INPUT:$INDEX:1}" == "x" ]
  8.   then
  9.     break
  10.   else
  11.     echo "${INPUT:$INDEX:1}"
  12.   fi
  13. done
  14.  
  15. exit 0

Last edited by Neo; 10-04-2009 at 05:11 PM.. Reason: Debugging (sort of ...) .. .neo: added highlight=bash bbcode
# 3  
Old 10-04-2009
Quote:
Originally Posted by HowardL
Hi,
I am fairly novice at bash but not bad at C and so wondered if a for loop could be done as shown below:
Code:
#!/bin/sh

echo "Enter some strings , terminate with a single 'x':";
REPLY=;

#I am trying to replace this with a arithmetic for statement:


Why? That for syntax is non-standard.
Quote:
Code:
j=0;
while [ "${REPLY:0:1}" != "x" ];  do
  ((j++));

# My logic:  This works (1 is true):
# for((j=1; 1 ; j++ )); do  

# So shouldn't the test statement in here evaluate to 1 or 0 and work too?   

# for((j=1; [ "${REPLY:0:1}" != "x" ]; j++ )); do


The second expression must be an arithmetic expression, not a command.
Quote:
Code:
# That gets this error line:
# syntax error: operand expected (error token is "[  != . ] ")

# I tried variants of the following and still no luck.
 
# for((j=1; [[ "${REPLY:0:1}" != "x" ]] ; j++ )); do
# for((j=1; ([ "${REPLY:0:1}" != "x" ]) ; j++ )); do
# for((j=1; (( [[ "${REPLY:0:1}" != "x" ]] )); j++ )); do

# Can it be done at all?

  read 
  echo "${REPLY}";
done;
echo "goodbye";

Am I nuts?

Use the standard syntax:

POSIX code:
  1. j=0
  2. while [ "${REPLY:0:1}" != "x" ]
  3. do
  4.   read
  5.   j=$(( $j + 1 ))
  6. done

Last edited by cfajohnson; 10-04-2009 at 09:56 PM.. Reason: added highlight=bash bbcode tags; it's not bash, so changed to POSIX code
# 4  
Old 10-04-2009
The "standard syntax' is fine.
I had a feeling what I was trying to do was just plain wrong , but
there was a grain of uncertainty and I'm stubborn...
...like Dr. House's there! Nice effort.
I guess it's that challenge thing.
Thanks to both of you. Nice forum youhave here.
Howard();
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Construct path

Hi, I need to construct the below path from the two available directory path, O/P /home/data/test/run/ht/WEB/HTML /home/data/test/run/ht/WEB/JSP /home/data/test/run/ht/WEB/CSS Path:1 ------ /home/data/test/run/ Path:2 ------ /home/data/share/app/01/lang/ht/WEB/HTML... (5 Replies)
Discussion started by: vel4ever
5 Replies

2. UNIX for Dummies Questions & Answers

awk construct unfamiliar to me

Please help me out: I've seen this construct awk '{...}1'several times, like in scrutinizer's today's post awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i}1' infilebut I can't find (manuals, man pages, internet FAQs,...) an explanation of what it does resp. stands for. Any hint is appreciated! (5 Replies)
Discussion started by: RudiC
5 Replies

3. Shell Programming and Scripting

Help with if-else construct

Hi all i have been trying to do a small 'question and answer' script using if-else statement and a combination of pipe. I have succeeded in allowing the user to login with user name and password stored in a sequence username/password in a file named "pass" like this: echo "please enter your... (14 Replies)
Discussion started by: arikutex
14 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. AIX

send: spawn id exp3 not open

Hi, AIX 5.3 Trying to implement script that change oracle password. Getting following errors: send: spawn id exp3 not open while executing "send "xxx\r" Any idea? (4 Replies)
Discussion started by: groosha
4 Replies

7. Shell Programming and Scripting

syntax error in the if construct

Hi can anyone tell me why is this code giving error mode=$1 if ] || ] then echo "MODES:" exit 1 fi Thanks (5 Replies)
Discussion started by: Anteus
5 Replies

8. Shell Programming and Scripting

if-else construct not working

Hi all, Sorry to ask this easy question but I am stuck. In a scenario i am executing one shell script which contains a if - else construct : if ; then echo $line $line >> successful_build.txt else $line >> failed_services.txt fi explaination : if the... (5 Replies)
Discussion started by: bhaskar_m
5 Replies

9. Shell Programming and Scripting

ksh construct

Hi Guys, could someone tell me what this ksh construct does typeset -r PROG_PWD=${0%/*} does I understand the -r for readonly but I would very much appreciate a definitive account of what this will set $PROG_PWD to. If I run this at the cmd line it it gets set to /usr/bin but I would... (2 Replies)
Discussion started by: ajcannon
2 Replies

10. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies
Login or Register to Ask a Question