Prevent script from outputitng status code - bash

I wrote some ugly script of mine and besides giving me what I want in the output it also gives me the status code value.
The script output is below. How do I prevent the script from showing that status code in the output??
P.S. I'll put the script below, hope it wouldn't hurt your eyes too much
Status code = 0
Status code = 0
Status code = 0
job_1_1_1
Status code = 0
Status code = 0
Status code = 0
Status code = 0
Status code = 0
Status code = 0
job_1_1_2
Status code = 0
Status code = 0
START_ID=`dsjob -logsum -type STARTED UPSTREAM_MDM_D4 seq_1_1 | nawk 'ORS=(FNR%2)?FS:RS' | grep Starting | tail -1 | awk '{print $1 }'`; FATAL_IDS=`dsjob -logsum -type INFO UPSTREAM_MDM_D4 seq_1_1 | grep INFO | awk '{print $1 }'`; for TEST_ID in ${FATAL_IDS}; do if [[ "${TEST_ID}" -ge "${START_ID}" ]]; then WARN_DTL=`dsjob -logdetail UPSTREAM_MDM_D4 seq_1_1 ${TEST_ID}`; if `echo $WARN_DTL|grep -q 'has finished, status = 3'`; then message=`echo $WARN_DTL| grep -oP '\w+(?= has finished, status = 3)'`; fatal_errors=$fatal_errors$'\n'$message;fi; fi;done; echo $fatal_errors

You should decide what you want exactly achieve, one of method can be for example:
(
your commands
) 2>&1 | grep -v 'Status code = 0'
Beware - the above is not a good practice - only a quick-hack like solution...

Related

If condition with AWK giving incorrect output

Here is a snippet of a code ,I am expecting the result to be printed as Failed but it ends up giving me Ok message instead. I have cross checked and crontab entry does have the string that the code is searching for. NR ==0 should be false as I understand, what is the purpose of $0<1 ?
Crontab entry as seen :
# comment x
00 5 * * * /opt/vol/scripts/volumes.sh -m SOP
Code:
shell: |
crontab -l | grep -v "^#" | grep "/opt/vol/scripts/volumes.sh" | grep "SOP" |
awk 'END { if (NR==0 || $0<1) print "OK - sop job not scheduled"; else print "FAILED - sop job should not be scheduled"; }
Incorrect Output:
OK - sop job not scheduled
Expected Output:
FAILED - sop job should not be scheduled
To simplify:
if crontab -l | grep -Eq "^\s*[^#].*/opt/vol/scripts/volumes\.sh.*SOP"; then
echo "OK - sop job scheduled"
exit 0
else
echo "FAILED - sop job should not be scheduled" >&2
exit 1
fi
If it's not the expected echo's, edit the echo's lines

curl, split string and exit if needed

This is an output when run curl --silent localhost:9091/health
# HELP metrics_qollector_ttt_health_check The status of ttt.
# TYPE metrics_qollector_ttt_health_check gauge
metrics_qollector_ttt_health_check 1.0
# HELP metrics_qollector_database_health_check The status of the database.
# TYPE metrics_qollector_database_health_check gauge
metrics_qollector_database_health_check 1.0
# HELP metrics_qollector_health_checks_latency_seconds Health check latency in seconds.
# TYPE metrics_qollector_health_checks_latency_seconds summary
metrics_qollector_health_checks_latency_seconds_count{method="runtime",} 1.0
metrics_qollector_health_checks_latency_seconds_sum{method="runtime",} 9.51166E-4
I need to boil a bash script that checks if metrics_qollector_ttt_health_check == "1.0" or metrics_qollector_database_health_check == "1.0" then exit 0; else exit 1;
What I have been trying to do is the following
#!/usr/bin/env bash
set -e
HEALTHSTATUS=$(curl --silent localhost:9091/health)
for item in ${HEALTHSTATUS}
if [[ item[0] = "metrics_qollector_ttt_health_check" && item[1] = "1.0" ] || [ item[0] = "metrics_qollector_database_health_check" && item[1] "1.0" ]]; then
exit 0;
else
exit 1;
done
grep -q exits with status 0 if a match was found and exists with status 1 if no match was found. You can replace your whole script with the following one-liner:
curl --silent localhost:9091/health |
grep -Eq '^metrics_qollector_(ttt|database)_health_check 1\.0$'
You have so many ways of doing this, but here is one:
#!/bin/bash
STATUS=$(curl --silent http://localhost:9091/health | egrep "^metrics_qollector_ttt_health_check |^metrics_qollector_database_health_check ")
EXIT_STATUS=$(echo $STATUS | awk '{if(NF == 4) { if(($2 == 0) || ($4 == 0)) {print "0"} else {print "1"} } else {print "2"}}')
exit $EXIT_STATUS
awk should parse your float values (1.0) to numeric values for comparison. It also checks if you have both metrics.
the script exit codes:
0 - At least one of the values is 0.0
1 - None of the values is 0.0
2 - If for some reason you don't have one or both metrics in your
health API

how to use shell script to convert row to column or some nice table

I got the following script from stack overflow :
#!/bin/sh
in_file=temp2.txt # Input file
params=6 # Parameters count
res_file=$(mktemp) # Temporary file
sep=' ' # Separator character
# Print header
cnt=0
for i in $(cat $in_file | head -$((params*2))); do
if [ $((cnt % 2)) -eq 0 ]; then
echo $i
fi
cnt=$((cnt+1))
done | sed ":a;N;\$!ba;s/\n/$sep/g" >>$res_file
# Parse and print values
cnt=0
for i in $(cat $in_file); do
# Print values, skip param names
if [ $((cnt % 2)) -eq 1 ]; then
echo -n $i >>$res_file
fi
if [ $(((cnt+1) % (params*2))) -eq 0 ]; then
# Values line is finished, print newline
echo >>$res_file
elif [ $((cnt % 2)) -eq 1 ]; then
# More values expected to be printed on this line
echo -n "$sep" >>$res_file
fi
cnt=$((cnt+1))
done
# Make nice table format
cat $res_file | column -t
#rm -f $res_file
But then i have about 100 + lines in it and i'm getting a error**"column: line too long"** as below :
****column: line too long**** GigabitEthernet0/0 GigabitEthernet1/0/3 GigabitEthernet1/0/5 GigabitEthernet1/0/10
GigabitEthernet1/0/19 GigabitEthernet1/0/33 GigabitEthernet1/0/2
GigabitEthernet1/0/4 GigabitEthernet1/0/7 GigabitEthernet1/0/18
GigabitEthernet1/0/30 GigabitEthernet1/0/44 GigabitEthernet1/0/46
GigabitEthernet1/1/3 GigabitEthernet2/0/1 GigabitEthernet2/0/5
GigabitEthernet2/0/9 GigabitEthernet2/0/14 GigabitEthernet2/0/18
GigabitEthernet2/0/31 GigabitEthernet2/0/34 GigabitEthernet2/0/36
GigabitEthernet2/0/40 GigabitEthernet2/1/3 GigabitEthernet3/0/12
GigabitEthernet3/0/30 GigabitEthernet3/0/32 GigabitEthernet3/0/34
GigabitEthernet3/0/36 GigabitEthernet3/0/38 GigabitEthernet3/0/40
GigabitEthernet3/0/42 GigabitEthernet3/0/44 GigabitEthernet3/0/46
GigabitEthernet3/0/48 GigabitEthernet3/1/2
Any solutions you can give, i could not find the author of this script again here to ask him on this can be avoided.
Input file will be something like this :
{
GigabitEthernet0/0
GigabitEthernet1/0/2
GigabitEthernet1/0/3
GigabitEthernet1/0/4
GigabitEthernet1/0/5
GigabitEthernet1/0/7
GigabitEthernet1/0/10
GigabitEthernet1/0/18
GigabitEthernet1/0/19
GigabitEthernet1/0/30
GigabitEthernet1/0/33
GigabitEthernet1/0/44
GigabitEthernet1/0/45
GigabitEthernet1/0/46
GigabitEthernet1/1/2
GigabitEthernet1/1/3
GigabitEthernet1/1/4
GigabitEthernet2/0/1
GigabitEthernet2/0/2
GigabitEthernet2/0/5
GigabitEthernet2/0/8
GigabitEthernet2/0/9
GigabitEthernet2/0/12
GigabitEthernet2/0/14
GigabitEthernet2/0/15
GigabitEthernet2/0/18
GigabitEthernet2/0/22
GigabitEthernet2/0/31
GigabitEthernet2/0/33
GigabitEthernet2/0/34
GigabitEthernet2/0/35
GigabitEthernet2/0/36
GigabitEthernet2/0/38
GigabitEthernet2/0/40
GigabitEthernet2/1/2
GigabitEthernet2/1/3
GigabitEthernet2/1/4
GigabitEthernet3/0/12
GigabitEthernet3/0/23
GigabitEthernet3/0/30
GigabitEthernet3/0/31
GigabitEthernet3/0/32
GigabitEthernet3/0/33
GigabitEthernet3/0/34
GigabitEthernet3/0/35
GigabitEthernet3/0/36
GigabitEthernet3/0/37
GigabitEthernet3/0/38
GigabitEthernet3/0/39
GigabitEthernet3/0/40
GigabitEthernet3/0/41
GigabitEthernet3/0/42
GigabitEthernet3/0/43
GigabitEthernet3/0/44
GigabitEthernet3/0/45
GigabitEthernet3/0/46
GigabitEthernet3/0/47
GigabitEthernet3/0/48
GigabitEthernet3/1/1
GigabitEthernet3/1/2
GigabitEthernet3/1/3
GigabitEthernet3/1/4
}
Output i need something like this :
{
GigabitEthernet0/0 | GigabitEthernet1/0/33 |
GigabitEthernet1/0/2 | GigabitEthernet1/0/44 |
GigabitEthernet1/0/3 | GigabitEthernet1/0/43 |
GigabitEthernet1/0/4 | GigabitEthernet1/0/46 |
GigabitEthernet1/0/5 | GigabitEthernet1/1/2 |
GigabitEthernet1/0/7 | GigabitEthernet1/1/3 |
GigabitEthernet1/0/10| GigabitEthernet1/1/4 |
GigabitEthernet1/0/18| GigabitEthernet2/0/1 |
GigabitEthernet1/0/19| GigabitEthernet2/0/2 |
GigabitEthernet1/0/30| GigabitEthernet2/0/5 |
}
I have got this resolved using a column command.
it just posts the output something like this
column vacant_temp4.txt
GigabitEthernet1/0/1 GigabitEthernet1/0/48 GigabitEthernet2/0/34 GigabitEthernet3/0/44 GigabitEthernet4/0/28
GigabitEthernet1/0/2 GigabitEthernet1/1/2 GigabitEthernet2/0/35 GigabitEthernet3/1/1 GigabitEthernet4/0/29
GigabitEthernet1/0/3 GigabitEthernet1/1/3 GigabitEthernet2/0/36 GigabitEthernet3/1/2 GigabitEthernet4/0/30
GigabitEthernet1/0/5 GigabitEthernet1/1/4 GigabitEthernet2/0/38 GigabitEthernet3/1/3 GigabitEthernet4/0/31
GigabitEthernet1/0/7 GigabitEthernet2/0/1 GigabitEthernet2/0/45 GigabitEthernet3/1/4 GigabitEthernet4/0/32
GigabitEthernet1/0/8 GigabitEthernet2/0/5 GigabitEthernet2/1/2 GigabitEthernet4/0/1 GigabitEthernet4/0/33
GigabitEthernet1/0/9 GigabitEthernet2/0/8 GigabitEthernet2/1/3 GigabitEthernet4/0/5 GigabitEthernet4/0/34
GigabitEthernet1/0/14 GigabitEthernet2/0/9 GigabitEthernet2/1/4 GigabitEthernet4/0/6 GigabitEthernet4/0/35
GigabitEthernet1/0/16 GigabitEthernet2/0/10 GigabitEthernet3/0/2 GigabitEthernet4/0/9 GigabitEthernet4/0/36
GigabitEthernet1/0/19 GigabitEthernet2/0/13 GigabitEthernet3/0/5 GigabitEthernet4/0/12 GigabitEthernet4/0/37
GigabitEthernet1/0/20 GigabitEthernet2/0/14 GigabitEthernet3/0/7 GigabitEthernet4/0/13 GigabitEthernet4/0/38
GigabitEthernet1/0/26 GigabitEthernet2/0/16 GigabitEthernet3/0/13 GigabitEthernet4/0/16 GigabitEthernet4/0/39
GigabitEthernet1/0/27 GigabitEthernet2/0/20 GigabitEthernet3/0/16 GigabitEthernet4/0/17 GigabitEthernet4/0/40
GigabitEthernet1/0/28 GigabitEthernet2/0/21 GigabitEthernet3/0/19 GigabitEthernet4/0/18 GigabitEthernet4/0/41
GigabitEthernet1/0/30 GigabitEthernet2/0/25 GigabitEthernet3/0/22 GigabitEthernet4/0/19 GigabitEthernet4/0/42
GigabitEthernet1/0/31 GigabitEthernet2/0/26 GigabitEthernet3/0/25 GigabitEthernet4/0/20 GigabitEthernet4/1/1
GigabitEthernet1/0/35 GigabitEthernet2/0/27 GigabitEthernet3/0/26 GigabitEthernet4/0/21 GigabitEthernet4/1/2
GigabitEthernet1/0/36 GigabitEthernet2/0/28 GigabitEthernet3/0/27 GigabitEthernet4/0/22 GigabitEthernet4/1/3
GigabitEthernet1/0/37 GigabitEthernet2/0/29 GigabitEthernet3/0/37 GigabitEthernet4/0/23 GigabitEthernet4/1/4
GigabitEthernet1/0/40 GigabitEthernet2/0/30 GigabitEthernet3/0/39 GigabitEthernet4/0/24
GigabitEthernet1/0/45 GigabitEthernet2/0/31 GigabitEthernet3/0/41 GigabitEthernet4/0/26
GigabitEthernet1/0/46 GigabitEthernet2/0/32 GigabitEthernet3/0/42 GigabitEthernet4/0/27
It looked better than what you saw above on my putty screen .
Thank you oliv , tripleee , paul & all.

how do I parse output from ntpq?

I am fairly new to bash scripting... I have an issue with a cronjob where I get too many emails when "ntpq: read: Connection refused" error comes up. I want to create a conditional when this error shows up, DO NOT send the email.
However, I can't seem to parse the output from "nptq -nc peers". I did try to redirect the output of the cronjob to a test.txt file and then create another cronjob that parses that file. However, I feel like there is a better solution.
Thanks for your help!
Here is my code for the cronjob
#!/bin/bash
limit=10101010101010101010000 # Set your limit in milliseconds here
offsets=$(/usr/sbin/ntpq -nc peers | /usr/bin/tail -n +3 | awk 'BEGIN { FS = " " } ; { print $9 }' | /usr/bin/tr -d '-')
for offset in ${offsets}; do
if echo $offset $limit | awk '{exit $1>$2?0:1}'
then
echo "NTPD offset $offset > $limit. Please investigate"
exit 1
fi
done

Incrementing a single number in a history line

So I'm having difficulty figuring this out.
What I am trying to do, is display the most recently entered command
Let's use this as an example:
MD5=$(cat $DICT | head -1 | tail -1 | md5sum)
This command has just been executed. It is contained inside of a shell script.
After it is executed, the output is checked in an if..then..else.. statement.
If the condition is met, I want it to run the command above, except I want it incremented by one, every time it is ran.
For instance:
MD5=$(cat $DICT | head -1 | tail -1 | md5sum)
if test ! $MD5=$HASH #$HASH is a user defined MD5Hash, it is checking if $MD5 does NOT equal the user's $HASH
then #one liner to display the history, to display the most recent
"MD5=$(cat $DICT | head -1 | tail -1 | md5sum)" #pipe it to remove the column count, then increment the "head -1" to "head -2"
else echo "The hash is the same."
fi #I also need this if..then..else statement to run, until the "else" condition is met.
Can anyone help, please and thank you. I'm having a brain fart.
I was thinking using sed, or awk to increment. grep to display the most recent of the commands,
So say:
$ history 3
Would output:
1 MD5=$(cat $DICT | head -1 | tail -1 | md5sum)
2 test ! $MD5=$HASH
3 history 3
-
$ history 3 | grep MD5
Would output:
1 MD5=$(cat $DICT | head -1 | tail -1 | md5sum)
Now I want it to remove the 1, and add a 1 to head's value, and rerun that command. And send that command back through the if..then..else test.
UPDATED
If I understood your problem well, this can be a solution:
# Setup test environment
DICT=infile
cat >"$DICT" <<XXX
Kraftwerk
King Crimson
Solaris
After Cyring
XXX
HASH=$(md5sum <<<"After Cyring")
# Process input file and look for match
while read line; do
md5=$(md5sum<<<"$line")
((++count))
[ "$HASH" == "$md5" ] && echo "The hash is the same. ($count)" && break
done <$DICT
Output:
The hash is the same. (4)
I improved the script a little bit. It spares one more clone(2) and pipe(2) call using md5sum<<<word notation instead of echo word|md5sum.
At first it sets up the test env creating infile and a HASH. Then it reads each line of the input file, creates the MD5 checksum and checks if is matches with HASH. If so it writes some message to stdout and breaks the loop.
IMHO the original problem was a little bit over-thought.

Resources