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

--silent exists with code 1 #210

Closed
its-dibo opened this issue Feb 9, 2023 · 3 comments
Closed

--silent exists with code 1 #210

its-dibo opened this issue Feb 9, 2023 · 3 comments
Labels

Comments

@its-dibo
Copy link

its-dibo commented Feb 9, 2023

if the --silent option provided, the script hides the error but still exit with code 1, we just need to totally ignore the error.
otherwise provide another option --ignore to totally ignore errors and exit with code 0

shx --ignore cp -r non-exists dist
@nfischer
Copy link
Member

nfischer commented Feb 9, 2023

If you just want to ignore the error, I suggest appending || shx true to the command. Here's an example:

$ shx --silent cp -r does-not-exist/ path/to/destination/ || shx true

In that example, cp -r does-not-exist/ path/to/destination/ represents a command which might fail. shx --silent at the beginning means it will not print the error message and || shx true at the end means the full command will always have exit code 0.

Let me know if this works for you.

@nfischer
Copy link
Member

nfischer commented Feb 9, 2023

I forgot to mention: the || shx true part should be portable across most operating systems. This relies on your shell to evaluate the && or || syntax, but this is a fairly safe assumption because it's supported by sh, bash, zsh, and even on Windows (cmd.exe). This is even supported in new versions (v3+) of the fish shell (fish-shell/fish-shell#4620).

This should definitely be good enough for npm package scripts because npm executes those in either sh or cmd.exe depending on the operating system version (source: https://docs.npmjs.com/cli/v9/commands/npm-run-script#script-shell).

@its-dibo
Copy link
Author

nice suggestion but it easer and more readable to achieve it by --ignore, especially if the command has multiple sub commands like this

shx cmd | cmd2 && shx cmd3

@nfischer

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

No branches or pull requests

2 participants