Skip to content
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

A standalone command to test exit codes #7153

Open
cmpute opened this issue Nov 17, 2022 · 4 comments
Open

A standalone command to test exit codes #7153

cmpute opened this issue Nov 17, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@cmpute
Copy link

cmpute commented Nov 17, 2022

Related problem

As I read through #5347, #3858, #5740, #6667, #7076, #6617, there is no native or even clean way to support the bash behavior cmd1 || cmd2.

Describe the solution you'd like

I would like to propose a way to solve this without big changes to the syntax or the pipeline mode of nushell. We just need a command that can convert an exit code to an boolean or error. Therefore I propose a new command (either named test or error test) to do this:

  • nu -c 'exit 0' | test will become true.
  • nu -c 'exit -1' | test will become false.
  • nu -c 'exit -1' | test -c 1 will become true. (it only return false when the exit code is 1)
  • nu -c 'exit -1' | test -e will become a nu error
  • nu -c 'exit -1' | test -e {msg: "non-zero return"} will become a nu error with custom message

With this command, the bash behavior of cmd1 || cmd2 will become (cmd1 | test) || (cmd2 | test), which is still elegant in my opinion. The behavior of cmd1 && cmd2 can also be supported this way.

Describe alternatives you've considered

One solution is to support the behavior natively as tried by #6374. But it turns out to be a big change and it might affect other scripts running right now.

There is also a workaround a I mentioned in #6667, we can use following sentences

  • if (cmd1 | complete).exit_code != 0 { cmd2 }
  • if (cmd1 | complete | $in.exit_code) != 0 { cmd2 }

All of these are less intuitive, and the problem is that the stdout and stderr of cmd1 will be captured by the complete command instead of being printed out.

Additional context and details

No response

@cmpute cmpute added the enhancement New feature or request label Nov 17, 2022
@webbedspace
Copy link
Contributor

I believe the Bash || operator is intended to be added before 0.80, according to this.

@fdncred
Copy link
Collaborator

fdncred commented Nov 17, 2022

can you do this with $env.LAST_EXIT_CODE? I have a prompt that displays exit codes in the prompt in red-ish.
Screenshot 2022-11-17 at 6 22 42 AM

@cmpute
Copy link
Author

cmpute commented Nov 17, 2022

can you do this with $env.LAST_EXIT_CODE?

Yes I can do if (cmd1; $env.LAST_EXIT_CODE == 0) { cmd2 }, but it's still cumbersome and it's hard to combine multiple commands.

@cmpute
Copy link
Author

cmpute commented Nov 17, 2022

I believe the Bash || operator is intended to be added before 0.80, according to this.

I actually think that, because nushell is a new shell designed from scratch, we can do better than bash. For the behavior of || and &&, we can definitely support it, but the functionality is still limited. The proposed command can convert a non-zero exit code to a nushell error, and it can raise error when specific exit code is returned.

For the behavior of >, >> and 2>, etc., I think nushell should not support them at all. These symbols caused me a lot of confusion when I first try to learn bash. I actually prefer just to use the save command for this. (though I'm not sure if there is a way to redirect stderr to stdout in nushell yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants