openSUSE Forums > Programming/Scripting » Using arrays in bash

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 14-Oct-2009, 11:11
Puzzled Penguin
 
Join Date: Jan 2009
Posts: 4
arani hasn't been rated much yet
Default Using arrays in bash

Hi,
I am trying to implement a simple bubble sort program in bash. However, I seem to be facing some problems while operating on arrays. So far, I have been using the Advanced Bash Shell Guide on tldp.org as my reference, but even the syntax given there does not seem to work. I would be grateful if someone here in this forum could try to look into this matter:


19 if [ ${array["$comp_pos"]} -lt ${array["$j"]} then
20 temp = ${array[${comp_pos}]}
21 {array[${comp_pos}]} = ${array[${j}]}
22 {array[${j}]}=$temp
23 fi


Thanks for going through this post.

I am getting the following error message:

8.sh: line 20: temp: command not found
8.sh: line 21: {array[2]}: command not found
8.sh: line 22: {array[1]}=: command not found
8.sh: line 20: temp: command not found
8.sh: line 21: {array[3]}: command not found
8.sh: line 22: {array[2]}=: command not found
Reply With Quote
  #2 (permalink)  
Old 14-Oct-2009, 12:28
microchip8's Avatar
Shaman Penguin
 
Join Date: Jun 2008
Location: /dev/belgium
Posts: 2,193
microchip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the rough
Default Re: Using arrays in bash

temp="${array[${comp_pos}]}"

There should be no spaces between the variable name and its values, so temp = somevalue is not correct. The above in bold is so change all accordingly (if you have others too)

Also, the if conditional is missing a closing square bracket and semi-colon at end... correct is (or one of the correct ways is): if [ ..... ]; then

Further, an array is called with ${value[pos]} while assigning an array is value[pos]=
__________________
My site: http://microchip.bplaced.net
My repo: http://download.opensuse.org/repositories/home:/microchip8
SUSE Unbound Forum: http://suseunbound.lefora.com


Do coders dream of sheep() ?
Reply With Quote
  #3 (permalink)  
Old 14-Oct-2009, 12:48
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,900
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Using arrays in bash

I am afraid most of the error messages have nothing to do with the usage of arrays. The are just simple errors you made.

Code:
temp = ${array[${comp_pos}]}
This means calling the command temp with two parameter fields:
1. =
2. what ${array[${comp_pos}]} becomes after bash did all substitutions.
What you probably want is:
Code:
temp=${array[${comp_pos}]}
This means: asign what ${array[${comp_pos}]} becomes after substitution to the variable temp.

Code:
{array[${comp_pos}]} = ${array[${j}]}
Again first the shell does some substitutions: it will e.g. substitute ${comp_pos} with 2 and I do not know what it will substitute ${j} with and after that ${array[jresult]}. But the result will be something like:
Code:
{array[2]} = someresult
Maybe by now you will see that you not only made the same mistake as above (spaces where not allowed), but also forgot a $ as the first character.
Orr as ana alternative (i do not know what you want to achieve exactly) you wanted the { } removed.

In short you should better read what you have written, and then ask yourself: how many fases will this go through and what happens in every of those phases. The man pages of bash come to help here. This involves things like: alias substitution, parameter substitution. ..., program exececution.

I will leave the rest to you. You can of course come back with further questions, but I advise you first to write some simple scripts to train yourself and find out about the wonderfull (im)possibilities of bash
__________________
Henk van Velden
Reply With Quote
  #4 (permalink)  
Old 14-Oct-2009, 13:03
microchip8's Avatar
Shaman Penguin
 
Join Date: Jun 2008
Location: /dev/belgium
Posts: 2,193
microchip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the rough
Default Re: Using arrays in bash

In the additional info Henk provided above, it seems you don't really seem to understand one-dimentional arrays in Bash. I'll give a very simple example how to create and call them...

Code:
for i in {1..3}; do
    myvar[i]="value$i"
done

echo "${myvar[*]}"
in the for loop, some values get assigned to the 'myvar' arrays and later on we print them with 'echo ${myvar[*]}' (the * in the array is used to report all array values and one can also use @ instead of * but there are minor differences so you'll have to read up a bit). More examples you can find about arrays - Bash Arrays | Linux Journal
__________________
My site: http://microchip.bplaced.net
My repo: http://download.opensuse.org/repositories/home:/microchip8
SUSE Unbound Forum: http://suseunbound.lefora.com


Do coders dream of sheep() ?
Reply With Quote
  #5 (permalink)  
Old 15-Oct-2009, 06:32
Puzzled Penguin
 
Join Date: Jan 2009
Posts: 4
arani hasn't been rated much yet
Default Re: Using arrays in bash

Yes, i have got it. As I said before, I was trying to perform a bubble sort, and the fragment of the code tries to swap values, in case the condition is satisfied. From what I gathered from your replies, and the links where I was referred, I found that this is what the code should be:

PHP Code:
if [ "${array["$comp_pos"]}" -gt "${array["$j"]}" ] ; then
                        temp
=${array["$comp_pos"]}
                        array[
"${comp_pos}"]=${array["$j"]}
                        array[
"$j"]=$temp
                        
#echo "$temp"
     
fi 
So many thanks, microchip8 and hcw for your kind advice and help. Your help really did the trick for me. Thanks once again!
Reply With Quote
  #6 (permalink)  
Old 15-Oct-2009, 06:42
microchip8's Avatar
Shaman Penguin
 
Join Date: Jun 2008
Location: /dev/belgium
Posts: 2,193
microchip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the rough
Default Re: Using arrays in bash

No problemo, ask anytime.... and what? Henk and me get no reputation points?
__________________
My site: http://microchip.bplaced.net
My repo: http://download.opensuse.org/repositories/home:/microchip8
SUSE Unbound Forum: http://suseunbound.lefora.com


Do coders dream of sheep() ?
Reply With Quote
  #7 (permalink)  
Old 16-Oct-2009, 10:34
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,900
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Using arrays in bash

Was away for one and a half day. Look if your programming style improved immensly (with all the nice colours). But still no reps
__________________
Henk van Velden
Reply With Quote
  #8 (permalink)  
Old 16-Oct-2009, 12:54
microchip8's Avatar
Shaman Penguin
 
Join Date: Jun 2008
Location: /dev/belgium
Posts: 2,193
microchip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the roughmicrochip8 is a reputation jewel in the rough
Default Re: Using arrays in bash

What can I say, Henk? Unthankful cretins... why do we even bother?
__________________
My site: http://microchip.bplaced.net
My repo: http://download.opensuse.org/repositories/home:/microchip8
SUSE Unbound Forum: http://suseunbound.lefora.com


Do coders dream of sheep() ?
Reply With Quote
  #9 (permalink)  
Old 16-Oct-2009, 15:14
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,900
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Using arrays in bash

I agree, but maybe the place where to click for it should be a bit more eye catching. I did not realy understand what it was for until, by incident, I read something about in a post.

In any case, he said: "it works". And he said: "thank you". Much more then many do
__________________
Henk van Velden
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2