-1

I want to be able to add numbers under data and bss section out of the size information of a file in command line

./script.sh [file name]

So far I wrote my Shell script as :

ExcPath=$1 #read file name from command line
Numberone= size $1 | $data #put data column into Numberone
Numbertwo= size $1 | $bss #put bss column into Numbertwo
sum=$(( $Numberone + $Numbertwo )) # calculate the sum of DATA and BSS
echo $sum 

$data and $bss are variables that I assumed that it is how shell reads from column "data" and "bss"

output from size test:

text   data    bss    dec    hexfile name
2231    600      8   2839      b17   test

Expected output after running my script:

608

How could I achieve this in Shell Script? What did I do wrong?

  • You say what the expected output, but not what the actual output is. Please add this to your question. – ajgringo619 Sep 26 '20 at 16:59
  • That’s not how pipes work. $data and $bss are undeclared variables. You have to pipe the output of size to another program that can read the standard output (stdout) of the first program as input (stdin). – jsbillings Sep 26 '20 at 17:16
  • Numberone= size $1 | $data means: 1/ set Numberone to the empty string (because the space separates the variable assignment from the actual command; 2/ then run the command size $1 which, if $1 is a valid file will give its size, unless you have redefined size; 3/ | says to pipe the output of 2/ into the standard input of the command referred to by $data. Clearly not what you are trying to achieve. The shell is not the best tool for text processing, awk is a good alternative, so while the response provided by @Praveen Kumar is a bit terse, it is a technically valid answer. – asoundmove Sep 28 '20 at 21:24

2 Answers2

2

I'm afraid your script is completely wrong. Please take the time to read some basic tutorials on shell scripting so you can understand what you need to do. Note that you need to actually read the file, not just put it in a variable.

The most common tool for this sort of thing is awk. With awk you could do:

$ awk 'NR>1{print $2+$3}' file
608

NR is the current line number, and $N is the Nth field of the current line. So that command tells awk to skip the first line (NR>1) and then print the sum of the second and third fields.


The shell is a horrible tool for parsing files, but if you absolutely must do it in the shell, you could try:

#/bin/bash

ExcPath=$1

Now, skip the first line and read the rest

tail -n+2 "$ExcPath" |

split on whitespace and save in the variables

while read text data bss dec hexfile name; do echo $(( $data + $bss)) done

Save that as script.sh and then run ./script.sh filename as you were doing:

$ ./script.sh file
608
terdon
  • 242,166
0

command

z=0;for l in `awk '{for(i=1;i<=NF;i++){if($i ~ /data|bss/){print i}}}' filename`; do m=`awk -v l="$l" 'BEGIN{sum=0}{sum=sum+$l}END{print sum}' p`;z=$(($z+$m)); done;echo $z

output

608
  • While this is a technically good answer, it would go a long way if you could explain how it works to the OP. – asoundmove Sep 28 '20 at 21:17
  • Thanks for comment. it will find in which column number data or bss exsists and it will add corresponding column number and print the output – Praveen Kumar BS Sep 30 '20 at 18:31