Results 1 to 6 of 6

Thread: array in script

  1. #1

    array in script

    Hello
    Is it possible to use arrays in the shell scripts?

  2. #2
    Quote Originally Posted by vavshek View Post
    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")

  3. #3
    Join Date
    Apr 2006
    Location
    Heesch, Netherlands
    Posts
    118
    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:

    Code:
    array1=(`cat $file`)
    updating values can be done by:

    Code:
    array1[2]=$value
    writing back my array to file:

    Code:
    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)
    Solar inverter monitoring with Asus wl500gx http://solar.reinieren.net (dutch)

  4. #4
    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)

  5. #5
    Join Date
    Apr 2006
    Location
    Heesch, Netherlands
    Posts
    118
    Bash will work for sure.
    Code:
    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:
    Code:
    /bin/sh
    /opt/bin/bash
    Don't forget to start your scripts with:
    Code:
    #! /opt/bin/bash
    If you want to run the script in bash
    Solar inverter monitoring with Asus wl500gx http://solar.reinieren.net (dutch)

  6. #6

    Pointer

    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:
    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} )

Similar Threads

  1. Update NoIp, DynDNS, dyn.ee, ipactive via script
    By rj.2001 in forum WL-500g/WL-500gx Tutorials
    Replies: 9
    Last Post: 04-01-2011, 15:48
  2. Help to make Script to restart dialup connection
    By xipbox in forum WL-500gP Q&A
    Replies: 0
    Last Post: 14-11-2007, 18:35
  3. Script for asus router
    By and_woox in forum WL-500gP Q&A
    Replies: 1
    Last Post: 12-11-2007, 03:53
  4. Call script from post-boot and not returing?
    By scriptman in forum WL-500gP Q&A
    Replies: 2
    Last Post: 04-12-2006, 03:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •