PDA

View Full Version : ksh arrays


mtx
05-06-2008, 08:21 AM
i have started to use (better said learn) ksh as a scripting language about a week ago. as i was reading "Learning the Korn Shell (2nd edition)" i have reached at some point the array (http://unix.compufutura.com/korn/ch06_04.htm) part.
so i have tried to make the following simple script.

# cat array.sh
#!/usr/local/bin/ksh93

K=~/bin

cd $K
KA=$(ls $PRE.*)
print ${#KA }

running the script yields the following output

# ./array.sh
1

As far as i have read in the book this should print the total number of array components.
Using the -x feature of ksh i have the following output:

# ksh93 -x array.sh
+ ls addvpn.sh disk.size.sh ksh.sh s.test.sh array.sh vpn.sh
+ KA=$'addvpn.sh\ndisk.size.sh\nksh.sh\ns.test.sh\ns sh.connect.sh\nvpn.sh'
+ print 1

Trying to print an element from the array let's say the first

...
print ${KA[1]}

doesn't work either. The out put is blank.

# ksh93 -x array.sh
+ ls addvpn.sh disk.size.sh ksh.sh s.test.sh ssh.connect.sh vpn.sh
+ KA=$'addvpn.sh\ndisk.size.sh\nksh.sh\ns.test.sh\ns sh.connect.sh\nvpn.sh'
+ print ''

what am i missing here? thanks

all the best,
v

corey_james
05-06-2008, 08:33 AM
I really have to question why you're writing a script for KSH and not using a more popular shell such as bourne or BASH ?

Ignoring that .... Maybe this will clear some things up:


$ set -A someArray `ls *.jpg`
$ echo ${somearray }
blah.jpg
something.jpg
file.jpg
aa.jpg
$echo ${somearray[0]}
blah.jpg


make any sense?

If you're going to use a variable to store the contents of the output of an application you need to put it in these backquotes ` `

mtx
05-06-2008, 08:52 AM
That doesn't work either. I was using $() for command substitution because i found that this is the proper way to do it in ksh (from the man page and book i mentioned).
I am using ksh because i found out from a bsdforums post (http://www.freebsdforums.com/forums/showthread.php?t=51133&highlight=shell) that people are using ksh for scripting because it has much more features when it comes to scripting.
Ok i don't know if this is the wisest decision so i am gonna ask your opinion about which shell do you use for scripting?
thank you

all the best,
v

ocicat
05-06-2008, 09:40 AM
I was using $() for command substitution because i found that this is the proper way to do it in ksh (from the man page and book i mentioned).
This isn't a Korn shell-ism as much as it is what is advocated by The Open Group's Unix specification:

http://www.opengroup.org/onlinepubs/7990989775/xcu/chap2.html

mtx
05-06-2008, 09:59 AM
thanks ocicat for clarification. i got it working. i had to use

set -A

to define the array. i wasn't using this because in the man page and book it says that you can assign values to an indexed arrays like

arr=`ls`


all the best,
v

corey_james
05-06-2008, 11:51 AM
Ok i don't know if this is the wisest decision so i am gonna ask your opinion about which shell do you use for scripting?

/bin/sh - you can guarantee this will exist on all unix ( and unix like ) operating systems. If you don't care about portability ... then it doesn't matter

JMJ_coder
05-06-2008, 01:04 PM
Hello,

Ok i don't know if this is the wisest decision so i am gonna ask your opinion about which shell do you use for scripting?

Korn was an attempt to integrate both the Bourne and C shells (it's debatable as to how successful that attempt has been). But, I find 9 times out of 10, if it works in Bourne, it'll work in Korn. One time, I had to translate a Bourne script to Korn and the only thing I had to change was the line that points to the shell from #! /bin/sh to #! /bin/ksh.

Many do use Korn for scripting now, I think it is number 2 or 3 after Bash - but Bash is still the undisputed king (cf. LinuxQuestions 2007 Member Choice Awards).

Like corey_james said, for ultimate portability, go with Bourne.

But, learning Korn is still a good skill to have, and if you know Korn or Bash, you know about 90% of the other respective language.

mtx
05-06-2008, 01:31 PM
thanks for your opinion JMJ_coder. every new thing doesn't harm to be learned. that was the first thought when i started ksh. anyway i think i'll go with bourne because of the portability. one more question. do you guys recommend some book for bourne?
thank you

all the best,
v

scottro
05-06-2008, 01:47 PM
http://www.amazon.com/Unix-Shell-Programming-Stephen-Kochan/dp/0672324903/ref=pd_bbs_2?ie=UTF8&s=books&qid=1210078008&sr=8-2

Also, the Sam's Teach Yourself Shell Scripting in 24 Hours isn't bad.

JMJ_coder
05-06-2008, 01:58 PM
Hello,

The book I have is Kochan & Wood's Unix Shell Programming (http://www.amazon.com/Unix-Shell-Programming-Stephen-Kochan/dp/0672324903/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1210078452&sr=8-1)

And it covers both Bourne and Korn!

The Unix in a Nutshell (http://www.amazon.com/Unix-Nutshell-Fourth-Arnold-Robbins/dp/0596100299/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1210078649&sr=1-1) also has a good section on each of the Bourne, Korn, and the C shells.

lvlamb
05-06-2008, 02:27 PM
Small detail, people on bsdforums suggesting to use ksh most probably are openbsd persons using the default user's shell ksh which is really the public domain ksh, not ksh93 (c) AT&T.
There are several variants of ksh, most of all compatible with /bin/sh, always with subtle differences.

drl
05-07-2008, 11:00 AM
HI.

The article http://en.wikipedia.org/wiki/Comparison_of_computer_shells has an extensive table for viewing the similarities and differences of the features of many shells (8 *nix shells + cmd.exe) ... cheers, drl