Trying to get this bash script to work:


for result in $(cat ./fn7);
for dip1 in $(cat ./fn4);
for dip2 in $(cat ./fn5);
for dip3 in $(cat ./fn6);
for ip in $(seq 0 255);
ifconfig > ./fn1;
cat ./fn1 |grep "broadcast" > ./fn2;
cat ./fn2 |cut -d " " -f10 > ./fn3;
cat ./fn3 |cut -d "." -f1 > ./fn4;
cat ./fn3 |cut -d "." -f2 > ./fn5;
cat ./fn3 |cut -d "." -f3 > ./fn6;
echo $dip1.$dip2.$dip3. > ./fn7;
ping -c 1 $result.$ip > ./results

It fails with a error of:

./IP-Sweeper.sh: line 4: syntax error near unexpected token `for'
./IP-Sweeper.sh: line 4: `for dip1 in $(cat ./fn4);'
  • Being POSIX compliant and using #!/bin/sh has its downsides. Primarily not being able to take advantage of Bash's additional features. If you need those features or don't need to be portable it doesn't matter as long as your remember that sh is portable. – torchhound Apr 29 '16 at 22:01
  • 1
    The above script isn't using any bashisms, and it is better for beginners to understand writing proper scripts from the beginning. – forquare Apr 29 '16 at 22:04
  • That is your opinion and I am supplying my own competing opinion. I would point out that most guides are specifically Bash guides and while understanding the difference is important; most people will learn Bash before they know that sh exists. – torchhound Apr 29 '16 at 22:09
  • 1
    It isn't possible to correct this script without knowing what it should do as it has a lot of syntax and logic errors that are misleading. – Julie Pelletier Apr 29 '16 at 22:21
  • its designed to pull my IP address and then chop off the end octet. Then run a sequence on the 4th octet with the ping command sending a single packet to all computers in my network segment and let me know which are alive targets. This was a script I am trying to get to work for my OSCP course to make things easier and to prove to myself I understand scripting. – Jason Avery Apr 30 '16 at 13:37

The syntax for a for loop is like this:

for x in {list}

There is neither do nor done on any of your loops.

Bash Guide for Beginners and Advanced Bash Scripting Guide are good references on syntax and best scripting practices.

| improve this answer | |
  • Tried it still doesn't work for what ever reason it doesn't populate the varibles with the supplied information in the files. – Jason Avery Apr 30 '16 at 13:38
  • @Jason please add this new script to your question. Ideally cut it down to the smallest example illustrating what doesn't work. Remember to explain what input you give this example, what it gives you back and what you expected. – roaima Apr 30 '16 at 16:24

I think your error is in not having a do done block after your for statement.

| improve this answer | |

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.