0

I found a bash script to validate ip addresses here: http://www.linuxjournal.com/content/validating-ip-address-bash-script

When i try to run it I get the error :

 test.sh: line 22: syntax error: unexpected "(" (expecting "fi")

Line 22 looks like this:

  ip=($ip)

Question

  1. Is this line supposed to be creating a list or array?
  2. I've tried two different shells, and neither work. tried:

    #!/bin/bash

and also

 #!/bin/sh

Any suggestions would be appreciated.

  • 2
    Kindly paste the script here, so that the Question is not reliant on an external URL to be available. – Jeff Schaller Sep 13 '17 at 18:37
  • What you say is line 22 is not actually line 22. – jesse_b Sep 13 '17 at 18:41
  • 1
    Also the way that script is written, it has to be named "valid_ip" in order to be run directly. Otherwise you are supposed to source it and just use the valid_ip function with an argument like valid_ip 10.10.10.10 – jesse_b Sep 13 '17 at 18:49
  • Either way there is no point for it to be in that script. I recommend either removing the function and adding it to your bashrc or removing all the useless parts of the script (everything other than the function). – jesse_b Sep 13 '17 at 18:59
  • 1
    "Is this line supposed to be creating a list or array?" Since bash only has arrays, not lists, the question doesn't really make sense. – Barmar Sep 13 '17 at 20:18
3

That:

test.sh: line 22: syntax error: unexpected "(" (expecting "fi")

is not an error message that bash reports. It is one that the Almquist shell and its derivatives like dash or busybox sh report though:

$ grep -z '(expecting' /bin/dash
%s unexpected (expecting %s)

ash-based shells don't support arrays, that script is meant for bash (3.2 or above) as the #!/bin/bash she-bang indicates, though it looks more like a mix between bash, zsh and ksh93 syntax if it's meant to check for quad-decimal IPv4 addresses where the parts are limited to 3 decimal digits (in bash, it would give some errors about 008.008.008.008 for instance).

With standard POSIX sh syntax which both dash and bash recognise, you could do instead:

valid_ip() (
  IFS=.
  set -o noglob
  set -- $1''
  [ "$#" -eq 4 ] || exit
  for i do
    case $i in
      ("" | *[!0-9]*) exit 1;;
    esac
    [ "$i" -le 255 ] || exit
  done
)
| improve this answer | |
1

This works:

#!/bin/bash

ip=127.0.0.1

ip=($ip)

printf 'The first IP number is "%s"\n' "${ip[0]}"

If you're running this under bash, then you have something else wrong in your script relating to an if statement. If you're running this under /bin/sh, then it should fail as sh doesn't know about arrays.

I'd suggest you run your script through ShellCheck at https://www.shellcheck.net/

| improve this answer | |
1

1) Is this line supposed to be creating a list or array?

It's creating an array. First it sets the IFS to . and then it says ip=($ip). So if your ip=10.11.12.13 your array would be ip=([0]=10 [1]=11 [2]=12 [3]=13) Then it goes on to verify that each octet is less than 256.

2) What shell should this use

This script is definitely made for bash and therefore should be ran in bash.

3) What is my issue

The issue is that you have modified the script in some way that has caused a syntax error. Please post the full script exactly as you are running it and we can try to figure it out. Additionally as I said in my comment, you must name this script valid_ip.sh. Failure to do such shouldn't cause your error but it will also make the script not work.

| improve this answer | |
  • I didn't change the script. i did a copy paste. but if you still want me to paste it all here i will. – dot Sep 13 '17 at 19:30
  • @glennjackman the second part of the script checks if the basename is valid_ip. If not it doesn't do anything. @dot if you didn't change anything how did you determine line 22? Because line 22 in that script is stat=$? not ip=($ip) but my guess is that the fi just below that somehow got removed. – jesse_b Sep 13 '17 at 19:36
  • Also @glennjackman the function does modify the IFS but then changes it back although I guess that could be an issue if some operation where happening in the background at that exact moment? – jesse_b Sep 13 '17 at 19:57
  • @Jesse_b, quite right, my bad. – glenn jackman Sep 13 '17 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.