DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 6th May 2008
mtx's Avatar
mtx mtx is offline
Real Name: Valentin Bud
Fdisk Soldier
 
Join Date: May 2008
Location: RO/TM
Posts: 79
Thanked 5 Times in 3 Posts
Default ksh arrays

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 part.
so i have tried to make the following simple script.
Code:
# cat array.sh
#!/usr/local/bin/ksh93

K=~/bin

cd $K
KA=$(ls $PRE.*)
print ${#KA[*]}
running the script yields the following output
Code:
# ./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:
Code:
# 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\nssh.connect.sh\nvpn.sh'
+ print 1
Trying to print an element from the array let's say the first
Code:
...
print ${KA[1]}
doesn't work either. The out put is blank.
Code:
# 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\nssh.connect.sh\nvpn.sh'
+ print ''
what am i missing here? thanks

all the best,
v
__________________
Stop! think! ... the problem is somewhere between the monitor and chair...
"First they ignore you, then they laugh at you, then they fight you, then you win." Gandhi
links: spreadbsd syk
Reply With Quote
  #2   (View Single Post)  
Old 6th May 2008
corey_james corey_james is offline
Uber Geek
 
Join Date: Apr 2008
Location: Brisbane, Australia
Posts: 238
Thanked 4 Times in 10 Posts
Default

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:

Code:
$ 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 ` `
Reply With Quote
  #3   (View Single Post)  
Old 6th May 2008
mtx's Avatar
mtx mtx is offline
Real Name: Valentin Bud
Fdisk Soldier
 
Join Date: May 2008
Location: RO/TM
Posts: 79
Thanked 5 Times in 3 Posts
Default

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 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
__________________
Stop! think! ... the problem is somewhere between the monitor and chair...
"First they ignore you, then they laugh at you, then they fight you, then you win." Gandhi
links: spreadbsd syk
Reply With Quote
  #4   (View Single Post)  
Old 6th May 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,873
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by mtx View Post
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/...xcu/chap2.html
Reply With Quote
  #5   (View Single Post)  
Old 6th May 2008
mtx's Avatar
mtx mtx is offline
Real Name: Valentin Bud
Fdisk Soldier
 
Join Date: May 2008
Location: RO/TM
Posts: 79
Thanked 5 Times in 3 Posts
Default

thanks ocicat for clarification. i got it working. i had to use
Code:
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
Code:
arr=`ls`
all the best,
v
__________________
Stop! think! ... the problem is somewhere between the monitor and chair...
"First they ignore you, then they laugh at you, then they fight you, then you win." Gandhi
links: spreadbsd syk
Reply With Quote
  #6   (View Single Post)  
Old 6th May 2008
corey_james corey_james is offline
Uber Geek
 
Join Date: Apr 2008
Location: Brisbane, Australia
Posts: 238
Thanked 4 Times in 10 Posts
Default

Quote:
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
Reply With Quote
  #7   (View Single Post)  
Old 6th May 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Hello,

Quote:
Originally Posted by mtx View Post
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.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #8   (View Single Post)  
Old 6th May 2008
mtx's Avatar
mtx mtx is offline
Real Name: Valentin Bud
Fdisk Soldier
 
Join Date: May 2008
Location: RO/TM
Posts: 79
Thanked 5 Times in 3 Posts
Default

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
__________________
Stop! think! ... the problem is somewhere between the monitor and chair...
"First they ignore you, then they laugh at you, then they fight you, then you win." Gandhi
links: spreadbsd syk
Reply With Quote
  #9   (View Single Post)  
Old 6th May 2008
scottro's Avatar
scottro scottro is offline
Real Name: Scott Robbins
Spam Deminer
 
Join Date: Apr 2008
Location: NYC
Posts: 298
Thanked 31 Times in 25 Posts
Default

http://www.amazon.com/Unix-Shell-Pro...0078008&sr=8-2

Also, the Sam's Teach Yourself Shell Scripting in 24 Hours isn't bad.
Reply With Quote
Old 6th May 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Hello,

The book I have is Kochan & Wood's Unix Shell Programming

And it covers both Bourne and Korn!

The Unix in a Nutshell also has a good section on each of the Bourne, Korn, and the C shells.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
Old 6th May 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

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.
__________________
da more I know I know I know nuttin'
Reply With Quote
Old 7th May 2008
drl's Avatar
drl drl is offline
Port Guard
 
Join Date: May 2008
Posts: 18
Thanked 3 Times in 3 Posts
Default

HI.

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

Tags
ksh

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Perl arrays question stukov Programming 12 18th November 2008 08:44 PM
How to vectorize a function for numpy arrays in Python kasse Programming 0 26th August 2008 12:12 PM
C 2D arrays jgroch Programming 16 2nd August 2008 01:54 AM


All times are GMT. The time now is 01:00 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick