LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-11-2006, 08:49 AM   #1
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
Shell Scripting Question: planets[9]=Pluto ??


Hi,

I was revising shell scripting and came across the following script:

Code:
#!/bin/sh
set -a planets mecury venus 
planets[9]=Pluto 
printf ${planets[*]} 
exit 0
I could not understand what the satement planets[9]=Pluto did?
Could anyone please just give me a hint as to how the above script works?

The output of the script is:
Pluto

Thanks for any help.
 
Old 08-11-2006, 10:19 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
In Bash:
# make array named "planets" and add lowercase named members "mercury" and "venus"
# (else use declare -a planets; planets[0]=mercury etc, etc):
planets=( mecury venus )
# the 10th array member has contents "Pluto". Note this doesn't make Pluto a planet. Pluto still is a rock ;-p:
planets[9]=Pluto
# is missing a newline. Prints the contents of the array as string:
printf ${planets[*]}
# always exit a script in OK state:
exit 0
 
Old 08-11-2006, 02:53 PM   #3
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Thanks a lot for the reply. It kinda feels special when the moderator replies

I tried the following script (Sorry, should've tried this before starting this thread) :
Code:
#!/bin/sh
planets[9]=Pluto 
printf ${planets[*]} 
exit 0
and the output was same: Pluto
So, this is what I understand:
The line planets[9]=Pluto declares/defines an array that can store 9 members and it's ninth member is Pluto. Then the statement printf ${planets[*]} Prints the contents of the array as a string. Also since the other members of the array planets are not defined the shell substitues "" whenever they are requested.
Is my interpretation correct?

I also want to ask that what does the following statement do:
Code:
set -a planets mecury venus
I looked up the man page of the set command, but could not find any refernece to the -a flag. What I know about the set command is that a simple set command like:
Code:
set planets mecury venus
would set $1 to planets, $2 to mercury, $3 to venus etc. There were also other uses of set like storing a random number in a variable, but I could not find what the -a flag did.

Thanks for all the help.
 
Old 08-12-2006, 06:02 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
The line planets[9]=Pluto declares/defines an array that can store 9 members and it's ninth member is Pluto.
The line planets[9]=Pluto declares/defines (or adds to) an array named "planets" with one element at position nine whose content is the string "Pluto". The name of the element is planets[9] and it does not define the amount of elements an array can contain.


Then the statement printf ${planets[*]} Prints the contents of the array as a string. Also since the other members of the array planets are not defined the shell substitues "" whenever they are requested.
Since no other elements are defned the shell doesn't print a thing. Use "set -x" to see when running.


set -a planets mecury venus
I explained that to make an array named "planets" you can simply use "planets=( mecury venus )" or else use declare -a planets; planets[0]=mercury etc, etc. set doesn't work for this.


I looked up the man page of the set command
It's a Bash shell builtin: run "help set". BTW, Bash arrays start count at element number zero.
 
Old 08-14-2006, 09:28 AM   #5
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Thanks a lot for the reply. That answer clarified a lot of things
 
  


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
shell scripting question JSLayton Linux - General 4 06-01-2006 06:55 AM
a shell-scripting question: mrchaos Slackware 3 09-22-2005 11:00 AM
Shell scripting question. dragin33 Linux - General 2 08-11-2004 05:17 PM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 03:36 PM

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

All times are GMT -5. The time now is 07:51 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