Array of IP addresses in sh script - arrays

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.

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.

/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

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[*]}"

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

Related

Create a bat file for git-bash script

Hi all. I gave a git-bash installed and want some automatisation. I've got a .bat file, which I want to run as
some.bat param | iconv -cp1251 > l.log | tail -f l.log
And I want to run it not in WinCMD but in git-bash shell - tell me plz how to do it?
Git bash on windows uses BASH. You can use bash to create a quick script that can take in parameters and echo them so that you can pipe them to your next utility.
It could look something like this
File: some.sh
#!/bin/bash
#Lots of fun bash scripting here...
echo $1
# $1 here is the first parameter sent to this script.
# $2 is the second... etc. $0 is the script name
then by setting some.sh as executable
$ chmod +x some.sh
You'll then be able to execute it in the git-bash shell
./some.sh param | cat ... etc | etc...
You can read about bash programming
I'd reccomend looking at some bash scripting tutorials such as this one

translation from bash to ash shell: how to handle arrays defined by input?

I try to transfer the excellent example docker-haproxy from centos to alpine.
A shell script is used to process a list of values given as parameters to the script into an array, then write these values plus their index to some file.
The following construction works in bash:
ServerArray=${SERVERS:=$1}
...
for i in ${ServerArray[#]}
do
echo " " server SERVER_$COUNT $i >> /haproxy/haproxy.cfg
let "COUNT += 1"
done
but not in ash (or sh):
syntax error: bad substitution
The error refers to line
for i in ${ServerArray[#]}
What is the correct syntax here? I guess the line
ServerArray=${SERVERS:=$1}
does not define an array as intended, but googling for long did not help me.
bash to sh (ash) spoofing says
sh apparently has no arrays.
If so, how to solve the problem then?
I guess I can do with this construction:
#!/bin/sh
# test.sh
while [ $# -gt 0 ]
do
echo $1
shift
done
delivers
/ # ./test 172.17.0.2:3306 172.17.0.3:3306
172.17.0.2:3306
172.17.0.3:3306
which is what I need to proceed

How to use arrays in Unix sh shell?

I want use an array in my shell script test.sh as follows:
#!/bin/bash
index=100345
NAME[0]="Zara"
NAME[$index]="Qadir"
echo "First Index:" ${NAME[0]}
echo "Second Index:" ${NAME[$index]}
but when execute it by sh test.sh:
NAME[0]=Zara: not found
NAME[100345]=Qadir: not found
test.sh: ${NAME[...}: Bad substitution
How to solve it?
Don't do this:
sh test.sh
That runs the script under whatever is the default shell.
Instead, do this:
bash test.sh
This assures that it is bash which runs the script.
Example
Using your test.sh script, this generates errors:
$ sh test.sh
test.sh: 3: test.sh: NAME[0]=Zara: not found
test.sh: 4: test.sh: NAME[100345]=Qadir: not found
test.sh: 5: test.sh: Bad substitution
This does not:
$ bash test.sh
First Index: Zara
Second Index: Qadir
On my system, sh is a link to dash which is a fast POSIX shell but it does not support arrays. To get fancy features like arrays, one must use a fancy shell like bash.
The array is not supported by the simple terminal. Use the command bash your_Script_Name.sh. EX: I have a script file array.sh in which I am using the array. In this case, i will run this script as:
bash array.sh

Shell Script: correct way to declare an empty array

I'm trying to declare an empty array in Shell Script but I'm experiencing an error.
#!/bin/bash
list=$#
newlist=()
for l in $list; do
newlist+=($l)
done
echo "new"
echo $newlist
When I execute it, I get test.sh: 5: test.sh: Syntax error: "(" unexpected
Run it with bash:
bash test.sh
And seeing the error, it seems you're actually running it with dash:
> dash test.sh
test.sh: 5: test.sh: Syntax error: "(" unexpected
Only this time you probably used the link to it (/bin/sh -> /bin/dash).
I find following syntax more readable.
declare -a <name of array>
For more details see Bash Guide for Beginners: 10.2. Array variables.
In BASH 4+ you can use the following for declaring an empty Array:
declare -a ARRAY_NAME=()
You can then append new items NEW_ITEM1 & NEW_ITEM2 by:
ARRAY_NAME+=(NEW_ITEM1)
ARRAY_NAME+=(NEW_ITEM2)
Please note that parentheses () is required while adding the new items. This is required so that new items are appended as an Array element. If you did miss the (), NEW_ITEM2 will become a String append to first Array Element ARRAY_NAME[0].
Above example will result into:
echo ${ARRAY_NAME[#]}
NEW_ITEM1 NEW_ITEM2
echo ${ARRAY_NAME[0]}
NEW_ITEM1
echo ${ARRAY_NAME[1]}
NEW_ITEM2
Next, if you performed (note the missing parenthesis):
ARRAY_NAME+=NEW_ITEM3
This will result into:
echo ${ARRAY_NAME[#]}
NEW_ITEM1NEW_ITEM3 NEW_ITEM2
echo ${ARRAY_NAME[0]}
NEW_ITEM1NEW_ITEM3
echo ${ARRAY_NAME[1]}
NEW_ITEM2
Thanks to #LenW for correcting me on append operation.
Try this to see if you are oriented to dash or bash
ls -al /bin/sh
If it says /bin/sh -> /bin/dash, then type this:
sudo rm /bin/sh
sudo ln -s /bin/bash /bin/sh
Then type again:
ls -al /bin/sh*
then must says something like this:
/bin/sh -> /bin/bash
It means that now sh is properly oriented to Bash and your arrays will work.
DOMAINS=(1); if [[ ${DOMAINS-} ]]; then # true
unset DOMAINS; if [[ ${DOMAINS-} ]]; then # false
If the array is empty just do this:
NEWLIST=
You can check it with:
if [ $NEWLIST ] ; then
# do something
fi
a non empty array declaration looks like this:
NEWLIST=('1' '2' '3')
To fill an array during process:
ARRAY=("$(find . -name '*.mp3')")
Hope this helps

Possible to use $1 within Bash array?

The script I'm writing will require me to pass some command line parameters. I would like to use these parameters within an array, but I'm not sure how.
A very basic example of this would be (script run as ./script.sh array1):
#!/bin/bash
array1=( a b c d )
echo ${#$1[#]}
The output should be 4, but I receive the following error:
line 5: ${#$1[#]}: bad substitution.
I don't have to use arrays, but would like to.
Thanks for any ideas
you need to get bash to substitute the value of $1 before evaluating the line, try this...
eval echo \${#$1[#]}
eval echo '${#'$1'[#]};'

Resources