Add floating numbers in bash - bash

I'm trying something very easy but all code that I'm trying doesnt work.
I need add two float numbers in bash. I'm doing this:
result1=`$CURL -o /dev/null -s -w %{time_total} $url1`
result2=`$CURL -o /dev/null -s -w %{time_total} $url2`
result3=`$CURL -o /dev/null -s -w %{time_total} $url3`
total= `expr $result2 + $result3`
echo $total | $GAWK -F: '{ print "connection_1.value " $1 }'
but in the prompt I'm getting this output:
./http_response_2: line 12: 0,018+0,255: command not found
connection_1.value
I'm trying too do this:
result1=`$CURL -o /dev/null -s -w %{time_total} $url1`
result2=`$CURL -o /dev/null -s -w %{time_total} $url2`
result3=`$CURL -o /dev/null -s -w %{time_total} $url3`
total= `$result2 + $result3 | bc`
getting the same result.
Thanks in advance!

There are 3 issues:
There should be no space between total= & `
echo missing before $result2 + $result3
There is comma in your input, instead of decimal point.
Fixing all these issues:
total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l)
If you are concerned about the leading 0 before decimal point, try:
total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l | xargs printf "%g")

Instead of replacing commas with dots, don't produce commas in the first place.
They emerge from localization, so use LC_ALL=C as prefix, like:
LC_ALL=C curl -o /dev/null -s -w %{time_total} www.google.com
and abandon the outdated backticks, use $(...) instead:
result1=$(LC_ALL=C $CURL -o /dev/null -s -w %{time_total} $url1)

Related

Is it possible to put value from linux pipe into curl querystring param?

I have a numeric output from command, let's say:
sh -c 'exit 1' ; echo $?
or
bc <<< "1 + 1"
And I need to send it in GET request via curl, like http://example.com/?value=1
I've tried this:
sh -c 'exit 1' ; echo $? | curl -G -d #- http://example.com/
but it just got param with a name 1 and empty value.
I know I can do something like:
result=`sh -c 'exit 129' ; echo $?` | curl -G -d value=${result} http://example.com
but I'd like to keep the first part of command unchanged and modify only part after pipe. Is it possible?
One possible solution I found:
sh -c 'exit 129'; echo $? | xargs -I '{}' curl -G "http://example.com?value={}";

grep -c kills script when no match using set -e

Basic example:
#!/bin/bash
set -e
set -x
NUM_LINES=$(printf "Hello\nHi" | grep -c "How$")
echo "Number of lines: ${NUM_LINES}" # never prints 0
Output:
++ grep -c 'How$'
++ printf 'Hello\nHi'
+ NUM_LINES=0
If there are matches, it prints the correct number of lines. Also grep "How$" | wc -l works instead of using grep -c "How$".
You can suppress grep's exit code by running : when it "fails". : always succeeds.
NUM_LINES=$(printf "Hello\nHi" | grep -c "How$" || :)

Bash: execute command repeatedly and write output tab seperated to file

I want my file to Log like that:
Time_Namelookup: 0,1 0,2 0,12 0,45 ...
Time_Connect: 0,34 0,23 0,23 0,11 ...
Time_Starttransfer: 0,9 0,23 0,12 ...
I want that Values are added to their specific line every n seconds.
I got a code like this:
while [ "true" ]
do
echo "Time_Namelookup:" >> $file
curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/
echo "Time_connect" >> $file
curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/
echo "Time_Starttransfer:" >> $file
curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/
sleep 5
done
But I get something like
Time_Namelookup: 0,1
Time_Connect: 0,34
Time_Starttransfer: 0,9
Time_Namelookup: 0,2
Time_Connect:0,23 0,23
Time_Starttransfer: 0,23
Time_Namelookup: 0,45
Time_Connect: 0,11
Time_Starttransfer: 0,12
Can you help me ?
You could put this inside your loop
if [ ! -f $file ]; then
echo "Time_Namelookup:" > $file
echo "Time_Connect:" >> $file
echo "Time_Starttransfer:" >> $file
fi
name_lookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i -e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
-e "s/\(Time_Connect:.*\)/\1 $connect/" \
-e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
$file
you can try this;
file=yourFile
echo "Time_Namelookup :" >> $file
echo "Time_Connect :" >> $file
echo "Time_Starttransfer:" >> $file
while [ "true" ]
do
time_namelookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
time_connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
time_starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i "/Time_Namelookup :/s/$/\t$time_namelookup/" $file
sed -i "/Time_Connect :/s/$/\t$time_connect/" $file
sed -i "/Time_Starttransfer:/s/$/\t$time_starttransfer/" $file
sleep 5
done
sed -i : to be edited in-place.
The following part is to find "Time_Namelookup :" in the file.
/Time_Namelookup :/
/s/$/\t$time_namelookup/"
s :substitute command
/../../ :delimiter
$ :end of line character.
\t$time_namelookup: to append tab and the value.

wget bash function without messy output

I am learning to customize wget in a bash function and having trouble. I would like to display Downloading (file):% instead of the messy output of wget. The function below seems close I am having trouble calling it for my specific needs.
For example, my standard wget is:
cd 'C:\Users\cmccabe\Desktop\wget'
wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
and that downloads the .csv as a .txt in the directory specified with all the messy wget output.
This function seems like it will do more-or-less what I need, but I can not seem to get it to function correctly using my data. Below is what I have tried. Thank you :).
#!/bin/bash
download() {
local url=$1 wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
local destin=$2 'C:\Users\cmccabe\Desktop\wget'
echo -n " "
if [ "$destin" ]; then
wget --progress=dot "$url" -O "$destin" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
else
wget --progress=dot "$url" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
fi
echo -ne "\b\b\b\b"
echo " DONE"
}
EDITED CODE
#!/bin/bash
download () {
url=http://xxx.xx.xxx.xxx/data/getCSV.csv
destin='C:\Users\cmccabe\Desktop\wget'
echo -n " "
if [ "$destin" ]; then
wget -O getCSV.txt --progress=dot "$url" -O "$destin" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
else
wget -O getCSV.txt --progress=dot $url 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
fi
echo -ne "\b\b\b\b"
echo " DONE"
menu
}
menu() {
while true
do
printf "\n Welcome to NGS menu (v1), please make a selection from the MENU \n
==================================\n\n
\t 1 Patient QC\n
==================================\n\n"
printf "\t Your choice: "; read menu_choice
case "$menu_choice" in
1) patient ;;
*) printf "\n Invalid choice."; sleep 2 ;;
esac
done
}

Bash error with subtraction

I've got some problem with substraction and I don't know why :(
it's my code:
#!/bin/bash
w3m http://www.weather.com/weather/hourbyhour/graph/PLXX0027 > plik.txt
image= grep -o 'http.*' plik.txt
t= cat plik.txt |sed '105q;d' | grep -o '[0-9][0-9]'
a=32
temp=$((t-a))
echo $temp
I've received sth like:
name#name ~/Desktop $ sh p.sh
http://s.imwx.com/v.20120328.084252//img/wxicon/70/14.png
25
-32
but i wan to receive substraction of 25-32... (of course 25 depends of value in webpage) but why it don't want substract it?
Try defining properly all variables, with $() surrounding them.
#!/bin/bash
w3m http://www.weather.com/weather/hourbyhour/graph/PLXX0027 > plik.txt
image=$(grep -o 'http.*' plik.txt)
t=$(cat plik.txt |sed '105q;d' | grep -o '[0-9][0-9]')
a=32
temp=$((t-a))
echo $temp

Resources