substraction of variable having float value is not working [duplicate] - bash

This question already has answers here:
Floating-point arithmetic in UNIX shell script
(4 answers)
Closed 3 years ago.
I am executing below as part of bash script on solaris 10.
MEM_USED_PC=`prstat -Z 1 1 | grep -i sz | awk '{print $5}' | sed 's/%//'`
MEM_TOTAL_PC=100
MEM_FREE_PC=$(($MEM_TOTAL_PC-$MEM_USED_PC))
but echo $MEM_FREE_PC gives below error:
100-6.5: syntax error: invalid arithmetic operator (error token is ".5")
What could be the problem?

You can use the calculator CLI, bc
MEM_FREE_PC=$(echo "$MEM_TOTAL_PC - $MEM_USED_PC" | bc)
echo $MEM_FREE_PC

Since bash doesn't support floating point, you need something like awk to compute the result:
$ MEM_TOTAL_PC=100
$ MEM_USED_PC=99.33
$ MEM_FREE_PC=$(awk -v MEM_TOTAL_PC=$MEM_TOTAL_PC -v MEM_USED_PC=$MEM_USED_PC 'BEGIN {print MEM_TOTAL_PC-MEM_USED_PC}')
$ echo $MEM_FREE_PC
0.67

Related

Bash unable to assign value [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt
wc -l data.txt | awk '{ print $1 }'
yields the result 12, which is desired. However if I put it in the test.sh file, it won't work. I've tried different quotations, but all I've managed to get is the entire line as a string...
Test.sh
#! /bin/bash
# Count lines for data.txt files
data1=wc -l data.txt | awk '{ print $1 }'
echo "Lines in data.txt: $data1"
exit
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $() syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight = assignment like you did...

How can i put command output into a variable [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.
Trying to run the following bash script. The output of the following ling fails:
mem = free | grep Mem | awk '{print $4/$2 * 100}'
With the error:
mem: command not found
Like this:
mem=$(free | grep Mem | awk '{print $4/$2 * 100}')
You can also use backticks:
mem=`free | grep Mem | awk '{print $4/$2 * 100}'`
But parentheses are preferred now. More: http://mywiki.wooledge.org/BashFAQ/082

Bash: How to do decimal number division? [duplicate]

This question already has answers here:
Division in script and floating-point
(6 answers)
Closed 9 years ago.
$num=12.53
How can I divide by 5 and get a decimal result in bash script?
$(($num/5)) doesn't work.
BASH doesn't support decimal point arithmetic. You need to use bc or awk:
num=12.53
bc -l <<< "scale=2; $num/5"
2.50
OR using awk:
awk -v n=$num 'BEGIN {printf "%.2f\n", (n/5)}'
kent$ num=12.53
kent$ echo "scale=2;$num/5"|bc
2.50
kent$ awk -v n="$num" 'BEGIN{printf "%.2f\n", n/5}'
2.51
note the bc's scale and printf's format may give different result.

Matching only the first three numbers of a string in ksh [duplicate]

This question already has answers here:
KSH check if string starts with substring
(4 answers)
Closed 8 years ago.
VARIABLE=`grep PortNumber` testfile.txt | awk -F'"' '{print $2}'`
echo $VARIABLE
33111
I want to do a check to ensure the first 2 numbers of the variable are the digit '3' only.
How can I do this using a standard ksh script?
EDIT:
I think I have it in the following, does this look correct?
echo $VARIABLE | egrep -q '^[3]{2}'
You can do :
echo $VARIABLE | grep -E "^3+{2}"

Error while trying to pass awk result to variable [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 8 years ago.
I'm trying to create a simple bash script to get the HTTP codes from CURL
So here is my code :
#!/bin/bash
AWKRESULT = $(curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}')
echo $AWKRESULT
the result of
curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}'
is 500.
However it's always has this result :
./test.sh[2]: AWKRESULT: not found.
any idea what am I missing?
Remove the spaces around the =:
AWKRESULT=$(...)

Resources