LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2007, 12:41 AM   #1
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
Can "Arrays" be used in shell scripting


Can "Arrays" be used in shell scripting



What I need to do with array is to read "N" numbers from keyboard and when
the position of the number is supplied through keyboard that number should be deleted....For doing this I need to store the numbers in an array


Can anyone tell me about the usage of array or any other method to store N numbers
 
Old 11-30-2007, 12:44 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, here's the Bash arrays guide: http://www.tldp.org/LDP/abs/html/arrays.html
HTH
 
Old 11-30-2007, 12:46 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Have a look here. I suggest you download the entire document.

D'oh, d'oh, d'oh ... too slow

Last edited by syg00; 11-30-2007 at 12:47 AM. Reason: Too late
 
Old 11-30-2007, 12:55 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Bash can have array variables.

Look in the info bash manual for the details.

There are exampes for creating and indexing them. You can determine many elements are in an array variable: ${#name[@]}.

One thing to keep in mind is that you might have a variable where name[0] is defined and name[10] is defined but the others aren't set.

So if you have defined a variable "name":
name=(10 11 12 13); name[100]=2

Then the array will have a size of 4.
So bash array variables are sort of a cross between an array and a hash. The element index has to be a positive integer.

You need to use square brackets around the index, and regular brackets around the variable when referencing them:
echo ${name[0]} ${name[1] ${name[100]}

Other general purpose scripting languages such as perl have arrays and hashes.
 
Old 11-30-2007, 01:14 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Arrays in bash are pretty limited. Some other shells (e.g. zsh) have associative arrays, which bash lacks (what a pity!).

If you are starting to require complex data structures, perhaps shell script isn't the best language for your project. If you're going to do a lot of manipulation of strings in arrays and sets and so on, maybe Perl or Python would be a better choice of language. It depends on what you are trying to do.
 
Old 11-30-2007, 03:12 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I agree totally.
you can use arrays in bash and ksh

I've been shell scripting professionally for about 10 years
but I never use them because, by then you should really start thinking about
using a decent scripting language.
they are a lot more fun to use, as matthew said, perl or python maybe.
 
Old 12-05-2007, 11:43 PM   #7
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you friends.....I will try to learn arrays
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Have to automate the SSH login procedure through shell scripting" sorav Programming 2 11-21-2007 04:53 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
shell scripting: "formatting" issue with text files eur0dad Programming 3 08-17-2006 10:50 AM
shell scripting - using ${variable:=","} Steve Riley Linux - General 7 02-08-2005 05:07 AM
Shell Scripting: Users "**Never logged in**" cparker15 Linux - General 1 10-02-2003 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration