0
#!/bin/bash
COUNT=100
SIZE=100
INTERVAL=0.2
for myHost in `cat /files0/home/singhtr/input`
do
    echo $myHost

    ping -i $INTERVAL -q -n -s  $SIZE -c $COUNT $myHost|grep -i packet

done

The input file:

# cat input
4.2.2.2 abc ATT
8.8.8.8 def BCC

How can I print multiple values from input file like this

Expected Output:

IP of the device: 4.2.2.2
Device Name: abc
customer name:ATT
2 packets transmitted, 2 received, 0% packet loss, time 1001ms

and so on as the loop goes.

I want to achieve this through bash/shell script.

1

2 Answers 2

0
#!/bin/bash
while read myHost
do
    COUNT=100
    SIZE=100
    INTERVAL=0.2
    IP=$(echo $myHost | cut -d " " -f1)
    NAME=$(echo $myHost | cut -d " " -f2)
    CUSTOMER=$(echo $myHost | cut -d " " -f3)
    PING=$(ping -i $INTERVAL -q -n -s  $SIZE -c $COUNT $IP | grep -i packet)
    echo "IP of the device: $IP"
    echo "Device name: $NAME"
    echo "Customer name: $CUSTOMER"
    echo "$PING"
    echo ""
done < /files0/home/singhtr/input

This is one possible solution to your Problem. You can just use cut to select the field you want to output. And then output it with a simple echo command. To read in a file a single line at a time you can use

while read LINE
do
 ...
done < FILE

EDIT

If you want to run a script on a regular basis you should consider creating a cronjob. You can type crontab -l to see the cronjobs of the current logged-in user. If you want to schedule a cronjob you can do as follows:

$ crontab -e

edit the crontab by inserting for example:

*/15 * * * * /path/to/script >> /path/to/log_\`date +%s\` 2>&1

This will create a cronjob that runs the script at the location /path/to/script every 15 minutes and write it output + errors that might occur to file located at /path/to/log_ with a suffix of a unix time stamp (for example 1549883612).

1
  • can you please share the sample code to run this script in let's say every 15 minutes and save the output to a file in same directory with name segregated by time value or any number.
    – Tribs
    Commented Feb 11, 2019 at 10:46
0

Er... you need to get a grip on some details of Bash. :-) Right now you're off by a few hundred yards.

This should help you get going:

#!/bin/bash

echo ----- reading one field at a time
for myhost in `cat ips`
do
        echo $myhost
done

echo
echo ----- reading and printing one line at a time

while read line
do
        echo $line
done < ips

echo ----- separating fields
while read line
do
        ip=$( cut -d' ' -f1 <<< "$line" )
        dev=$( cut -d' ' -f2 <<< "$line" )
        cust=$( cut -d' ' -f3 <<< "$line" )

        echo IP of device: $ip
        echo device name: $dev
        echo customer name: $cust

        #do your ping here
        echo
done < ips

relevant details can be found in:

$ man cut

$ man bash -> section "SHELL BUILTIN COMMANDS", subsection about the command read

and about the "<<<":

$ man bash -> section "Here Strings"

or use echo $line ... instead of the here string (<<<)

echo $line | cut -d' ' -f1 etc

Not the answer you're looking for? Browse other questions tagged .