1

I am trying to write a bash ping sweeper and I get two error messages.

The first is:

./pingsweep.sh: line 2: [192.168.199.182: command not found

The second is

ping: 192.168.199.182.seq: Name or service not known.

I cant spot the error from the initial code:

#!/bin/bash
if ["$1" == ""]
then
    echo "Usages:./pingsweep.sh [network]"
    echo "Example: ./pingsweep.sh 192.168.199"
else
    for ip in 'seq 1 254'; do
        ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | sed 's/.$//'&
    done
fi
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Pierce2r
  • 15
  • 1
  • 4
  • 1
    You need a space after `[` and before `]` in your `if` statement. Otherwise, `bash` sees `["$1"` as a single string/command. It will also see `""]` as a single string. – lurker May 25 '19 at 01:08
  • 2
    pasting your code to https://shellckeck.net will help you find many syntax errors like that. Good luck. – shellter May 25 '19 at 01:10
  • 1
    You also need to use back ticks (\`) instead of single quotes in your `seq` command. – Philip Wrage May 25 '19 at 01:10

1 Answers1

2

Your segment ["$1" constructs a single word starting with [ and ending with whatever you passed as the first argument. Because that's not one of the bash builtins (like [), it tries to run it as a command and fails:

./pingsweep.sh: line 2: [192.168.199.182: command not found
                        ^^^^^^^^^^^^^^^^
                        |
                        +--> see *especially* this first character.

You need a space between [ and the variable, and before the closing ] as well. Even if this weren't necessary for syntax, it would be required for my OCD sensibilities :-)

if [ "$1" == "" ]

Additionally, the command for ip in 'seq 1 254' (note the single quotes) will iterate over the one string seq 1 254, not run the command and iterate over its output. For that, you need to use backticks (`) rather than quotes, or my preference since they're easier to nest:

for ip in $(seq 1 254)

But you should keep in mind that bash actually has decent loops without having to resort to seq:

for (( ip=1; ip<10; ip++ )); do echo $ip; done
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • thanks i now get this bash commands with your help and the for (( ip=1; ip<10; ip++ )); do echo $ip; done really solved it.Thanks ones again – Pierce2r May 25 '19 at 04:13