0

I'm trying to write a shell script which will print the top 5 biggest repositories of Bitbucket and will present the Project name, Reposotry name, and its size. repository config file example:

[bitbucket] project = TEST repository = customer_management_test

output of du command:

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -5
2.0G    /bbhome/shared/data/repositories/1792
2.7G    /bbhome/shared/data/repositories/3517
3.0G    /bbhome/shared/data/repositories/2450
3.1G    /bbhome/shared/data/repositories/5703
4.4G    /bbhome/shared/data/repositories/2829

This the code I'm trying to run in REHL Bitbucket machine:

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -5
while IFS= read -r line;do
        DIR=`echo $line | awk '{print$2}'`
        Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
        Size=`echo $line | awk '{print $1}' `
        echo $Size $Rep
done

But I'm not getting the expected result.

Actual:

2.0G    /bbhome/shared/data/repositories/1792
2.7G    /bbhome/shared/data/repositories/3517
3.0G    /bbhome/shared/data/repositories/2450
3.1G    /bbhome/shared/data/repositories/5703
4.4G    /bbhome/shared/data/repositories/2829

Expected (one example for 1792):

2.0G   project = TEST  repository = customer_management_test 

what's wrong with the syntax?

  • 1
    ... you're not actually piping the output of the du pipeline into the while loop? – steeldriver Dec 17 '19 at 14:00
  • what do you mean? – arielma Dec 17 '19 at 14:10
  • Your first line runs du -sh /bbhome/shared/data/repositories/* |sort -h |tail -5 and outputs the result via stdout to the terminal. Then your while loop loops over standard input (which is empty). You need another | connecting stdout from the pipeline to stdin of the loop. – steeldriver Dec 17 '19 at 14:13
  • can you write me the correct syntax to use? – arielma Dec 17 '19 at 14:16
1

Your first line runs du -sh /bbhome/shared/data/repositories/* |sort -h |tail -5 and outputs the result via stdout to the terminal. Then your while loop loops over its standard input (which is empty).

You need another | connecting stdout from the pipeline to stdin of the loop:

du -sh /bbhome/shared/data/repositories/* |sort -h |tail -5 |
while IFS= read -r line; do

  <stuff with "$line">

done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.