Linux sequence function isn't working properly in a bash script

cloudytechi147

New Member
Joined
Oct 12, 2021
Messages
4
Reaction score
0
Credits
63
I'm working on a Linux course and using the best Linux OS, the instructor uses a ping-sweeping function as an example. It is meant to iterate through potential IPs on a network from 1 through 255 and return the ones that reply. The code is as follows

Code:
#!/bin/bash

for ip in 'seq 1 254'; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

This is within a file called ipsweep.sh, which we then call with

Code:
./ipsweep.sh [first three tuples of IP]

When I run the script, I get the result

Code:
ping: [myIP].seq: Name or service not known

So what I assume is it's not reading seq as a function and simply hitting it and trying to throw it into my script as-is. Obviously, an IP like 192.168.1.seq doesn't exist so we run into this.

I don't fully understand the syntax of the sequence function because I'm new to Linux and scripting in general, but I've tried using

Code:
for ip in (seq 1 254); do

instead but the script won't recognize the parenthesis. Essentially I just need to know how to get the 'seq 1 254' function to work. Any help is greatly appreciated!!
 


Moving this to Command Line, where scripting inquiries are better handled.

Do understand we cannot do your homework for you. Have you asked your teacher or tutor for help?

Good luck.

Chris Turner
wizardfromoz
 
I'm working on a Linux course and using the best Linux OS, the instructor uses a ping-sweeping function as an example. It is meant to iterate through potential IPs on a network from 1 through 255 and return the ones that reply. The code is as follows

Code:
#!/bin/bash

for ip in 'seq 1 254'; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

This is within a file called ipsweep.sh, which we then call with

Code:
./ipsweep.sh [first three tuples of IP]

When I run the script, I get the result

Code:
ping: [myIP].seq: Name or service not known

So what I assume is it's not reading seq as a function and simply hitting it and trying to throw it into my script as-is. Obviously, an IP like 192.168.1.seq doesn't exist so we run into this.

I don't fully understand the syntax of the sequence function because I'm new to Linux and scripting in general, but I've tried using

Code:
for ip in (seq 1 254); do

instead but the script won't recognize the parenthesis. Essentially I just need to know how to get the 'seq 1 254' function to work. Any help is greatly appreciated!!

The problem is you're using single quotes:
Bash:
for ip in 'seq 1 254'; do

Your professor would have been using back-ticks to perform command substitution, which invokes a sub-shell:
Bash:
for ip in `seq 1 254`; do

Change it to back-ticks and it should work.

However, back-ticks are considered deprecated for command substitution. It's really only supported for backwards compatibility with older Bourne shell scripts.

It is better to use the following, more modern syntax instead:
Bash:
for ip in $(seq 1 254); do

The reason for this is because the syntax parsing is more consistent. You can simply wrap virtually any command inside $() and it will behave exactly as it would outside of the sub-shell. Whereas with back-ticks, sometimes things don't work exactly the way you expect!
Also, with $() - you can nest multiple command substitutions inside one another. With back-ticks, you cannot!

There are no real hard and fast rules for when to use back-ticks and when not to. But on the whole the $() syntax is cleaner, it's more expressive (you can do more complicated things with it, thanks to the ability to nest substitutions), it behaves more consistently and should be preferred over back-ticks!

The back-ticks will fix your script, but the $() stuff was just FYI!
 

Members online


Top