for loop throwing an error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop throwing an error
# 1  
Old 02-12-2010
for loop throwing an error

Hi Guys,

I am trying a simple for loop which is throwing an error.
Below is my code:

Code:
#/bin/sh

set -A array "are" "you" "there";

for ( i = 0 ; i < ${#array[@]} ; i++ )
do
echo ${array[$i]}
done

I am getting this error

Code:
tci001wasd02 $ sh -vx array_for.sh
#/bin/sh

set -A array "are" "you" "there";
+ set -A array are you there

for ( array_for.sh[5]: 0403-057 Syntax error at line 5 : `(' is not expected.

I even tried with "((" in the for loop.

Thanks for your help in advance.

Regards,
Magesh
# 2  
Old 02-12-2010
Something like this should work:

Code:
 
#/bin/sh
array=( are you there )
i=0
j=${#array[*]}
while [ $i -ne $j ]
do
echo ${array[$i]}
i=`expr $i + 1`
done

# 3  
Old 02-12-2010
ya vi-curious,,

I have wrote a script with While loop.. its working fine..but i want to try for loop syntax in unix instead of while..

isn't "for loop" not possible in unix?

My while script:

Code:
#/bin/sh

set -A array "are" "you" "there";
a=0;
while [ "$a" -le "2" ]
do
for i in ${array[$a]}
do
echo $i;
a=$((a+1));
done
done

# 4  
Old 02-12-2010
I did the while loop so that it would not be dependent on the array size but I see from your while loop that you were not concerned about that. So, for loop looks like this:

Code:
#/bin/sh

array=( are you there )

#for i in 0 1 2
for i in ${!array[@]}
do
echo ${array[$i]}
done


Last edited by Vi-Curious; 02-12-2010 at 06:30 AM.. Reason: Edited based on cfajohnson post
# 5  
Old 02-12-2010
Quote:
Originally Posted by mac4rfree
Hi Guys,

I am trying a simple for loop which is throwing an error.
Below is my code:

Code:
#/bin/sh

set -A array "are" "you" "there";


The standard Unix shell does not have arrays, so you should use a shebang that indicates which shell you are using.

That syntax is, I believe, ksh93, so your script should begin with:
Code:
#!/usr/bin/ksh93

(Or wherever your ksh93 is hiding.)
Quote:
Code:
for ( i = 0 ; i < ${#array[@]} ; i++ )


That is non-standard syntax; it isn't even correct for bash and ksh93 which use something similar:
Code:
for (( i = 0 ; i < ${#array[@]} ; i++ ))

I think that's right; I avoid non-standard syntax when it offers nothing more than the standard.

The following is not standard because of the array, but the for syntax is standard:
Code:
for i in "${!array[@]}"

Quote:
Code:
do
echo ${array[$i]}
done

I am getting this error

Code:
tci001wasd02 $ sh -vx array_for.sh
#/bin/sh

set -A array "are" "you" "there";
+ set -A array are you there

for ( array_for.sh[5]: 0403-057 Syntax error at line 5 : `(' is not expected.

I even tried with "((" in the for loop.
# 6  
Old 02-12-2010
@cfajohnson,
Thanks for pointing out the errors, i have changed the sh to ksh93.

But even now i am not able to run the for command.

Code:
tci001wasd02 $ cat array_for.sh
#/bin/ksh93

set -A array "are" "you" "there";

for (( i = 0 ; i < ${#array[@]} ; i++ ))
do
echo ${array[$i]}
done
tci001wasd02 $ sh -vx array_for.sh
#/bin/ksh93

set -A array "are" "you" "there";
+ set -A array are you there

for ((array_for.sh[5]: 0403-057 Syntax error at line 5 : `(' is not expected.

the command u mentioned above

Code:
for i in "${!array[@]}"

will give me all the elements in the array. But i want only certain elements from the array, which i wanted to do it using a for loop...

Please help me in doing the above.

Thanks & Regards,
Magesh
# 7  
Old 02-12-2010
Quote:
Originally Posted by mac4rfree
@cfajohnson,
Thanks for pointing out the errors, i have changed the sh to ksh93.

But even now i am not able to run the for command.

Code:
tci001wasd02 $ cat array_for.sh
#/bin/ksh93

set -A array "are" "you" "there";


I would use the more portable (though arrays are not standard) form of assigning an array:
Code:
array=( are you there )   ## doesn't work in ksh88, AFAIK

Quote:
Code:
for (( i = 0 ; i < ${#array[@]} ; i++ ))
do
echo ${array[$i]}
done
tci001wasd02 $ sh -vx array_for.sh
#/bin/ksh93

set -A array "are" "you" "there";
+ set -A array are you there

for ((array_for.sh[5]: 0403-057 Syntax error at line 5 : `(' is not expected.


Then use the standard syntax.

(That works for me in ksh93; perhaps you are using a different version of ksh93. All the more reason to use the standard syntax; it will work in all versions and other shells.)
Quote:

the command u mentioned above

Code:
for i in "${!array[@]}"

will give me all the elements in the array. But i want only certain elements from the array, which i wanted to do it using a for loop...

Both forms give you all the elements in the array. You have to select the ones you want inside the loop.

Always use the standard syntax when possible without losing efficiency. Non-standard features may (and often do) change from one verion of a shell to the next.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Prustat throwing error only in zones not in global

Hi, Prustat is throwing error only in zones. But it is working fine in global. dtrace: invalid probe specifier /* ** The following is a trimmed version of two seperate DTrace scripts: ** ** socketsnoop.d - snoop TCP network socket traffic by process. ** This is intended to identify the... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. UNIX for Beginners Questions & Answers

Make throwing error while compiling python3.6 on Solaris10

Hi, I have downloaded gzipped source tarball of python3.7. I have tried to compile like below, ./configure --prefix=/directory Compiling seems to be ok..But when running make all..Getting too many missing initializer warnings /python3.6/Python-3.6.0/Modules/_cursesmodule.c: At top level:... (1 Reply)
Discussion started by: Sumanthsv
1 Replies

3. Shell Programming and Scripting

For Loop throwing error

Hello Gurus, I am writing one script at linux. The logic is There is a find command which will find some specific files daily and store at a variable Then echo that variable . Now when I am trying to read the variable by using for loop it is throwing error as below:cat: CKDT.dat: No such... (5 Replies)
Discussion started by: pokhraj_d
5 Replies

4. Shell Programming and Scripting

Function throwing an error

Hi All I have two shell scripts where the second is getting invoked from the first. E.g. test1.sh and test2.sh Within test1, the code is something like this: #!/bin/bash . test2.sh usage() { echo "..." echo "....." } SRC=$1 DEST=$2 case "$3" in tran) doTran ;; *)... (7 Replies)
Discussion started by: swasid
7 Replies

5. Shell Programming and Scripting

awk--throwing an error

Hi all, I have below code sqlplus -s ext/exo@TIS << EOF whenever sqlerror exit failure rollback; set echo off set head off set serveroutput on set termout on set trimspool on SPOOL $SPOOL_FILE select 'ROWS '|| ' '||decode((count(Part_no)),0,'With greater values not... (3 Replies)
Discussion started by: Kiransagar
3 Replies

6. Shell Programming and Scripting

Tr--translate is throwing an error

Dear all, I would like to count the no;of word "INFORMATION" in a file called alt.lst and output to a unix variable INFORMATION.so to do this I wrote the below code INFORMATION=echo 'INFORMATION' | tr -cs 'A-Za-z' '\n' < /app/tisq005/01/home/tisq005b/scripts/alt.lst | grep -c "INFORMATION"... (2 Replies)
Discussion started by: Kiransagar
2 Replies

7. Shell Programming and Scripting

POCO install throwing error

All , I am trying to install POCO lib on Solaris server. When I do make -s,I am getting the following error. make: Fatal error in reader: Makefile, line 10: Unexpected end of line seen Few of the content: # # Makefile # # The global Makefile for POCO # #sinclude config.make ... (0 Replies)
Discussion started by: prasperl
0 Replies

8. Shell Programming and Scripting

for loop with internal unix command in statement throwing error

Hi I've gotten a plugin script that won't run. I keeps throwing an error at the following line. for BARCODE_LINE in `cat ${TSP_FILEPATH_BARCODE_TXT} | grep "^barcode"` do #something done The error reads ... (3 Replies)
Discussion started by: jdilts
3 Replies

9. Red Hat

formating a USB which is throwing error write delayed

Hello, while i was saving a web file directing on to usb location there was some network problem which result in error WRITE DELAYED on windows xp. so pen drive is not getting completely formated(show as Windows was unable to complete format).From then onwords it is not allowing to copy... (0 Replies)
Discussion started by: amarnathbasis8
0 Replies

10. UNIX for Dummies Questions & Answers

forcefully throwing error : unix script

how can i make one script fail if some condition is not satisfied.i m writing if ..else logic in script.i need some standard command to do that ,, (1 Reply)
Discussion started by: dr46014
1 Replies
Login or Register to Ask a Question