Skip to content

Conversation

@Photon89
Copy link
Member

@Photon89 Photon89 commented Jan 5, 2026

The currently used /etc/issue seems to be less suiting (only gives \S{PRETTY_NAME} \r (\l) for me on Manjaro).

With /etc/os-release we would get

NAME="Manjaro Linux"
BUILD_ID=rolling

bin/shutter Outdated
if (-f '/etc/os-release') {
if (File::Which::which('sed')) {
print `sed '1q;d' /etc/os-release`;
print `sed '5q;d' /etc/os-release`, "\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these magic numbers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They grab the 1st and 5th line in the file. The complete file looks like this for me:

NAME="Manjaro Linux"
PRETTY_NAME="Manjaro Linux"
ID=manjaro
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="32;1;24;144;200"
HOME_URL="https://manjaro.org/"
DOCUMENTATION_URL="https://wiki.manjaro.org/"
SUPPORT_URL="https://forum.manjaro.org/"
BUG_REPORT_URL="https://manjaro.org/help/"
PRIVACY_POLICY_URL="https://manjaro.org/privacy-policy/"
LOGO=manjarolinux

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please treat this file as key/value pairs instead of counting which line is on which position.

NAME=Gentoo
ID=gentoo
PRETTY_NAME="Gentoo Linux"
ANSI_COLOR="1;32"
HOME_URL="https://www.gentoo.org/"
SUPPORT_URL="https://www.gentoo.org/support/"
BUG_REPORT_URL="https://bugs.gentoo.org/"
VERSION_ID="2.18"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to know a bash tool which can read a file this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come on, this is perl, which is designed for text processing. Why do you need bash or other external tools?

I printed the NAME, feel free to adjust

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everyone is a perl expert :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, this code is way over my level! 🥲

@Photon89
Copy link
Member Author

Photon89 commented Jan 7, 2026

I added version/build ID info. As far as I understand, only one of the variables is set in /etc/os-release, so putting both of them into the output string will give a nice output including only the one provided.

In my case I get "Manjaro Linux rolling".

@DarthGandalf
Copy link
Member

What happens if both are set?

There's no guarantee which of them should be set

@Photon89
Copy link
Member Author

Photon89 commented Jan 7, 2026

What happens if both are set?

Then we get something like "Distro 1.23rolling". So there is no space character between version and build ID. But if we add a space character, there will be two of them in "Distro rolling".

@DarthGandalf
Copy link
Member

 			my %map = map {
				chomp;
				my ($key, $value) = split /=/, $_, 2;
				$value =~ s/^(['"])(.*)\1$/$2/;
				($key, $value)
			} <$fh>;

<$fh> reads the file line by line, and executes the block in map{} for each line, setting $_ to the line.
chomp removes \n from the end of $_
split finds first = and produces array of at most "2" elements - to the left of first = and to the right of it; then elements of these array are assigned to 2 vars.
Then s/// removes quotes from start and end of the value, where the starting quote needs to match the ending quote - both are ether ' or ".
The return value of map block is array of 2 elements, so they are concatenated to one long array key1/value1/key2/value2/etc, but it's assigned to %map, which takes the elements pairwise and understands first of each pair to be the key and second the value.

			local $, = ' ';
			say grep { $_ } map { $map{$_} } qw/NAME VERSION_ID BUILD_ID/;

qw// is same as ("NAME", "VERSION_ID", "..."), just spelled more compactly, this is idiomatic perl for array of strings.
map function turns it into array of values, by looking up each key in %map; during lookup in the %map the sigil becomes $ instead
Then grep function takes the array of values, and removes values which are falsy
Then say function prints the array to stdout, using $, as separator between elements
local is how such special vars are normally temporarily modified - the value of the var will revert to previous value when this statement goes out of scope.

@Photon89
Copy link
Member Author

Photon89 commented Jan 9, 2026

Sounds like magic! 😄

I tested the latest commit, it produces a nice output on my machine. Unfortunately, I can't approve because I created this PR.

@Photon89 Photon89 merged commit 6190c45 into master Jan 9, 2026
1 check passed
@Photon89 Photon89 deleted the debug-output branch January 11, 2026 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants