Solved

Array in bourne shell?

Posted on 2000-02-24
8
3,322 Views
Last Modified: 2008-02-01
Can we simulate a array in bourne shell?  If yes, how?
0
Comment
Question by:ramalaks
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2556069
In a POSIX-compliant shell, you get name[number] style arrays.  If you're running genuine olde fashioned Bourne shell, however, you can't do that.  You could, in theory, cobble together something that looks like
  get_value name_number
  set_value name_number value
but this would be greatly complicated by the fact that genuine olde fashioned Bourne shell also does not support shell functions.
0
 
LVL 1

Author Comment

by:ramalaks
ID: 2556135
You mean, form the string in such a way that it appears as array element?

Will this work if the array size is not known?  
0
 
LVL 84

Expert Comment

by:ozo
ID: 2556584
What array operations do you want to simulate?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:ramalaks
ID: 2558020
I will have to use array of strings and the size is based on the input argument to the shell script which is not a fixed value.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2558267
You haven't answered the fundamental question posed by my original post.  Are you using genuine olde fashioned Bourne shell, or aren't you?  If you're writing your own script, that sounds like you're under no constraints as far as what scripting language you use, so you can use one that's based on POSIX.2 compliant shell such as Korn Shell (ksh).  Actually, most modern .*x systems have a POSIX compliant /bin/sh as well.

The syntax looks like this
  index=257
  vbl["$index"]=hello
  echo "${vbl[$index]}"

There's no need to pre-declare the size of the array (as there was in early version of Korn Shell).
0
 
LVL 1

Author Comment

by:ramalaks
ID: 2558409
Sorry chris, It is old fashioned bourne shell which does not support arrays.
That is why, I asked if we can simulate by string cat to form variables look  like array. I dont know how to do that.

Yes, Csh, and Ksh support arrays. I can always use ksh. But I was curious to know how to simulate array in bourne becuase, I have done almost 50% of my script in Bourne..at one point I will need an array. Thanks

I still want to know how do we simulate array in bourne shell?  
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 25 total points
ID: 2558502
index=257
value="hello world"
eval "array_$index='$value'"
eval echo '"$'"array_$index"'"'
0
 
LVL 1

Author Comment

by:ramalaks
ID: 2577838
eval "array_$no='$i'"
eval echo "$"array_$no""


Works. Thanks and points to chris.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
nodeip 9 86
phantom space used up on RHEL?  (du shows no space used) 1 81
Linux performance issues with 100% swap usage, etc 15 94
UNiX Script filesystem space usage 19 77
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question