dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.
MIT License
36.41k stars 3.27k forks source link

Shorter if syntax #112

Open qiufeihai opened 3 years ago

qiufeihai commented 3 years ago

[[ 1 == 1 ]] && { command exit 1 } || { this command will exec } how to avoid???

linux478 commented 2 years ago

I do not understand what you are trying to tell.

Skizo4 commented 2 years ago

Thank you very much.

On Thu, Dec 9, 2021, 4:04 PM Michael Phillips @.***> wrote:

I do not understand what you are trying to tell.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dylanaraps/pure-bash-bible/issues/112#issuecomment-989989149, or unsubscribe https://github.com/notifications/unsubscribe-auth/AV7BMB2TZLLR2FZW4FR3FHTUQDHPZANCNFSM4WTEZRYA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

TedHartDavis commented 2 years ago

I am so confused by this.

TinCanTech commented 2 years ago

Missing terminating quote, bracket or keyword

terminalforlife commented 2 years ago

I'm sure you found a solution by now, but just in-case you didn't:

You have two syntax errors, because you're missing the ; before the final } in each command group. The typical way to group commands (without a subshell) is { like this; }. Grouping a single command is redundant.

The COMMAND && COMMAND || COMMAND approach does not equate to if, then, else, although it can work in a similar way. Likewise, COMMAND || COMMAND && COMMAND does not equate to the opposite.

Why? Because what the first example actually means is that the 2nd command will execute if the 1st one succeeds, or the 3rd command will execute if the 1st command fails. However, if the 1st command succeeds and the 2nd command also happens to fail, then all three commands wind up being executed regardless. Understanding this is very important, as you can wind up doing some pretty stupid stuff otherwise.

Furthermore, the exit statuses are not contained, unlike with an if statement, which is pretty important if it's the last command in a script or you're checking $?.

YasserKa commented 2 years ago

"Shorter if syntax" should be removed to stop spreading miss-information.

More context can be found here: http://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3

terminalforlife commented 2 years ago

Agreed, it's misleading and incorrect. I see what the author meant, but it's important to be accurate when it comes to complicated matters like programming, IMO. Logical operators can be used similar to an if statement, in a simple way, but it's not technically or actually a replacement or "shorter" version.