echo multiple variable with calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo multiple variable with calculation
# 1  
Old 03-11-2011
echo multiple variable with calculation

Code:
$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:
Code:
21.3, 44.2, 51.6

How to achieve that, some one please help, i just start learning shell script.
# 2  
Old 03-11-2011
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.
Code:
{
  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/'

# 3  
Old 03-11-2011
Quote:
Originally Posted by homeboy
Code:
{
  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?
# 4  
Old 03-11-2011
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.
# 5  
Old 03-11-2011
Quote:
Originally Posted by homeboy
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?
Code:
{
     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/'

# 6  
Old 03-11-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Column Calculation

Hallo Team, I need you you help. I need to sum up all the columns in a .csv file. Lets call the file file1.csv and it looks like below: $ cat file1.csv... (2 Replies)
Discussion started by: kekanap
2 Replies

2. Shell Programming and Scripting

Arithmetic calculation in variable

Hi, I am trying to do an addition to a value stored in a variable... var1=`grep -n "match" sample.txt|cut -d : -f1` var2=`echo $var1|cut -d " " -f1` (Here i want add +1 to the output value) (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

3. Shell Programming and Scripting

echo the NAME of the variable

#!/bin/bash varA="AAA1" varB="BBB2" varC="CCC3" for proccx in $varA $varB $varC do echo "the current name is ????? , the value is $proccx" echo " " done #end of script I want the output to look something like this: the current name is varA, the value is AAA1 the current name is... (5 Replies)
Discussion started by: ajp7701
5 Replies

4. UNIX for Dummies Questions & Answers

How to assign echo in variable

I've testing the following code: echo test.txt | cut -d . -f1and get the output "text" So why can't i assign the command to a variable? VAR='"echo test.txt | cut -d . -f1"' echo $VAR (5 Replies)
Discussion started by: jl487
5 Replies

5. Shell Programming and Scripting

Problem in echo value after some calculation

val=21 total=3250 echo "`echo "scale=2; $val*100/$total" | bc`" Output: .64 How do i show the output become "0.64" instead of ".64" ?? Someone can help? (1 Reply)
Discussion started by: alvin0618
1 Replies

6. Shell Programming and Scripting

Calculation in Multiple files using awk

Hi All, I have some 10 files named samp1.csv, samp2.csv,... samp10.csv Each file having the same number of fields like, Count, field1, field2, field3. And a source.csv file which has three fields field1, field2, field3. Now, i want to find the total count by taking the field1,... (8 Replies)
Discussion started by: johnwilliams.sp
8 Replies

7. UNIX for Dummies Questions & Answers

echo multiple lines

I have a code: echo "First Line ^M Second Line" | mail -s "Lines" abc@gmail.com Basically, I want to send an email with text in this format: First Line Second Line But there is something wrong with my 'echo'. The ^M is not interpreted as carriage return. Please help. (6 Replies)
Discussion started by: laiko
6 Replies

8. Shell Programming and Scripting

assigning variable value using echo

I am modifying an existing script and it has the following line: export SomeEnvVar=`echo ${SomeLocalVar}` Why wouldn't it just read: export SomeEnvVar=${SomeLocalVar} Is there some reason to use echo instead of a direct assignment? :confused: (2 Replies)
Discussion started by: shellburger
2 Replies

9. Shell Programming and Scripting

echo variable problem

hi I have say five variable. I would ask the user which one they want me to print and then print accordingly. TEST_1='10.2.3.4' TEST_2='11.2.3.5' TEST_3='12.2.3.5' TEST_4='13.2.3.5' TEST_5='14.2.3.5' print_var() { echo "Accessing var num $1" echo TEST$1 #??? But How do... (6 Replies)
Discussion started by: sabina
6 Replies

10. UNIX for Dummies Questions & Answers

Display from a variable using echo.

I have a variable that is outputting a lot of space. here has been 45 lines returned ... how can I remove the spaces between the "been and the 45" CODE: fil_len=`wc -l < coshb.txt` if ; then cat coshb.txt | more echo " " echo "There has been ${fil_len} lines... (4 Replies)
Discussion started by: jagannatha
4 Replies
Login or Register to Ask a Question