PDA

Bekijk de volledige versie : array in script



vavshek
28-11-2007, 10:36
Hello
Is it possible to use arrays in the shell scripts?

ABATAPA
28-11-2007, 14:10
Hello
Is it possible to use arrays in the shell scripts?

Bash can this:

# set | grep VER
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i586-alt-linux-gnu")

mistraller
29-11-2007, 22:39
Yes, it's easy, I use some array's in SolGet, my solar monitoring script.

For instance, i have a file where some values are saved. I get them into an array with cat:


array1=(`cat $file`)

updating values can be done by:


array1[2]=$value

writing back my array to file:


echo ${array1[0]} ${array1[1]} ${array1[2]} ${array1[3]} ${array1[4]} > $file

If you're interested, my full script is available at http://solget.sourceforge.net

(I installed bash on my asus)

vavshek
02-12-2007, 21:12
Thanks for replies. So I have to instal bash on my Asus? (in sh scripts I got such error ./test: ./test: 5: Syntax error: "(" unexpected)

mistraller
04-12-2007, 23:41
Bash will work for sure.

ipkg install bash will do the trick.
If you change your passwd file, you can set bash as default shell.
Keep in mind that bash will be installed into opt, which will be an external drive most of the times. So if that drive is not available, and you have only one account with bash as default shell, you can run into a lock out situation.
I took the risk, I run bash as default shell. (with a good backup scenario :))

You also have to create a file called /etc/shells which contains all possible shells, to let ssh accept the shells.
My file contains:

/bin/sh
/opt/bin/bash
Don't forget to start your scripts with:

#! /opt/bin/bash
If you want to run the script in bash

expert_vision
13-03-2011, 12:35
I hope no one gets upset that I revive a 3 years old thread, but I found this useful.

I installed bash and managed to use arrays, but I need something like a pointer for this code:

for i in `seq 2 25`; do
var=`expr $i - 2`
h[$var]=`awk '/D*/{print(<pointer>)}' "$data"`
echo ${h[var]}
done

Basically I need to cycle trough tokens from 2 to 25 of a line in a file ($data is path to a file).
<pointer> has to be something like value of the variable with the name equal to the value of variable "i" (ie ${$i} )