0

With the below code, I keeping getting: line 9: [: 8.8.8.8: integer expression expected Unsure Why?

#!/bin/bash
sourceFile="file.log"
targetFile="2file.log"
ping="8.8.8.8"
while IFS='' read -r line || [[ -n "$line" ]]; do
 echo "$line" >> "$targetFile"
 sudo service networking restart
 ping -q -c 5 "$ping"
 if [ "$ping" -ne 0 ]; then
    sed -n -e 8p "$2file.log"
 fi
done < "$sourceFile"
Cœur
  • 37,241
  • 25
  • 195
  • 267
spbr
  • 23
  • 6
  • 1
    Check your test against 0 (zero) – grail Feb 22 '17 at 15:52
  • 1
    You appear to want to check the *exit status* of the previous command against 0, not the value of `$ping`: `if [ $? -ne 0 ]; then` or more directly `if ! ping -q -c 5 "$ping"; then`. – chepner Feb 22 '17 at 16:30

2 Answers2

0

Because you are trying to compare "8.8.8.8" (string) to 0 (integer)

ping="8.8.8.8"
if [ "$ping" -ne 0 ]; then

ping variable is string.

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
0

You are comparing a string value ("8.8.8.8") to an integer value (0)

You can retrieve the exit code of your ping command with $?

ping="8.8.8.8"

ping -q -c 5 "$ping"

ping=$?

if [ $ping -ne 0 ]; then
    echo "NOTOK"
else
    echo "OK"
fi

Check this post

Community
  • 1
  • 1
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159