I assigned output of piping into a variable, but when I try to use the variable to do math, it won't allow me:
%%bash
cd /data/ref/
grep -v ">" EN | wc -c > ref
cat ref
cd /example/
grep -v ">" SR | wc -l > sample
cat sample
echo $((x= cat sample, y= cat ref, u=x/y, z=u*100))
I get this error:
41858
38986
bash: line 7: x= cat sample, y= cat ref, u=x/y, z=u*100: syntax error in expression (error token is "sample, y= cat ref, u=x/y, z=u*100"
You received that error because you passed an invalid arithmetic expression into a bash arithetic expansion. Only an arithmetic expression is allowed for this place. What you try to do seems like this:
so hopefully you get an output like this:
Notes:
There is no need to
cd
before executing agrep
, it accepts the full path with the target filename as an argument. So without changing directory, you cangrep
various locations.In order to save the output of your command (which is only a number) you don't need to save it in a file and
cat
the file. Just use the syntaxvar=$( )
andvar
will be assigned the output of this command substitution.Have in mind that
/
will result to 0 for the division38986/41858
because it's the integer division. If you want to do math calculations with decimals, you can see this post for how to do them usingbc
.To print anything, use the shell builtin
printf
. Here the last two numbers are formatted with 2 decimal points.