Bash error with subtraction - bash

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

Related

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$" || :)

Storing a line in a variable

Hi I have the following batch script where I submitted each file to a separate processing as follows:
for file in ../Positive/*.txt_rn; do
bsub <<EOF
#BSUB -L /bin/bash
#BSUB -W 150:00
#BSUB -M 10000
#BSUB -n 3
#BSUB -e /somefolder/errors/%J.err
#BSUB -o /somefolder/errors/%J.out
while read line; do
name=`cat \$line | awk '{print $1":"$2"-"$3}'`
four=`cat \$line | awk '{print $4}' | cut -d\: -f4`
fasta=\$name".fa"
op=\$name".rs"
echo \$name | xargs samtools faidx /somefolder/rn4/Rattus_norvegicus/UCSC/rn4/Sequence/WholeGenomeFasta/genome.fa > \$fasta
Process -F \$fasta -M "list_"\$four".txt" -p 0.003 | awk '(\$5 >= 0.67)' > \$op
if [ -s "\$op" ]
then
cat "\$line" >> ../Positive_Strand/$file".cons"
fi
rm \$lne
rm \$op
rm \$fasta
done < $file
EOF
done
I am am somehow unable to store the values of the column from the line (which is in $line variable into the $name and $four variable and hence unable to carry on further processes. Also any suggestions to edit the code for a better version of it would be welcome.
If you change EOF to 'EOF' then you will more properly disable shell interpretation. Your problem is that your back-ticks (`) are not escaped.
I've fixed your indentation and cleaned up some of your code. Note that the syntax highlighting here doesn't understand cat <<'EOF'. If you paste that into vim with highlighting enabled, you'll see that block is all the same color since it's just a string.
bsub_helper() {
cat <<'EOF'
#BSUB -L /bin/bash
#BSUB -W 150:00
#BSUB -M 10000
#BSUB -n 3
#BSUB -e /somefolder/errors/%J.err
#BSUB -o /somefolder/errors/%J.out
while read line; do
name=`cat $line | awk '{print $1":"$2"-"$3}'`
four=`cat $line | awk '{print $4}' | cut -d: -f4`
fasta="$name.fa"
op="$name.rs"
genome="/somefolder/rn4/Rattus_norvegicus/UCSC/rn4/Sequence/WholeGenomeFasta/genome.fa"
echo $name | xargs samtools faidx "$genome" > "$fasta"
Process -F "$fasta" -M "list_$four.txt" -p 0.003 | awk '($5 >= 0.67)' > "$op"
if [ -s "$op" ]
then
cat "$line" >> "../Positive_Strand/$file.cons"
fi
rm "$lne" "$op" "$fasta"
EOF
echo " done < \"$1\""
}
for file in ../Positive/*.txt_rn; do
bsub_helper "$file" |bsub
done
I created a helper function because I needed to get the input in two commands. I am assuming that $file is the only variable in that block that you want interpreted. I also surrounded that variable (among others) with quotes so that the code can support file names with spaces in them. The final line of the helper has nested double quotes for this reason.
I left your echo $name | xargs … line alone because it's so odd. Without quotes around $name, xargs will take each whitespace-separated entry as its own file. With quotes, xargs will only supply one (likely invalid) file name to samtools.
If $name is a single file, try:
samtools faidx "$genome" "$name" > "$fasta"
If $name is multiple files and none of them have spaces, try:
samtools faidx "$genome" $name > "$fasta"
The only reason to use xargs here would be if you have too much content for one command line, but if you're running echo $name | xargs then you'll run into the same problem.

Add floating numbers in 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)

Grep multiple occurrences given two strings and two integers

im looking for a bash script to count the occurences of a word in a given directory and it's subdirectory's files with this pattern:
^str1{n}str2{m}$
for example:
str1= yo
str2= uf
n= 3
m= 4
the match would be "yoyoyoufufufuf"
but i'm having trouble with grep
that's what i have tried
for file in $(find $dir)
do
if [ -f $file ]; then
echo "<$file>:<`grep '\<\$str1\{$n\}\$str2\{$m\}\>'' $file | wc -l >" >> a.txt
fi
done
should i use find?
#Barmar's comment is useful.
If I understand your question, I think this single grep command should do what you're looking for:
grep -r -c "^\($str1\)\{$n\}\($str2\)\{$m\}$" "$dir"
Note the combination of -r and -c causes grep to output zero-counts for non-matching files. You can pipe to grep -v ":0$" to suppress this output if you require:
$ dir=.
$ str1=yo
$ str2=uf
$ n=3
$ m=4
$ cat youf
yoyoyoufufufuf
$ grep -r -c "^\($str1\)\{$n\}\($str2\)\{$m\}$" "$dir"
./noyouf:0
./youf:1
./dir/youf:1
$ grep -r -c "^\($str1\)\{$n\}\($str2\)\{$m\}$" "$dir" | grep -v ":0$"
./youf:1
./dir/youf:1
$
Note also $str1 and $str2 need to be put in parentheses so that the {m} and {n} apply to everything within the parentheses and not just the last character.
Note the escaping of the () and {} as we require double-quotes ", so that the variables are expanded into the grep regular expression.

Getting an error 'integer expression expected', while comparing two integers

# Program which notifies the User through 'notify-send' when device temperature exceeds the threshold.
#!/bin/bash
Temp=adb shell cat /sys/devices/platform/omap/omap_temp_sensor.0/temperature
if [ $Temp -gt 42000 ]
then
notify-send Temperature "$Temp " -i /usr/share/pixmaps/idle.xpm
cvlc /home/Xme/Desktop/Beep-263732.mp3
else
echo "Exit"
fi
Getting error as
: integer expression expected
I am not getting the data type of $Temp which is reading the data by Device, and how can i compare the integers, i tried if [ [$Temp > 42000] ] did not work.
As we said in the comments, this solved the issue:
Temp=$(adb shell cat /sys/devices/platform/omap/omap_temp_sensor.0/temperature) | grep -o "[0-9]*")
First of all, you were not fetching the number properly. Note that you need to use
Temp=$(command)
While you were using
Temp=command
Then we saw that your input was not integer. I guess there must be some trailing characters. To delete them, I suggest to use grep -o "[0-9]*", which just matches the numbers in the string given. EXamples:
$ echo "23 " | grep -o "[0-9]*"
23
$ echo "as23.22" | grep -o "[0-9]*"
23
22
$ echo "23" | grep -o "[0-9]*"
23

Resources