Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:52:28+00:00 2026-06-14T08:52:28+00:00

I’m trying to create a simple Bash script to check if the website is

  • 0

I’m trying to create a simple Bash script to check if the website is down and for some reason the "and" operator doesn’t work:

#!/usr/bin/env bash

WEBSITE=domain.example
SUBJECT="$WEBSITE DOWN!"
EMAILID="an@email.example"
STATUS=$(curl -sI $WEBSITE | awk '/HTTP\/1.1/ { print $2 }')
STRING=$(curl -s $WEBSITE | grep -o "string_to_search")
VALUE="string_to_search"

if [ $STATUS -ne 200 ] && [[ "$STRING" != "$VALUE" ]]; then
    echo "Website: $WEBSITE is down, status code: '$STATUS' - $(date)" | mail -s "$SUBJECT" $EMAILID
fi

The "-a" operator also doesn’t work:

if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]

Could you also please advise when to use:

  • single and double square brackets
  • parenthesis

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:52:30+00:00Added an answer on June 14, 2026 at 8:52 am

    If $STATUS expands to the empty string, then
    if [ $STATUS -ne 200 ] is invalid because it is equivalent to
    if [ -ne 200 ], which should generate an error similar to "-ne: unary operator expected".

    The command

    if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]];
    

    is invalid regardless of the expansion of $STATUS because it is passing [[ and ]] as arguments to the [ command, but [ requires that its last argument be the literal string ]. It would probably be better to do:

    if ! [ "${STATUS}" -eq 200 ] 2> /dev/null && [ "${STRING}" != "${VALUE}" ]; then ...
    

    or

    if [ "${STATUS}" != 200 ] && [ "${STRING}" != "${VALUE}" ]; then ...
    

    In each of these, ] is the final argument to the invocation of [, as required. The shell parses && and ; as tokens which terminate the [ command, but strings like -a, [[, and ]] are just strings that the shell passes to the command. (In the first case, the shell treats 2> /dev/null as a redirection operator, so those strings are not passed to [ as arguments). Note that using -a as an argument to [ as a boolean AND is considered bad practice, and ought to be avoided.

    [ and [[ are very different, but the only difference between [ and test is that [ requires that its final argument be ] and test does not. (Some will argue that another difference is that [ is a builtin but test is not, and although there may have been a time in the distant past when some shells had [ as a builtin while test was always an invocation of /bin/test, that probably has not been the case in any common shell for decades). Another reasonable solution is:

    if test "${STATUS}" != 200 && test "${STRING}" != "${VALUE}"; then ...
    

    Personal opinion: never use [[. It suppresses important error messages and is not portable to different shells. Also, as seen by the existence of this question, it is confusing. One may argue that the confusion here is caused by the shell’s grammar and obscure behavior of [, and I would agree. [ is also confusing. You can prevent a lot of confusion if you avoid both [[ and [, and always use test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.