-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
combined use of ID and ID_LIKE #372
Conversation
bin/etc-update
Outdated
@@ -33,12 +33,13 @@ get_config() { | |||
} | |||
|
|||
OS_RELEASE_ID=$(source /etc/os-release >/dev/null 2>&1; echo "${ID}") | |||
OS_RELEASE_ID_LIKE=$(source /etc/os-release >/dev/null 2>&1; echo "${ID_LIKE}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can extract multiple variables from a sourced file in this way:
eval "$(source /etc/os-release &>/dev/null; declare -A OS_RELEASE_DATA=([ID]="${ID}" [ID_LIKE]="${ID_LIKE}"); declare -p OS_RELEASE_DATA)"
And then use "${OS_RELEASE_DATA[ID]}"
and "${OS_RELEASE_DATA[ID_LIKE]}"
.
So either directly case ":${OS_RELEASE_DATA[ID]}:${OS_RELEASE_DATA[ID_LIKE]}:" in
or assign OS_RELEASE_ID="${OS_RELEASE_DATA[ID]}"
and OS_RELEASE_ID_LIKE="${OS_RELEASE_DATA[ID_LIKE]}"
and use case ":${OS_RELEASE_ID}:${OS_RELEASE_ID_LIKE}:" in
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't abuse eval
. Ever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My solution avoids spawning 2 subprocesses and opening /etc/os-release
twice for reading.
It is also possible to send contents of variables to temporary files, but that only avoids subprocess problem.
So there is no consensus that this would be any abuse of eval
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many solutions to every problem, and the one using eval
is almost always wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a way to read the file once, without using eval:
for var in OS_RELEASE_ID OS_RELEASE_ID_LIKE; do
read -r -d '' || break
declare ${var}="${REPLY}"
done < <(source /etc/os-release >/dev/null 2>&1; printf -- '%s\0%s\0' "${ID}" "${ID_LIKE}")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're all sort of missing the forest for the trees. Not only is eval (a dangerous thing which should only be used with care, and this doesn't qualify) completely unnecessary, you don't need two variables to begin with.
OS_RELEASE_POSSIBLE_IDS=$(source /etc/os-release >/dev/null 2>&1; echo "${ID}:${ID_LIKE}")
case ":${OS_RELEASE_POSSIBLE_IDS}:" in
The variables are used once, right here and now. They don't need to be uniquely available. No read loop hacks, no frivolous use of eval, no opening the file twice for reading, no forking bash into a second subshell on top of the first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, now I see that the consumer joins them with a colon anyway, so it makes sense to join them in the subshell.
Implementation based on @eli-schwartz proposal, is there anything else needed? |
bin/etc-update
Outdated
fedora|rhel) OS_FAMILY='rpm' ;; | ||
arch|archarm|arch32|manjaro|antergos) OS_FAMILY='arch' NEW_EXT='pacnew';; | ||
*) OS_FAMILY='gentoo' ;; | ||
case "${OS_RELEASE_POSSIBLE_IDS}" in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please omit quotes for case statements, since they are unnecessary, just like in [[
.
This looks good, please remove the case statement quotes and squash all commits into one. |
We should be good, how do I squash all commits now? |
Signed-off-by: Kewl Fft xrjy@nygb.rh.bet (rot13) combined use of ID and ID_LIKE Signed-off-by: Kewl Fft xrjy@nygb.rh.bet (rot13)
Thanks @eli-schwartz , only one commit now |
@zmedico is it good for merging or do you need anything from me? thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I planning to merge things this weekend and make a release.
Merged in d5ed2a3, thanks! |
@@ -32,13 +32,13 @@ get_config() { | |||
"${PORTAGE_CONFIGROOT}"etc/etc-update.conf) | |||
} | |||
|
|||
OS_RELEASE_ID=$(cat /etc/os-release 2>/dev/null | grep '^ID=' | cut -d'=' -f2 | sed -e 's/"//g') | |||
OS_RELEASE_POSSIBLE_IDS=$(source /etc/os-release >/dev/null 2>&1; echo ":${ID}:${ID_LIKE}:") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally overlooked mentioning, that ID_LIKE can contain multiple, space-separated IDs. So this would really need to be ${ID_LIKE/ /:}
or it would not detect any ID_LIKE that has multiple associations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, then ${ID_LIKE/ /:}
will only separate the first 2 IDs with :
, what about if there are 3 of them?
I think we want ${ID_LIKE// /:}
here, do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…or you could just, like, use space as the separator and then you wouldn't have to replace anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then also quote the part in the case statement, then be personally responsible for making sure no one ever accidentally tries to optimize the leading and trailing spaces out of OS_RELEASE_POSSIBLE_IDS. But sure.
I'd personally avoid using spaces as a glob pattern match delimiter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With spaces, the case patterns become quite heavy and more prone to errors.
Signed-off-by: Xrjy SSG xrjy@nygb.rh.bet (rot13)