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?
size
to another program that can read the standard output (stdout) of the first program as input (stdin). – jsbillings Sep 26 '20 at 17:16Numberone= size $1 | $data
means: 1/ setNumberone
to the empty string (because the space separates the variable assignment from the actual command; 2/ then run the commandsize $1
which, if$1
is a valid file will give its size, unless you have redefinedsize
; 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