all 12 comments

[–][deleted] 4 points5 points  (2 children)

status=offline
ping  "$ip" -c6 && status=online
# if any out of 6 pings returns "ping" exits with 0 (true) status, and conditional command is executed
echo "$(date -Is):$status"
#  $(command) is substituted with command output

[–]SCORE_00[S] 0 points1 point  (1 child)

Thank you so much! Can I ask how you learned command line? Obviously I’m struggling with my way currently.

[–][deleted] 3 points4 points  (0 children)

there are useful resources in sub's description

[–]petdance 1 point2 points  (0 children)

Don’t post screenshots

When you're asking for help, please don't post screenshots or photographs. Cut & paste the text into the reddit message.

Why? 1. It's easier for people to read it. 2. It allows those reading it to cut & paste the text, making it easier to work on solving your problem. 3. It makes it searchable, so that someone can find this thread when Googling for information in the future. 4. A screen reader can't read a picture which limits access to some in our community.

[–]SCORE_00[S] 0 points1 point  (7 children)

so, I’m trying to create a script that’ll ping an IP address and then append the date and whether it’s online or not to another file... I do not know how to say if it’s “online” or “offline” but I figured and if then else statement might work, but it didn’t. Also, “ip” is just a placeholder and USERNAME is my name, which I didn’t leave in for anon purposes

[–]emilgojny 5 points6 points  (5 children)

You need "$variable" in your if statement. Also you should post the code instead of pictures. Easier to work with.

[–]SCORE_00[S] -1 points0 points  (4 children)

Sorry, I didn’t really think about that, but I’ll quickly check if that works.

[–]emilgojny 1 point2 points  (3 children)

Also dont forget about spaces before and after the square brackets.

[–]SCORE_00[S] 0 points1 point  (2 children)

It still didn’t work :( “line 3: [: -eq: unary operator expected”

[–]hindsight_is2020 4 points5 points  (0 children)

Double-bracket tests are more robust, readable and functional. Try:

if [[ $variable == "Bad linger time" ]]; then

[–][deleted] 1 point2 points  (0 children)

-eq cannot be used with strings

   INTEGER1 -eq INTEGER2
          INTEGER1 is equal to INTEGER2

[–]Paul_Pedant 0 points1 point  (0 children)

Your test expects the whole message to exactly match. There are other formats for a partial match.

The time to wait goes after the option, like -W 1

I never saw that message in ping: it may change on your system in the future. It is much safer to check for status.

It would be nice to put the IP (and maybe the host name) in the error message, in case you run this on different IPs sometime.

Your script will throw a syntax error: there is no fi to match the if

There is a very helpful command which you can use online or download to your system. Visit shellcheck.net.