You are not logged in.

#1 2022-07-23 16:38:40

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Determine if directories in variable 'directories' contain corrupted..

Determine if directories in variable 'directories' contain corrupted files

So here is my script thus far:

#!/bin/bash
#checkctl, JaydenDev 2022, MIT License

directories=("/boot" "/usr/lib" "/usr/lib32" "/usr/bin")

for group in "${directories[@]}"; do
    echo Processing directory: $group
    file $group/* | grep empty | grep -v ELF
    RESULT=$?
    if [ $RESULT -eq 0 ]; then
         echo Corrupted files in $group
     else
        echo No corrupted files in $group
    fi
done

I have one example empty file in /usr/bin/ called rpmbuild. The script should detect this, but instead, here's the output:

Processing directory: /usr/lib
No corrupted files in /usr/lib
Processing directory: /usr/lib64
No corrupted files in /usr/lib64
Processing directory: /usr/lib32
No corrupted files in /usr/lib32
Processing directory: /usr/bin
No corrupted files in /usr/bin
Processing directory: /bin
No corrupted files in /bin
Processing directory: /sbin
No corrupted files in /sbin
Processing directory: /usr/sbin
No corrupted files in /usr/sbin

Last edited by JaydenDev (2022-07-23 17:10:15)


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#2 2022-07-23 16:58:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Determine if directories in variable 'directories' contain corrupted..

How do you equate empty with "corrupted"?

Also why all the variables and looping?  And why loop through the same directories many times (as many of your directories in the array are symlinks to each other)?

If you just want to find empty files, use `find` with `-empty`:

find /usr/lib /usr/lib32 /usr/bin -type f -empty

But as for why yours fails - I suspect it's that you redirect the output of `file` to /dev/null, so you ignore all the results.

Last edited by Trilby (2022-07-23 17:01:10)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2022-07-23 17:06:43

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

Trilby wrote:

How do you equate empty with "corrupted"?

Also why all the variables and looping?  And why loop through the same directories many times (as many of your directories in the array are symlinks to each other)?

If you just want to find empty files, use `find` with `-empty`:

find /usr/lib /usr/lib32 /usr/bin -type f -empty

But as for why yours fails - I suspect it's that you redirect the output of `file` to /dev/null, so you ignore all the results.

Removing the >&/dev/null causes the script to function. I assume if the binary or library is an empty file, then it's probably corrupted. Same goes for binaries. This checks system files, binaries, and libraries. OP updated with latest script.

Last edited by JaydenDev (2022-07-23 17:07:02)


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#4 2022-07-23 17:51:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Determine if directories in variable 'directories' contain corrupted..

Aside from the same question of why all the gymnastics rather than just using `find`, your new grep pipeline will do nothing.  A file cant possibly be empty and be an ELF.  And what do you think RESULT will get set to?

Last edited by Trilby (2022-07-23 17:53:17)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2022-07-23 18:01:17

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

Trilby wrote:

Aside from the same question of why all the gymnastics rather than just using `find`, your new grep pipeline will do nothing.  A file cant possibly be empty and be an ELF.  And what do you think RESULT will get set to?

some files contain 'empty' in their name.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#6 2022-07-23 18:18:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Determine if directories in variable 'directories' contain corrupted..

JaydenDev wrote:

some files contain 'empty' in their name.

And that doesn't suggest to you a problem in your design?  Just the same filtering out ELF files from that makes no sense.  Perhaps they are not empty, contain empty in their name, but are not ELF files.  Or maybe some empty files contain ELF in their name!  But I give up, you are ignoring most of the relevant questions.

Last edited by Trilby (2022-07-23 18:19:39)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2022-07-23 18:42:43

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

Trilby wrote:
JaydenDev wrote:

some files contain 'empty' in their name.

And that doesn't suggest to you a problem in your design?  Just the same filtering out ELF files from that makes no sense.  Perhaps they are not empty, contain empty in their name, but are not ELF files.  Or maybe some empty files contain ELF in their name!  But I give up, you are ignoring most of the relevant questions.

I'm working on fixing the bad practices and methods. You asked why I used that method in the first place, I answered/=.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#8 2022-07-23 19:52:20

seth
Member
Registered: 2012-09-03
Posts: 49,981

Re: Determine if directories in variable 'directories' contain corrupted..

This is an xy-problem?
https://bbs.archlinux.org/viewtopic.php?id=278309

sudo env LC_ALL=C pacman -Qkk | grep -v ', 0 altered files

will tell you where you got issues.

Online

#9 2022-07-23 19:55:06

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

seth wrote:

This is an xy-problem?
https://bbs.archlinux.org/viewtopic.php?id=278309

sudo env LC_ALL=C pacman -Qkk | grep -v ', 0 altered files

will tell you where you got issues.

That's most definitely better than looking to see if files are empty. However that IS OS specific, specific to Arch.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#10 2022-07-23 20:08:37

seth
Member
Registered: 2012-09-03
Posts: 49,981

Re: Determine if directories in variable 'directories' contain corrupted..

However that IS OS specific, specific to Arch.

The thread doesn't imply a different requirement?

Online

#11 2022-07-23 20:14:54

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

seth wrote:

However that IS OS specific, specific to Arch.

The thread doesn't imply a different requirement?

It's not required, I'll just have to rewrite the script if I every switch to another distro (I highly doubt I will, I use arch, I prefer arch, arch forever).


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#12 2022-07-23 20:27:14

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Determine if directories in variable 'directories' contain corrupted..

seth wrote:

This is an xy-problem?
https://bbs.archlinux.org/viewtopic.php?id=278309

sudo env LC_ALL=C pacman -Qkk | grep -v ', 0 altered files

will tell you where you got issues.

Just an add tip;)
Only show the warnings:

sudo env LC_ALL=C pacman -Qkk | grep -v ', 0 altered files' > /dev/null

or if you must;) see the backup file:

sudo env LC_ALL=C pacman -Qkk | grep -v ', 0 altered files' 2> /dev/null

Offline

#13 2022-07-23 20:29:01

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

Thanks very much everyone! Once I finish this up it'll be ready for its first stable release on GitHub, the AUR, and my repository.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

#14 2022-07-23 20:30:28

seth
Member
Registered: 2012-09-03
Posts: 49,981

Re: Determine if directories in variable 'directories' contain corrupted..

JaydenDev wrote:

Determine if directories in variable 'directories' contain corrupted files

Trilby wrote:

How do you equate empty with "corrupted"?

Trilby wrote:

And that doesn't suggest to you a problem in your design?

In order to know whether a file is "corrupted" you must be able to compare it to some reference.
For arch packages, that's the checksum in the package database - for some other distro packages it's gonna be something different and for everything else, you'll have to provide that reference.

Online

#15 2022-07-23 21:48:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Determine if directories in variable 'directories' contain corrupted..

Don't randomly use sudo for commands that don't require root access.

And seriously, if you just want to find empty files, use find.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#16 2022-07-23 22:00:19

seth
Member
Registered: 2012-09-03
Posts: 49,981

Re: Determine if directories in variable 'directories' contain corrupted..

If you run pacman -Qkk w/o root permissions you'll be unable to test a bunch of files and get warnings for that.

Online

#17 2022-07-23 22:07:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Determine if directories in variable 'directories' contain corrupted..

True, but most - if not all - of the files in that category would quite commonly be edited and not match the package checksum anyways.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#18 2022-07-24 01:01:43

JaydenDev
Member
Registered: 2022-07-11
Posts: 172

Re: Determine if directories in variable 'directories' contain corrupted..

Alright so obviously I have a lot to work on with my bash scripting skills. So much so, some of them have a chance of being dangerous (not sure, but with the amount of bad practices in use its entirely possible). I will leave the development of this software here, and do some more practice.


System Specs:
Intel Core i5-2400 Nvidia GTX 1050ti Logitech G402 Hyperion Fury (Mouse) BestBuy Essentials USB Keyboard
Software Specifications:
Desktop Environment: KDE Plasma Window Manager: KWin Operating System: Arch Linux (btw)

Offline

Board footer

Powered by FluxBB