-1

In bash coding,

What to do is that if the grep size is less than 800MB i want to ignore but if grep size is more than 800MB, i want to print that path in file xyz/symlinks_paths.txt. but problem coming in else statement. Can you help me, i want to print that path whose size is more than 800MB in xyz/size_list.txt file. Also, line3 is a directory path.

while
    read -r line3
        do
            if [[ "ls -lh $line3 | grep zzz.exe | grep '[8-9][0-9][0-9][MG]'" = " " ]] 
                then break
                else line3 >> xyz/size_list.txt
            fi
        done < xyz/symlinks_paths.txt
Pompy
  • 41
  • 9
  • `break` breaks out of the loop – 123 Feb 19 '18 at 14:40
  • No, the error says, line3 is a directory OR line3 command not found, that means after break it is in if loop only. – Pompy Feb 19 '18 at 14:43
  • https://stackoverflow.com/questions/21011010/how-to-break-out-of-an-if-loop-in-bash – Adam Burry Feb 19 '18 at 14:43
  • Maybe you should try with `find`, which lists all your symlinks and can even give you the information on sizes for found files. – Stefan M Feb 19 '18 at 14:49
  • No Stefan, i dont want to use find command, because i have to run this on huge database, which will take days on find. – Pompy Feb 19 '18 at 14:53

2 Answers2

1

You really shouldn't parse ls.

On the other hand, you're exiting your while-loop with the break statement. Maybe you can negate your if-condition, so that you can put your else-Part into the then part.

What were you trying to achieve by this line:

line3 >> xyz/size_list.txt

Do you want to append the contents of ${line3} into the file? Than this should work:

echo "${line3}" >> xyz/size_list.txt
Stefan M
  • 868
  • 6
  • 17
1

Imho, it's better (and more readable) to do like that:

#!/bin/bash

while
    read -r line3
        do
            #819200 kbytes = 800 Mbytes. Echo in file if the size greater or equal
            if [ $(du -s "${line3}/zzz.exe" | awk '{print $1}') -ge 819200 ]
            then 
                echo "${line3}" >> xyz/size_list.txt
            fi
        done < xyz/symlinks_paths.txt
Viktor Khilin
  • 1,760
  • 9
  • 21