Quick
Quick

Reputation: 353

Array of IP addresses in sh script

I am having some trouble with a simple shell script on my ubuntu box. Here is the first part of the script:

#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
servers=(
199.315.117.225 
46.31.151.106 
46.31.154.82  
)

I run the script like this:

sudo sh script.sh

And this is the output:

script.sh: 6: script.sh: Syntax error: "(" unexpected

As far as I can tell, that is a valid array, so I don't understand why that paren is unexpected. I am a newbie so any help is much appreciated.

Upvotes: 0

Views: 7278

Answers (4)

kali doss
kali doss

Reputation: 89

In shell script array should be mentioned by

bash:

declare -a varname
varname=( 192.168.1.1 192.168.1.2 192.168.1.2 )

and you can get all ip by

echo "${varname[@]}"

or

echo "${varname[*]}"

Upvotes: 1

user3367314
user3367314

Reputation: 17

you can try the below code,

#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
ARRAY="199.315.117.225 46.31.151.106 46.31.154.82"

for i in $ARRAY
do
  echo $i
done

Upvotes: -1

sdomalap
sdomalap

Reputation: 21

/bin/bash is superset of /bin/sh... This array syntax works in bash.

https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh

http://www.linuxjournal.com/content/bash-arrays

Upvotes: 1

ruakh
ruakh

Reputation: 183301

The problem is that you're running your script using a shell (namely Dash) that doesn't support a feature your script is using (namely Bash-style arrays).

The easiest fix is to change this:

#!/bin/sh

to this:

#!/bin/bash

so that your script is run using Bash instead of Dash.

Upvotes: 2

Related Questions