Alternative to bc at comparison - sh

I have the following shell script with a structure that uses bc to calculate the rank depending on what the text variable is equal to.
#!/bin/sh
text="4.0"
if [ -n "${text}" ]; then
if [ "$(echo "${text} == 0.0" | bc)" -eq 1 ]; then
rating="None"
elif [ "$(echo "${text} >= 0.1 && ${text} <= 3.9" | bc)" -eq 1 ]; then
rating="Low"
elif [ "$(echo "${text} >= 4.0 && ${text} <= 6.9" | bc)" -eq 1 ]; then
rating="Medium"
elif [ "$(echo "${text} >= 7.0 && ${text} <= 10.0" | bc)" -eq 1 ]; then
rating="Top"
fi
fi
echo ${rating}
It works fine, but I am not sure that this is an accurate and effective solution.
Is there any other more fast solution? Thx.

Related

How to compare a string value in an IF else condition using shell script

#!/bin/sh
if [ $# -lt 2 ]
then
echo "Usage $0 servername envtype"
exit 1
fi
servername=$1
envtype=$2
srv=echo $servername | tr "a-z" "A-Z"
srvtype=echo $envtype | tr "a-z" "A-Z"
echo $srv
echo $srvtype
if [ "${srv}" <> "SCP" ];
then
if [" ${srv}" <> "RENTD" ];
then
echo "Invalid servername"
exit 1;
fi
fi
I am getting this error when running the shell script
[ 2 -lt 2 ]
servername=scp
envtype=prd
echo scp
tr a-z A-Z
srv=SCP
echo prd
tr a-z A-Z
srvtype=PRD
echo SCP
SCP
echo PRD
PRD
[ SCP ]
[ SCP ]
/tmp/testupper.sh: 19: [ SCP: not found
Found my issue.
[" ${srv}" <> "RENTD" ]
there is a space being prefixed to the srv variable, between the " and the $: " ${srv}"

Why does this POSIX sh function set $? = 1

Very simple script but no idea why it sends an exit code of 1...
docker run --rm -it alpine
/ # apk update && apk add curl
...
...
/ # func() { RESPCODE=$(curl -sS -w "%{http_code}" -o /dev/null https://jsonplaceholder.typicode.com/todos/1); EXITCODE=$?; [ $EXITCODE -ne 0 ] && return 2; }
/ # func
/ # echo $?
1
Why does it not exit with zero code?!
We assume curl suceeds.
EXITCODE=0
From https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html: The exit status of an AND list shall be the exit status of the last command that is executed in the list.
[ $EXITCODE -ne 0 ] exits with 1 exit status
return 2 does not execute
the last command in [ $EXITCODE -ne 0 ] && return 2 is [ $EXITCODE -ne 0 ]
[ $EXITCODE -ne 0 ] exited with 1 exit status
so the exit status of [ $EXITCODE -ne 0 ] && return 2 is 1
the last command in a function exited with 1 exit status.
the function exits with 1 exit status
echo $? outputs 1
Do not use && nor || as a condition. Use if.

Remove old Elasticsearch indexes if ELK is installed in docker container using curl

ELK is installed on docker . Due to old logs and indexes server hard disk capacity gets full resulting crash of ELK container.
Run below shell script on docker shell on which elk is installed
#!/bin/bash
DAYSAGO=date --date="200 days ago" +%Y%m%d
ALLLINES=/usr/bin/curl -s -XGET http://127.0.0.1:9200/_cat/indices?v | egrep logstash
echo
echo "THIS IS WHAT SHOULD BE DELETED FOR ELK:"
echo
echo "$ALLLINES" | while read LINE
do
FORMATEDLINE=echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $2 }' | sed 's/\.//g'
if [ "$FORMATEDLINE" -lt "$DAYSAGO" ]
then
TODELETE=echo $LINE | awk '{ print $3 }'
echo "http://127.0.0.1:9200/$TODELETE"
fi
done
echo
echo -n "if this make sence, Y to continue N to exit [Y/N]:"
read INPUT
if [ "$INPUT" == "Y" ] || [ "$INPUT" == "y" ] || [ "$INPUT" == "yes" ] || [ "$INPUT" == "YES" ]
then
echo "$ALLLINES" | while read LINE
do
FORMATEDLINE=echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $2 }' | sed 's/\.//g'
if [ "$FORMATEDLINE" -lt "$DAYSAGO" ]
then
TODELETE=echo $LINE | awk '{ print $3 }'
/usr/bin/curl -XDELETE http://127.0.0.1:9200/$TODELETE
sleep 1
fi
done
else
echo SCRIPT CLOSED BY USER, BYE ...
echo
exit
fi

diff between small and larg files not running

i am trying to compare beetwin 2 file and get output if they change
i am missing something
#!/bin/ksh
cd /tmp
FilesDiff=`diif -U 0 /tmp/file1 file2 |grep ^# |wc -l`
countnew = `cat /tmp/file1 |wc -l`
countold = `cat /tmp/file2 |wc -l`
if $FilesDiff != 0 and countnew > countold
then
exit 0
else
exit 1
fi
Yes, you have a spelling error, some parentheses missing around comparisions and some spacing errors. Fix them and the script will work:
#!/bin/ksh
cd /tmp
FilesDiff=`diff -U 0 /tmp/file1 file2 |grep ^# |wc -l`
countnew=`cat /tmp/file1 |wc -l`
countold=`cat /tmp/file2 |wc -l`
if (($FilesDiff != 0)) && (($countnew > $countold))
then
exit 0
else
exit 1
fi

How can I compare a number against a range in bash or Perl?

How to script a comparison of a number against a range?
1 is not within 2-5
or
3 is within 2-5
It's even better in Perl6.
Chained comparison operators:
if( 2 <= $x <= 5 ){
}
Smart-match operator:
if( $x ~~ 2..5 ){
}
Junctions:
if( $x ~~ any 2..5 ){
}
Given / When operators:
given( $x ){
when 2..5 {
}
when 6..10 {
}
default{
}
}
In Perl:
if( $x >= lower_limit && $x <= upper_limit ) {
# $x is in the range
}
else {
# $x is not in the range
}
In bash:
$ if [[ 1 -gt 2 && 1 -lt 5 ]]; then echo "true"; fi
$ if [[ 3 -gt 2 && 1 -lt 5 ]]; then echo "true"; fi
true
The smart match operator is available in Perl 5.10, too:
if ( $x ~~ [2..5] ) {
# do something
}
In Bash:
x=9; p="\<$x\>"; if [[ $(echo {10..20}) =~ $p ]]; then echo true; else echo false; fi
Edited to correctly handle conditions as noted in the comment below.
rangecheck () { local p="\<$1\>"; if [[ $(echo {10..20}) =~ $p ]]; then echo true; else echo false; fi; }
for x in {9..21}; do rangecheck "$x"; done
false
true
.
.
.
true
false
In perl
grep {/^$number$/} (1..25);
will give you a true value if the number is in the range and a false value otherwise.
For example:
[dsm#localhost:~]$ perl -le 'print "has `$ARGV[0]`" if grep {/^$ARGV[0]$/} (1..25)' 4
has `4`
[dsm#localhost:~]$ perl -le 'print "has `$ARGV[0]`" if grep {/^$ARGV[0]$/} (1..25)' 456
[dsm#localhost:~]$
The [[ version of test has supported regular expressions since Bash 3.0.
[[ 3 =~ ^[2-5]$ ]]; echo $? # 0
The numeric comparison operators in test sometimes return an error if the input isn't numeric:
[[ 1a -ge 1 ]]; echo $? # value too great for base (error token is "1a")
[[ '$0' -le 24 ]] # syntax error: operand expected (error token is "$o")
You can test if the input is an integer with =~:
x=a23; [[ "$x" =~ ^[0-9]+$ && "$x" -ge 1 && "$x" -le 24 ]]; echo $? # 1
x=-23; [[ "$x" =~ ^-?[0-9]+$ && "$x" -ge -100 && "$x" -le -20 ]]; echo $? # 0