Quick Review: Boxee Box
December 27, 2011 – 12:22 am | 3 Comments

Some of the technical issues with Boxee Box could have been fixed if the dev team was paying more attention to addressing the bugs rather than adding “features” of dubious value. In the final analysis, for the price and ease of use, Boxee Box is the best in its class and price range. You just need to be mindful of its limitations and buy it in hope of future improvements to its usability.

Read the full story »
Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Scripts

Korn Shell Arrays

Submitted by on January 24, 2008 – 11:13 amNo Comment
Korn Shell Arrays

Here are a few useful examples of how to use data arrays within Korn shell. Arrays are a great tool for storing user input and other data for quick, on-the-fly access. If, for example, you script repeatedly reads the same input file, you can speed things up by converting the file into an array (depending, of course, on the size of the file).

Create a simple array

set -A termnames gl35a t2000 s531 vt99 # array elements are separated by blanks, TABs, or NEWLINEs

set -A arrayname $(< filename) # where "filename" is the file that contains array values

typeset -A StateTax
StateTax[New Jersey]=0.06
print ${StateTax[New Jersey]}

Read the array

print ${#termnames[*]} #shows the number of elements in the array; “*” can be replaced by “@”

print ${termnames[*]} #shows all values

print ${termnames[0]} #shows the first value

for i in 0 3 4 #show values 0, 3, and 4
do
print ${termnames[$i]}
done

print ${termnames[3]} is equivalent to print ${termnames[2+1]}

Sample script 1

set -A termnames gl35a t2000 s531 vt99
select term in `print ${termnames[*]}`
do
if [[ -n $term ]]
then
TERM=${termnames[REPLY-1]}
print “TERM is $TERM”
break
fi
done

Read a file into an array, one line at a time

i=0
cat /var/log/messages | while read LINE
do
msgarray[$i]=”${LINE}”
(( i = i + 1 ))
done

Print values from the array

i=0 #array element count begins with “0″
while [ $i -lt ${#termnames[*]} ]
do
print ${termnames[$i]}
(( i = i + 1 ))
done

Put the output of a command into an array

set -A dt `date`

> print ${#dt[*]}
6
> print ${dt[*]}
Wed Jan 30 00:55:04 EST 2008

Sample script 2

#!/bin/ksh
#
# parms2array: store the passed parameters in an array
# source: http://open.itworld.com/5040/nls_unix_korn060810/page_1.html
set -A parms $*
print “You supplied ${#parms[@]} parameters:”
i=0
while [ $i -le ${#parms[@]} ]
do
print ${parms[$i]}
(( i=i+1 ))
done

> ./parms2array red orange yellow green blue indigo violet
You supplied 7 parameters:
red
orange
yellow
green
blue
indigo
violet

Popularity: 4% [?]

Related posts:

  1. Database operations with SQL and Korn shell
  2. Passing shell variables to awk and sed
  3. Generating complex SQL queries with shell scripts
  4. Contacting the sysadmin from command line

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.