How to divide 2 integer numbers and get the output stored in float variable using ksh shell scripting? [duplicate] - shell

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 3 years ago.
I have below values stored in the respective variables:
s=$(nice grep -i "sf WOW" *`date --date='1 hour ago' +%y%m%d%H`* | grep -i "TRAF:5" | wc -l)
f=$(nice grep -i "sf WOW" *`date --date='1 hour ago' +%y%m%d%H`* | grep -i "TRAF:7" | wc -l)
a=`expr $s + $f`
I need to store the division output in a float variable with an accuracy of 5 digits.

echo "scale=5; 355/113" | bc
3.14159

Related

How do I add a decimal value to a value in my bash script? [duplicate]

This question already has answers here:
How to add an integer number and a float number in a bash shell script
(4 answers)
Closed 1 year ago.
I'm grabbing the tail of my output file and then want to add 0.000001 to that value.
So far I've tried it this way:
offset=0.000001
time=$(($(`tail -1 speed.txt1`) + $offset))
Bash itself does not have any floating point ability. You need to use another tool to get a float result in the shell.
Given:
cat file
1
2
3
You can use bc for floating point in the shell:
offset='0.000001'
printf "%s+%s\n" $(tail -1 file) "$offset" | bc
3.000001
Or awk:
awk -v offset="$offset" 'END{printf "%.7f\n", $1+offset}' file
3.000001
Since bc does not understand scientific notation (ie, 1.3E6) and you can still normalize that with printf in the shell:
x=1.33E6
printf "%f\n" "$x"
1330000.000000
And then pass to bc:
offset="1e-6"
x=1.33E6
printf "%f+%f\n" "$x" "$offset"
1330000.000000+0.000001
printf "%f+%f\n" "$x" "$offset" | bc
1330000.000001

is this command valid for deleting evicted pods from last month? [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Difference whilst using ";" or "|" symbols in command lines [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Can you please let me know if this command is valid ?
kubectl get pods -A | awk {'printf "%s %s %s %s\n", $1,$2,$4,$6'} | grep -E "Evicted" |
while read line; do ns=$(echo $line | cut -d , -f 1); pod=$(echo $line | cut -d , -f 2); age=$(echo $line | cut -d, -f 6) ;
final_age=$(awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< "$age");
if [[ $final_age -gt 30 ]]; then kubectl delete pod --dry-run='server' -n "$ns" "$pod"; fi done
The intention is to fetch the old evicted pods which their age is greater than 30 days and delete them .. can you please check as it returns null on my machine..
expected output: the names of pods which will be deleted
resulted output: null

working with "unclear" declared variables [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I am trying to save the specific output from a piped command to a variable.
value= ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"
This saves the average ping to the variable "value".
However when I try to further process the variable e.g. in an echo line like:
echo "The Average ping is: $variable"
The output is
The Average ping is: $variable
Even when i try to pass the value to another Variable like:
value2= $value
the result is the same.
I read that variables in bash need to be declared in a certain way, may this be the problem in this specific case?
sh or bash:
value="`ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"`"
bash:
value="$(ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r")"

shell script(calculate the division of two variables taking from a file) [duplicate]

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 6 years ago.
I would like to divide two numbers which were extracted from a file using the below commands
temp1= grep PERM_ALLOCATED_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
temp2= grep PERM_IN_USE_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
and I'm able to print this
251658240
16239740
temp1 and temp2 respectively but I could not able to perform division for the above..
Sample output:
temp=temp2/temp1(0.064)
A possible solution is using echo and bc -l:
echo "251658240/16239740" | bc -l
Output:
15.49644514013155383029
Using your example, you can do that:
temp=`echo $temp2/$temp1*0.64 | bc -l`

BASH script looking for some guidance [duplicate]

This question already has answers here:
Bash variable scope
(7 answers)
Closed 9 years ago.
well im in this for hours and I can't understand why I cant save the values to the variable:
let inicio=${tlinhas[0]}/2+1
tail -n +$inicio $1 | head -n $tlinhas | grep $2 | while read linha
do
let palavras=$palavras+$(echo $linha | wc -w)
echo $palavras
done
printf "%d" $palavras
the problem is that every time I print the variable palavras its always zero but if I print it inside the while it has the value 14
[leganuno#LegaNuno-PC FichasIndividuais]$ ./exercicio1.sh f1 Licenciatura
7
7
0
Try
palavras = echo $($palavras + $(echo $linha | wc -w) | bc)

Resources