$total=500
echo "scale=2; $val1*100/$total" | bc
echo "scale=2; $val2*100*100/$total" | bc
echo "scale=2; $val3*100/$total" | bc
I want to make the above code to be accomplish in a single echo line.
For instance output:
21.3, 44.2, 51.6
How to achieve that, some one please help, i just start learning shell script.
There're many way to achieve that,here is a intuitive way to do that,redirect the output to a pipe,then replace all "newline" with ",",then use another pipeline to remove the restore the last newline.
{
8 echo "scale=2; $val1*100/$total" | bc
9 echo "scale=2; $val2*100*100/$total" | bc
10 echo "scale=2; $val3*100/$total" | bc
11 } | tr "\n" ',' | sed 's/,$/\n/'
May i know why got curly bracket?
the {} defined a namespace,or say code blocks.It groups the codes inside it as a unit.so all output of the code within {} will be redirected to a pipe.
I'm sorry i cant get it, i can't get it. If like the this how to run ? is it a function type?
{
echo "scale=2; $val1*100/$total" | bc
echo "scale=2; $val2*100*100/$total" | bc
echo "scale=2; $val3*100/$total" | bc
} | tr "\n" ',' | sed 's/,$/\n/'
No,it not a function,have you ever learned C?like the while statement.However,the {} is a un-named code blocks here.It just reunion some codes into a space(a local space).One important usage is to redirect it's I/O like here.