Writing a script to check if the output of a command is â3â
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.
#!/bin/bash
Server_Count=""
nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
if [ $? == 3 ]; then
Server_Count="$?"
else
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
shell-script scripting
add a comment |Â
up vote
2
down vote
favorite
I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.
#!/bin/bash
Server_Count=""
nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
if [ $? == 3 ]; then
Server_Count="$?"
else
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
shell-script scripting
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.
#!/bin/bash
Server_Count=""
nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
if [ $? == 3 ]; then
Server_Count="$?"
else
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
shell-script scripting
I'm trying to write a script to run a command and if the output of the command is 3 then script should exit and if it's not 3 then the script should send an email to let me know the count is not 3.
I wrote this code below but for some reason every time I run it, i keep getting email saying the output is not 3 even though it's 3.
#!/bin/bash
Server_Count=""
nslookup servers | grep -i "Address: 10" | wc -l > /dev/null
if [ $? == 3 ]; then
Server_Count="$?"
else
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
shell-script scripting
shell-script scripting
edited Aug 13 at 13:56
Jeff Schaller
32.4k849110
32.4k849110
asked Aug 10 at 21:16
Katkota
384
384
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
$?
will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $?
will be 0.
You want command substitution:
#!/bin/bash
server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
if [[ "$server_count" -ne 3 ]]; then
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
This will set server_count
to the results of wc -l
so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).
Also the -ne
check is being used here which is the correct check for integer comparison.
On a side note you were using the POSIX shell test [ ... ]
with a bash comparison operator ==
. This will still work on many systems but beware when using [ ... ]
you should use =
and when using [[ ... ]]
you can use either =
or ==
.
grep can count:server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And anothergrep
after that:| grep -qxF 3
... and you don't need the variable.
â Kusalananda
Aug 13 at 14:03
add a comment |Â
up vote
0
down vote
As Jesse_b explained, your code does not work since you are looking at the return status of wc -l
rather than the data returned from it.
if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
fi
This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup
, and if that count is not exactly three, it will send an email.
With -c
, grep
will output the number of lines that matches the given pattern. The -q
option to grep
stops grep from generating output. Here, we're only interested in whether grep
matches the 3
or not, and we check this by its return status. With -x
we force a match across a whole line (i.e. we will match only 3
and not 30
). The -F
option makes grep
do a string match instead of a regular expression match.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
$?
will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $?
will be 0.
You want command substitution:
#!/bin/bash
server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
if [[ "$server_count" -ne 3 ]]; then
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
This will set server_count
to the results of wc -l
so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).
Also the -ne
check is being used here which is the correct check for integer comparison.
On a side note you were using the POSIX shell test [ ... ]
with a bash comparison operator ==
. This will still work on many systems but beware when using [ ... ]
you should use =
and when using [[ ... ]]
you can use either =
or ==
.
grep can count:server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And anothergrep
after that:| grep -qxF 3
... and you don't need the variable.
â Kusalananda
Aug 13 at 14:03
add a comment |Â
up vote
4
down vote
accepted
$?
will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $?
will be 0.
You want command substitution:
#!/bin/bash
server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
if [[ "$server_count" -ne 3 ]]; then
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
This will set server_count
to the results of wc -l
so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).
Also the -ne
check is being used here which is the correct check for integer comparison.
On a side note you were using the POSIX shell test [ ... ]
with a bash comparison operator ==
. This will still work on many systems but beware when using [ ... ]
you should use =
and when using [[ ... ]]
you can use either =
or ==
.
grep can count:server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And anothergrep
after that:| grep -qxF 3
... and you don't need the variable.
â Kusalananda
Aug 13 at 14:03
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
$?
will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $?
will be 0.
You want command substitution:
#!/bin/bash
server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
if [[ "$server_count" -ne 3 ]]; then
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
This will set server_count
to the results of wc -l
so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).
Also the -ne
check is being used here which is the correct check for integer comparison.
On a side note you were using the POSIX shell test [ ... ]
with a bash comparison operator ==
. This will still work on many systems but beware when using [ ... ]
you should use =
and when using [[ ... ]]
you can use either =
or ==
.
$?
will be the exit code of the previous command not the result of the previous command. So assuming the command is successful, $?
will be 0.
You want command substitution:
#!/bin/bash
server_count=$(nslookup servers | grep -i "Address: 10" | wc -l)
if [[ "$server_count" -ne 3 ]]; then
echo "Server Count is not 3 .. Please check" | mail -s "Server count issue" Katkota@katkota.com
fi
This will set server_count
to the results of wc -l
so there is no need to set it again in your if statement. Therefore I have negated the if check (if server count does not equal 3 send the email, otherwise do nothing).
Also the -ne
check is being used here which is the correct check for integer comparison.
On a side note you were using the POSIX shell test [ ... ]
with a bash comparison operator ==
. This will still work on many systems but beware when using [ ... ]
you should use =
and when using [[ ... ]]
you can use either =
or ==
.
edited Aug 10 at 21:27
answered Aug 10 at 21:22
Jesse_b
10.5k22659
10.5k22659
grep can count:server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And anothergrep
after that:| grep -qxF 3
... and you don't need the variable.
â Kusalananda
Aug 13 at 14:03
add a comment |Â
grep can count:server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And anothergrep
after that:| grep -qxF 3
... and you don't need the variable.
â Kusalananda
Aug 13 at 14:03
grep can count:
server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
grep can count:
server_count=$( nslookup servers | grep -c "Address: 10" )
â glenn jackman
Aug 13 at 13:49
And another
grep
after that: | grep -qxF 3
... and you don't need the variable.â Kusalananda
Aug 13 at 14:03
And another
grep
after that: | grep -qxF 3
... and you don't need the variable.â Kusalananda
Aug 13 at 14:03
add a comment |Â
up vote
0
down vote
As Jesse_b explained, your code does not work since you are looking at the return status of wc -l
rather than the data returned from it.
if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
fi
This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup
, and if that count is not exactly three, it will send an email.
With -c
, grep
will output the number of lines that matches the given pattern. The -q
option to grep
stops grep from generating output. Here, we're only interested in whether grep
matches the 3
or not, and we check this by its return status. With -x
we force a match across a whole line (i.e. we will match only 3
and not 30
). The -F
option makes grep
do a string match instead of a regular expression match.
add a comment |Â
up vote
0
down vote
As Jesse_b explained, your code does not work since you are looking at the return status of wc -l
rather than the data returned from it.
if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
fi
This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup
, and if that count is not exactly three, it will send an email.
With -c
, grep
will output the number of lines that matches the given pattern. The -q
option to grep
stops grep from generating output. Here, we're only interested in whether grep
matches the 3
or not, and we check this by its return status. With -x
we force a match across a whole line (i.e. we will match only 3
and not 30
). The -F
option makes grep
do a string match instead of a regular expression match.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As Jesse_b explained, your code does not work since you are looking at the return status of wc -l
rather than the data returned from it.
if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
fi
This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup
, and if that count is not exactly three, it will send an email.
With -c
, grep
will output the number of lines that matches the given pattern. The -q
option to grep
stops grep from generating output. Here, we're only interested in whether grep
matches the 3
or not, and we check this by its return status. With -x
we force a match across a whole line (i.e. we will match only 3
and not 30
). The -F
option makes grep
do a string match instead of a regular expression match.
As Jesse_b explained, your code does not work since you are looking at the return status of wc -l
rather than the data returned from it.
if nslookup servers | grep -c -iF "Address: 10" | ! grep -q -xF 3; then
echo 'Server count is not 3... please check' | mail -s 'Server count issue' Katkota@katkota.com
fi
This gets rid of having to save things into variables by counting the number of lines that matches the given string in the output of nslookup
, and if that count is not exactly three, it will send an email.
With -c
, grep
will output the number of lines that matches the given pattern. The -q
option to grep
stops grep from generating output. Here, we're only interested in whether grep
matches the 3
or not, and we check this by its return status. With -x
we force a match across a whole line (i.e. we will match only 3
and not 30
). The -F
option makes grep
do a string match instead of a regular expression match.
answered Aug 13 at 14:34
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461905%2fwriting-a-script-to-check-if-the-output-of-a-command-is-3%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password