Solved

Array in bourne shell?

Posted on 2000-02-24
8
3,303 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
Comment Utility
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
Comment Utility
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
Comment Utility
What array operations do you want to simulate?
0
 
LVL 1

Author Comment

by:ramalaks
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 14

Expert Comment

by:chris_calabrese
Comment Utility
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
Comment Utility
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
Comment Utility
index=257
value="hello world"
eval "array_$index='$value'"
eval echo '"$'"array_$index"'"'
0
 
LVL 1

Author Comment

by:ramalaks
Comment Utility
eval "array_$no='$i'"
eval echo "$"array_$no""


Works. Thanks and points to chris.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now