Skip to content

Sync logical operators in shell scripting code #13560

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

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

petk
Copy link
Member

@petk petk commented Feb 29, 2024

This updates the obsolescent -a and -o binary primaries to && and ||.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html

@petk petk merged commit 42a4e50 into php:master Mar 1, 2024
@petk petk deleted the patch-posix-test-utility branch March 1, 2024 19:40
@thg2k
Copy link
Contributor

thg2k commented Mar 1, 2024

Didn't test it, but doesn't this spawn multiple processes per condition? What was wrong with -a and -o? Seems like a case of "don't fix it if it's not broken"

@petk
Copy link
Member Author

petk commented Mar 1, 2024

@thg2k, these usages are obsolete. Not broken and will most likely work everywhere in current shells, yet just written with low quality. See POSIX notes above. This syncs it with the rest of the usages across other files. And so we have less confusions like these: #13396 (comment) which style to use.

And Autoconf docs suggestion also looks good to me: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/autoconf.html#Limitations-of-Shell-Builtins

There are probably gazillion other issues in these shell scripts and code, so we'll see which to address and which to leave out. Like these "x$var" comparisons and similar. For now, that's good.

Multiple processes per condition? I'm not sure that's the case here. No?

@thg2k
Copy link
Contributor

thg2k commented Mar 2, 2024

Thank you for the pointers @petk, I've read it up and indeed it says they are obsolete. I find it hard to believe as I grew up writing my shell scripts with "-a" and "-o", but what can you do, progress is progress.

About the spawning multiple shells, I checked with strace and it does not. What I mean is that test expr1 -a expr2 would ideally spawn one sub-process, evaluate the full expression, and exit 0 or 1, whereas test expr1 && test expr2 would spawn two sub-processes, as in the generic cmd1 && cmd2. But as test is a shell-builtin, it seems this is all evaluated within the same process.

All in all, I will still continue writing my scripts with -a and -o, but thanks to you now I know I officially write obsolete code 😆

@petk
Copy link
Member Author

petk commented Mar 2, 2024

@thg2k I'm glad to hear that. Yes, you can safely use these doubled, tripled, etc. test calls inside the if. Yes, that && operator in those commands is a bit of a different thing. But I'm also not sure whether it would spawn two processes. 🤔 However, the & would spawn a process in the background.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants