JP Vossen via plug on 24 Nov 2021 15:12:36 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Running commands: was: Sys76


On 11/24/21 4:08 PM, Steve Litt via plug wrote:
jeffv via plug said on Wed, 24 Nov 2021 12:40:03 -0500
How To Run A Command After The Previous One Has Finished On Linux
https://www.linuxuprising.com/2021/11/how-to-run-command-after-previous-one.html
Using this, you can run not just 2 commands, but any number of
commands, after the previous one has finished. This will work with Sh,
Bash, Zsh and other shells.

Thank you jeffv! I've been looking for this capability for years. Until
now, when I ran more than one command after the and or or, I put those
commands to be run after the and or or in a subroutine, which was extra
work. Now, for just a couple commands, I'll just use the braces.

`{ }` simply denote code blocks, while `( )` spawns a sub-shell.  They are different, and that can mess you up because while the sub-shell inherits variables from the parent it can't pass them back and any changes in the sub-shell are lost!

Use `[[ expression ]] && block` or `[[ expression ]] || block` when simple and readable. Do not use `[[ expression ]] && block || block` because that doesn't do what you think it does, use `if .. then .. (elif ..) else.. fi` for that.

The `&&` and `||` operators are of *equal* precedence and are left associative.  Prove it:
	$ echo 1 && echo 2 || echo 3
	$ echo 1 || echo 2 && echo 3

Related, the examples for the forthcoming (2022-04) _bash Idioms_ are up at https://github.com/vossenjp/bashidioms-examples, in particular https://github.com/vossenjp/bashidioms-examples/blob/main/appa/bash_idioms_style_guide.md.  :-)

Later,
JP
--  -------------------------------------------------------------------
JP Vossen, CISSP | http://www.jpsdomain.org/ | http://bashcookbook.com/
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug