[Solved] Issue with using for loop as for in {2..6} in korn shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Issue with using for loop as for in {2..6} in korn shell
# 1  
Old 06-28-2013
[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays ,
The following code works

Code:
for ctcol in 2 3 4 5 6;
do 
set -A a$ctcol $(cut -d, -f $ctcol test_file)
done

how ever this does not work
Code:
for ctcol in {2..6};
do 
set -A a$ctcol $(cut -d, -f $ctcol test_file)
done

Kindly let me know what is wrong with it

Last edited by 100bees; 06-28-2013 at 02:21 AM..
# 2  
Old 06-28-2013
From what I know, range expansion {2..6} is a part of modern bash shell's parameter expansion feature and not a feature of ksh.

An alternative that will work for sure (even in old bourne shells):
Code:
i=2
while [ $i -le 6 ]
do
    <statements>
    i=`expr $i + 1`
done

Another alternative that will work on ksh:
Code:
for i in $(seq 2 6)
do
    <statements>
done


Last edited by balajesuri; 06-28-2013 at 02:46 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-28-2013
Quote:
Originally Posted by balajesuri
From what I know, range expansion {2..6} is a part of modern bash shell's parameter expansion feature and not a feature of ksh.

An alternative that will work for sure (even in old bourne shells):
Code:
i=2
while [ $i -le 6 ]
do
    <statements>
    i=`expr $i + 1`
done

Another alternative that will work on ksh:
Code:
for i in $(seq 2 6)
do
    <statements>
done

In any version of ksh newer than November 16, 1988, you can also use:
Code:
for ((i=2; i<=6; i++))
do  <statements>
done

Although not available in the original 1993 version of ksh, the:
Code:
for ctcol in {2..6}
do  ...
done

does work in recent versions of ksh93 including version s+ (sh (AT&T Research) 1993-12-28 s+) as provided with Apple's OS X 10.7.5.

And, in any standards conforming shell (including ksh versions newer than 1988 and bash) you could also use:
Code:
i=2
while [ $i -le 6 ]
do  <statements>
    ((i++))
done

The seq utility suggested above by balajesuri will work on most Linux systems, but is not available on some UNIX systems.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-28-2013
Quote:
Originally Posted by Don Cragun
Although not available in the original 1993 version of ksh, the:
Code:
for ctcol in {2..6}
do  ...
done

does work in recent versions of ksh93 including version s+ (sh (AT&T Research) 1993-12-28 s+) as provided with Apple's OS X 10.7.5.
Thanks for the detailed explanation along with the release dates of ksh. Very informative Smilie
# 5  
Old 07-02-2013
Thanks that was really helpful Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print the missing fields outside the for loop in Korn shell?

I have 2 for loop in my program , first one will list files based on timestamp and second one list the files based on type(RPT / SUB_RPT).Here is my code: #!/bin/ksh STG_DIR=/home/stg for pattern in `find $STG_DIR -type f -name 'IBC*csv' | awk -F'' '{print $(NF-1)}' | sort -u` do echo... (2 Replies)
Discussion started by: ann15
2 Replies

2. Shell Programming and Scripting

Calling sqlplus from Korn shell heredoc issue

Hi, I am facing an issue wherein some temporary files (here docs) are getting created in /tmp and are not getting deleted automatically. When i check the list of open files with below command i can see one file is getting appended continuously.(In this case /tmp/sfe7h.34p) The output is... (4 Replies)
Discussion started by: Navin_Ramdhami
4 Replies

3. Shell Programming and Scripting

Korn Shell Loop Problems

Very new to the Korn Shell, but I've been looking up loops online and it seems this should work. I'm just trying to convert an ip range in variables $A and $B and iterate the individual ip's out to new lines. Unfortunately I get {152..155} instead of 152, 153, 154, and 155. # for i in {$A..$B};... (8 Replies)
Discussion started by: Azrael
8 Replies

4. Shell Programming and Scripting

[Solved] How to refer more than 9 command line inputs for a scripts in korn shell?

Hi all, I have a script which should take more than 9 command line inputs while running. Likescript.sh a s d f g h j j k l o p i u y t r e w Now in the script if I have to access one of the input which is at position after 9, in this case say 'p' then how can I do that? echo $12 will not work... (15 Replies)
Discussion started by: pat_pramod
15 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Korn Shell execution

There are two Korn Shell scripts : script_1.ksh ( located in /home/dir1 ) script_2.ksh ( located in /home/dir2 ) Content of script_2.ksh is #!/usr/bin/ksh echo "Hello world.." The script_2.ksh is called from within script_1.ksh using the following command : ./home/dir2/script_2.ksh but... (7 Replies)
Discussion started by: kumarjt
7 Replies

6. Shell Programming and Scripting

korn shell for loops with expect issue

Hi I have the following Korn script having multiple for loops. #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal for d in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 i22 23 24 25 26; do for i in 01 02 03 04 05 06 07 ; do for h in 00 01 02 03 04 05 06 07 08 09 10 11 12... (2 Replies)
Discussion started by: cic
2 Replies

7. Shell Programming and Scripting

Help With Constructing A Korn Shell Search Loop

Hello All, I am a statistician and I am very new to the world of ksh programming. Daily, I analyze millions of rows of data and land information to DB2 tables. I have recently been asked to develop a ksh script to FTP an export file containing line item data from the production environment to the... (2 Replies)
Discussion started by: jonesdk5
2 Replies

8. Shell Programming and Scripting

simple if/then issue in korn shell

When I run this, it tests to Y, but why? If I take the double quotes off of test, i get the same. # !/bin/ksh TEST="N" if ; then echo $TEST" yes" else echo "no" fi (4 Replies)
Discussion started by: guessingo
4 Replies

9. Shell Programming and Scripting

Unix Korn Shell Array Issue (SunOS)

Hello, I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total... (5 Replies)
Discussion started by: Janus
5 Replies

10. Shell Programming and Scripting

Korn Shell Loop question

I'm needing help with assigning variables inside a while loop of ksh script. I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents. ... (4 Replies)
Discussion started by: stevefox
4 Replies
Login or Register to Ask a Question

Featured Tech Videos