Add up a column of numbers at the Unix shell

后端 未结 20 2268
Happy的楠姐
Happy的楠姐 2021-01-29 18:10

Given a list of files in files.txt, I can get a list of their sizes like this:

cat files.txt | xargs ls -l | cut -c 23-30

which pr

20条回答
  •  孤独总比滥情好
    2021-01-29 18:46

    You can use the following script if you just want to use shell scripting without awk or other interpreters:

    #!/bin/bash
    
    total=0
    
    for number in `cat files.txt | xargs ls -l | cut -c 23-30`; do
       let total=$total+$number
    done
    
    echo $total
    

提交回复
热议问题