So at the beginning of my script, I am defining "threshold," which has a number that isn't going to exist in the next part, (2) and a number that will exist in the next part (6). I'm having the result of running df -h
to a file called dffile. My question, is how do I get grep in line 7 to search all of the variable "threshold" for the number that will exist in the file? It works if I have the 6 before the 2 in the variable, so it seems as if it's only searching the first number in it. Thanks!
#!/bin/bash
threshold=("2%" "6%")
df -h > dffile
grep $threshold dffile >> thresh
cat thresh | awk '{print $6}' >> finding1
LINES=()
while IFS= read -r finding1
do
find $finding1 -xdev -size +40M -exec ls -lah {} \; | head -n 10
done < "finding1"
The output of df -h
on my test server is:
root@tstd0001:~/scripts# df -h
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 0% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
As you can see above, "2" from my variable, does not exist, whereas "6" does. My goal is to make grep find any number that matches a number inside the variable.
Let's consider that cat output_df
is the output of your df -h
command:
$ cat output_df
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 2% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 6% /dev/shm
tmpfs 5.0M 0 5.0M 2% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
Then you change the first part of your script in the following way:
thresold="2%|6%"
cat output_df | awk -v VAR=$thresold '{if($5~VAR)print $6}'
of course you will have to replace cat output_df
by df -h
in the final script.
This will give the output:
/dev
/
/dev/shm
/run/lock
Explanations:
thresold="2%|6%"
is a regex that matches 2%
or 6%
you can generalize it to "2%|6%|X%|Y%|...|Z%"
-v VAR=$thresold
awk
to print the 6th field when the 5th field does match the regex that you have passed to it via '{if($5~VAR)print $6}'
Then you can regroup everything without using intermediate files:
thresold="2%|6%"
for f in `df -h | awk -v VAR=$thresold '{if($5~VAR)print $6}'`
do
find $f -xdev -size +40M -exec ls -lah {} \; 2>/dev/null | head -n10
done
On my box it gives the following output:
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/srcpkgcache.bin
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/pkgcache.bin
-rw-r--r-- 1 root root 62M 4月 28 02:27 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
-rw-r--r-- 1 root root 56M 2月 20 06:10 /usr/lib/libreoffice/program/libmergedlo.so
-rw-r--r-- 1 root root 73M 5月 22 19:31 /usr/lib/thunderbird/libxul.so
-rwxr-xr-x 1 root root 142M 5月 22 01:35 /usr/lib/chromium-browser/chromium-browser
-rw-r--r-- 1 root root 41M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.28.2
-rw-r--r-- 1 root root 54M 11月 17 2017 /usr/lib/x86_64-linux-gnu/libLLVM-5.0.so.1
-rw-r--r-- 1 root root 99M 3月 18 2017 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
-rwxr-xr-x 1 root root 42M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitPluginProcess2
...
Notes: 2>/dev/null
this redirection is to remove all the permission errors by redirecting stderr
to /dev/null
(muting the stderr
)