Saturday, October 8, 2022

[SOLVED] Using && and || to check installed tools

Issue

I am testing if someone tools is installed in the system with command

In this case I am checking if unzip is installed. if unzip is installed the nothing happend but if unzip is not installed, the script will run apt-get update and apt-get install unzip.

All this I prefer run it in one all.

I am trying this command:

command -v unzip && echo "[+] unzip is installed in the system" || echo "[!] unzip is not installed in the system" && apt-get update && apt-get install unzip

With that command always I am installing unzip.

How can i do to install unzip ONLY if unzip is not installed?

Thank you


Solution

To group between logical statements, you need to enclose them in curly braces. The syntax is like this { ... ; }.

$ command -v bash &>/dev/null && echo 'Installed' || { echo 'Not installed' && echo "Please dont" && echo 'Yes, please do'; }
Installed

$ { command -v bash &>/dev/null && echo 'Installed'; } || { echo 'Not installed' && echo "Please dont" && echo 'Yes, please do'; }
Installed

$ command -v notexist &>/dev/null && echo 'Installed' || { echo 'Not installed' && echo "Please dont" && echo 'Yes, please do'; }
Not installed
Please dont
Yes, please do

$ { command -v notexist &>/dev/null && echo 'Installed'; } || { echo 'Not installed' && echo "Please dont" && echo 'Yes, please do'; }
Not installed
Please dont
Yes, please do

Note: A semicolon ; before the closing braces is important.



Answered By - Darkman
Answer Checked By - Dawn Plyler (WPSolving Volunteer)