Find the answer to your Linux question:
Results 1 to 2 of 2
Hi i'm trying to make a small script i would like to ping to my subnet from a certain number till another certain number for example Code: #!/bin/bash while [ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    2

    Post Bash case statement help


    Hi i'm trying to make a small script
    i would like to ping to my subnet from a certain number till another certain number for example

    Code:
    #!/bin/bash
    
    while [ $# -lt 9000 ]
    do
            case "$1" in 
            -$2-3) for i in 192.168.1.{$2..$3];do if ping -c1 -w1 &>/dev/null;then echo $i this one is up;fi; done
            shift shift;;
             
            -a) echo "hey just something random
           esac
    done
    and like the output could be like for example
    letstrythisbash.sh 65-74
    192.168.1.65 this one is up
    192.168.1.69 this one is up
    192.168.1.72 this one is up

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    must you use case? you could do what you want w/a loop like this:

    Code:
    #!/bin/bash
    
    # get args from command line
    args=$1
    
    # make sure it is in the form of "N-N"
    echo $args|grep -q ^[0-9][0-9]*-[0-9][0-9]*$
    if [ $? -ne 0 ]; then
      echo "
      Usage: $0 <FIRST_OCTET>-<LAST_OCTET>
       E.g.: $0 5-10
    "
      exit 1
    fi
    
    # split up into first and last number (octets of ip address)
    first=$(echo $1|cut -f1 -d-)
    last=$(echo $1|cut -f2 -d-)
    
    # duh
    if [ $first -gt $last ]; then
      echo first number must be less than second number
      exit 1
    fi
    
    # the loop
    for ((i=$first;i<=$last;i++)); do
      ip=192.168.1.$i
      echo ping $ip
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •