Korn Shell Loop Problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Loop Problems
# 1  
Old 01-05-2014
BSD 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.

Code:
# for i in {$A..$B}; do echo $C.$i; done 
219.19.188.{152..155}

Any tips for a BSD noobie?
I'm using OpenBSD if that helps.
# 2  
Old 01-05-2014
You cannot perform a variable expansion inside a sequence expression.

Another approach would be using seq
Code:
for i in $( seq ${A} ${B} ); do echo $C.$i; done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-06-2014
I actually tried that too but it appears seq is not installed on my system:

Code:
# for i in $(seq $A $B); do echo $C.$i; done 
ksh: seq: not found

I tried adding it with pkg_add:

Code:
# pkg_add -r seq
Can't find seq

Do you know if the package for this is called something different?

---------- Post updated 01-06-14 at 12:11 AM ---------- Previous update was 01-05-14 at 11:37 PM ----------

I thought coreutils was the package that contained seq, but I'm still finding this is not installed. pkg_add can't find sysutils either...
# 4  
Old 01-06-2014
Use a while loop instead:
Code:
while [ $A -le $B ]
do
        print $C.$A
        (( A++ ))
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 01-06-2014
I don't doubt this works on your system, but I'm getting some strange output for that?

Code:
# echo $A 
152
# echo $B
155
# while [ $A -le $B ]; do echo $C.$i; ((A++)); done 
219.19.188.10
219.19.188.10
219.19.188.10
219.19.188.10

---------- Post updated at 12:36 AM ---------- Previous update was at 12:27 AM ----------

Got it! I didn't see to change $C.$i to $C.$A. Thanks again Yoda!!!
# 6  
Old 01-06-2014
ksh in OpenBSD is pdksh, which is mostly ksh88, which does not support the brace expansion syntax you were attempting to use.

BSD systems typically have jot instead of seq.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 01-06-2014
To add to what alister mentioned, you could probably install a valid ksh93 version on your system(s).

--
Quote:
Originally Posted by Yoda
You cannot perform a variable expansion inside a sequence expression.
[..]
Yes you can in ksh, the parsing order for brace expansion is different than in bash.

Quote:
Originally Posted by Yoda
Use a while loop instead:
Code:
while [ $A -le $B ]
do
        print $C.$A
        (( A++ ))
done

If you use arithmetic evaluation with (( .. )), then why not use a for loop:
Code:
for (( i=$A; i<=$B; i++ )); do echo "$C.$i"; done

Or to keep it POSIXy:
Code:
i=$A
while [ $i -le $B ]; do
  printf "%s\n" "$C.$i"
  i=$((i+1))
done


Last edited by Scrutinizer; 01-06-2014 at 04:05 AM..
This User Gave Thanks to Scrutinizer For This Post:
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

[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 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 for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

3. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

4. AIX

for loop problems

I have a script that loops through a list of users to list files owned by these users for u is `cat users.list` do echo $u >> result.out find /home -user $u >> result.out find /var -user $u >> result.out find /opt -user $u >> result.out find /usr -user $u >> result.out done an so... (3 Replies)
Discussion started by: Dardeer
3 Replies

5. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

6. 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

7. Solaris

Problems with korn shell script

Hey Guys, I'm looking for some advice about a korn shell script I've written. I've spent hours googling for an answer hopefully someone here can help me out. Basically the part of the script I'm having problems with is when I need to SFTP a file from one server to another. The line looks... (6 Replies)
Discussion started by: hilather
6 Replies

8. 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

9. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies

10. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question