Anyway different ways of doing this program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Anyway different ways of doing this program
# 1  
Old 09-03-2013
Anyway different ways of doing this program

Okay so I'm 13 and my dad set me a challenge of writing a program that check to see if a box is online with input parameter when starting the program so i came back with this.The top commented out bit is what he showed me how to do after i show him my code.

Code:
#!/bin/sh
#in=${@}
#tst=`echo ${in}|tr ' ' "Z"`
#if [ "${tst}" = "" ]; then
#  echo "ERROR: Must provide some hosts as input to this script"
#  exit 1
#fi

#for hostName in $in
#do
#  ping -q -c 1 $hostName > /dev/null 2>&1
#  if [ $? -ne 0 ]; then
#    echo $hostName is offline
#  else
#    echo $hostName is online
#  fi

#done

#exit 0

count=0
x=0
while [ $count -ne $# ]
do
  if [ $count -eq 0 ]
  then
    x=$1
  elif [ $count -eq 1 ]
  then
    x=$2
  elif [ $count -eq 2 ]
  then
    x=$3
  elif [ $count -eq 3 ]
  then
    x=$4
  elif [ $count -eq 4 ]
  then
    x=$5
  elif [ $count -eq 5 ]
  then
    x=$6
  elif [ $count -eq 6 ]
  then
    x=$7
  elif [ $count -eq 7 ]
  then
    x=$8
  elif [ $count -eq 8 ]
  then
    x=$9
  fi
  ping -q -c 1 $x > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo "$x is offline"
  else
    echo "$x is online"
  fi
  count=`expr $count + 1`
done

Is there any different ways of doing it??
Smilie
# 2  
Old 09-03-2013
Typically for this short of problem you would use the shift shell internal.

Code:
if [ $# -eq 0 ]
then
    echo "Usage: $0 host [...]">&2
    exit 1
fi

while [ $# -gt 0 ]
do
    if ping -q -c 1 $1 > /dev/null 2>&1
    then
         echo $1 is offline
    else
        echo $1 is online
    fi
    shift
done


Last edited by Chubler_XL; 09-03-2013 at 06:13 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-03-2013
I did not know about the shift command.

I am new to shell scripting and did not know that command i do know thanks Smilie.
# 4  
Old 09-06-2013
Please correct me i think it should be online first then else offline.

Quote:
Originally Posted by Chubler_XL
Typically for this short of problem you would use the shift shell internal.

Code:
if [ $# -eq 0 ]
then
    echo "Usage: $0 host [...]">&2
    exit 1
fi

while [ $# -gt 0 ]
do
    if ping -q -c 1 $1 > /dev/null 2>&1
    then
         echo $1 is online
    else
        echo $1 is offline
    fi
    shift
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Different ways to check a file

to get the checksum of a file on unix systems, you can just use the "cksum" command. i discovered there are some watered down versions of unix systems i have to do some work on. surprisingly, these systems have perl installed on them and awk. so if the cksum command is not available on a... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. UNIX for Dummies Questions & Answers

Different ways to get OS version

I am trying to figure out the OS version of my Linux box. I got three commands: # uname -a Linux test01 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 # cat /proc/version Linux version 2.6.18-238.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704... (4 Replies)
Discussion started by: insvf
4 Replies

3. UNIX for Dummies Questions & Answers

Why use RADIUS for authentication as there are many ways to do it ?

I guess I probably ask a dumb question but why use RADIUS for authentication as there are many ways to do it, as authentication is basically a user/password check? What is the benifit(s) of using it ? Thanks! (3 Replies)
Discussion started by: qiulang
3 Replies

4. UNIX for Dummies Questions & Answers

Different ways in which applications are Launched.

How does X windows system launch a program? Does X give init a command (fork) or does x give BASH a command? Can/does X interact with init directly or does everything go through BASH? :wall: Thanks in advance! (4 Replies)
Discussion started by: theKbStockpiler
4 Replies

5. Shell Programming and Scripting

AWK help ! or others ways to do it!

Hi! I really need your help! I need to operate the columns separate by ',' of a file with this structure a1,a2,a3 b1,b2,b3,b4,b5 c1,c2 d1,d2,d3 e1 .... and I want the result of this subtractions a1-a1,a2-a1,a3-a1 b1-b1,b2-b1,b3-b1,b4-b1,b5-b1 (10 Replies)
Discussion started by: geparada88
10 Replies

6. UNIX for Dummies Questions & Answers

Two questions. First one; What are the ways in which a program starts to run.

This is my first post here so hello everyone! I know that a command of the programs name can start a program and clicking on a icon in GUI can as well as a startup shell script but how do I educate myself of the method that starts an application? Does the GUI run a script? What are the ways/ way... (2 Replies)
Discussion started by: theKbStockpiler
2 Replies

7. What is on Your Mind?

10 e-ffective Ways to Get Backlinks!

Backlinks are super important on the web today! Without them your site most likely will not get found by search engine spiders. If your website has been generating low to No traffic then it is probably due to the lack of backlinks. Below are 10 e-ffective Ways to Get Backlinks! 1. Forum... (0 Replies)
Discussion started by: donaldcarter55
0 Replies

8. Programming

Ways to eliminate Zombies?

what are the precautions to be taken care for avoiding zombie process ? (8 Replies)
Discussion started by: Gopi Krishna P
8 Replies

9. UNIX for Dummies Questions & Answers

Ways to know about a command.......

What are the ways to know about a command? (7 Replies)
Discussion started by: g.ashok
7 Replies

10. Tips and Tutorials

12 Ways to Parse a file

A common thing in shell scripting. I came across this script that will be useful for people learning to write script. #!/usr/bin/ksh # # SCRIPT: 12_ways_to_parse.ksh.ksh # # # REV: 1.2.A # # PURPOSE: This script shows the different ways of reading # a file line by line. Again... (0 Replies)
Discussion started by: google
0 Replies
Login or Register to Ask a Question