Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Useful Terminal Commands.
#7
(09-26-2018, 07:31 PM)leon.p Wrote:
(09-26-2018, 04:34 PM)LIFE.LinuxIsForEveryone Wrote: Whenever I install updates, or need to run multiple commands (FYI this is not needed for multiple program installs in-line), I use "&&" in between the commands. 

EXAMPLE: sudo apt update && sudo apt dist-upgrade -yy
--- This isn't needed for programs, as a user can simply space the programs out:$ sudo apt install inxi htop 
Cheers!

I want to expand on that because this little trick is extremely useful but can also cause some headache.

Imagine you have the folloing:
Code:
command1 && command2

command2 will only be executed if command1 ran succesful, i.e. has an exit value of 0.
If command1 encounters an error (has an exit value above 0), command2 will not be run.

you can simulate that with the folloing:
Code:
false && echo "foo"

Now comes the interesting part: You can use || instead of && to run command2 only when command1 fails:
Code:
false || echo "foo"    # "foo" will be displayed
true || echo "foo"    # "foo" will not be displayed

You can also combine both into a one-line if-else-statement
Code:
command1 && command2 || command3

If command1 is succesful, command2 will be executed, if command1 was not succesful, command3 will be executed.

Code:
true && echo "foo" || echo "bar"    # "foo" will be displayed
false && echo "foo" || echo "bar"    # "bar" will be displayed


If you want to run multiple commands and don't want the entire thing to stop when one command encounters an error, use ';':
Code:
echo "foo"; echo "bar"

Noice! As Johnny Carson used to say "Well, uh, I did not KNOW that" Smile

I know a decent amount of terminal commands, but my use is somewhat limited thus far (only been 2.5 years) that to be honest I've never used "&&" and had it fail. So again, thanks for expanding in-depth with all of that. That's all FANTASTIC information to know, for me & everyone reading this thread.

This is one of the multitude of reasons that Linux is so fun for me - I absolutely LOVE learning new & more-proper ways to use the terminal, and learning more and more commands as I go. Plus, learning how to _properly_ use specific commands as well, which  I may have been slightly foggy about previously. But I honestly had no idea regarding the double pipes, etc. Exclamation

Also one additional simple yet useful command I forgot to add near "uname -a" is:
$lsb_release -a
---Gives you all of the pertinent info regarding the (Distro)
  1. Distributor ID
  2. Description
  3. Release
  4. Codename
Cheers!
Human Being
---
Linux
Is
For
Everyone
Reply


Messages In This Thread
Useful Terminal Commands. - by bob777 - 09-02-2018, 02:13 AM
RE: Useful Terminal Commands. - by Cthulhu - 09-02-2018, 02:26 AM
RE: Useful Terminal Commands. - by leon.p - 09-02-2018, 01:56 PM
RE: Useful Terminal Commands. - by bescott9944 - 09-07-2018, 12:27 AM
RE: Useful Terminal Commands. - by leon.p - 09-26-2018, 07:31 PM
RE: Useful Terminal Commands. - by LIFE.LinuxIsForEveryone - 09-27-2018, 12:38 AM
RE: Useful Terminal Commands. - by bescott9944 - 06-07-2019, 05:12 AM
RE: Useful Terminal Commands. - by inraidius - 09-27-2018, 02:29 PM
RE: Useful Terminal Commands. - by SirGuyCarleton - 09-27-2018, 11:23 PM
RE: Useful Terminal Commands. - by rick forges - 09-28-2018, 10:01 PM
RE: Useful Terminal Commands. - by leon.p - 10-01-2018, 01:22 PM
RE: Useful Terminal Commands. - by waterismygod - 02-04-2019, 02:02 PM
RE: Useful Terminal Commands. - by Cthulhu - 03-23-2019, 09:47 AM
RE: Useful Terminal Commands. - by leon.p - 03-25-2019, 06:59 PM
RE: Useful Terminal Commands. - by smartin - 07-03-2019, 10:20 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)