1

The script is this

#!/bin/bash

echo
echo "################################################################"
echo "  Installing Htop                                               "
echo "################################################################"
echo

if ! location=$(type -p "htop"); then
    sudo apt install -y htop
fi

I'm confused as to what this code snippet from the script does location=$(type -p "htop"); I need a clear explanation about this.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • There is a lot unnecessary there. `location=` just assigns the result of the *command substitution* `$(type -p "htop")` where `type -p htop` will return the absolute path to `htop` if it is found (nothing if it isn't) so upon finding no `htop`, there is an empty assignment `location=""` along with `type` returning `1` which when inverted with `!` make the `if` test `true` -- installing `htop`. Better `if ! type -p htop; then ...` – David C. Rankin Dec 02 '22 at 20:54
  • Basically `it not htop found; then install htop`. – David C. Rankin Dec 02 '22 at 20:59
  • After the `if` the variable `location` might or might not be set and you don't know why (i.e. if install fails there would be no `htop` yet) – Diego Torres Milano Dec 02 '22 at 21:05
  • `help type` is your friend. – Charles Duffy Dec 02 '22 at 21:13
  • BTW, note that in general, ["please explain this code" questions are too broad to be on-topic here](https://meta.stackoverflow.com/questions/260466/where-should-could-someone-please-explain-this-code-to-me-questions-go). – Charles Duffy Dec 02 '22 at 21:15
  • ...the "check if a program is already installed" question linked as a duplicate is someone trying to do the same thing your sample code already does. Same thing, from a different direction. – Charles Duffy Dec 02 '22 at 21:17

2 Answers2

3
  • ! negates the exit status of the following command;
  • location=... assigns a value to the variable $location;
  • $(...) is command substitution. It expands to the output of the enclosed command, whose exit status is propagated as the assignment's exit status;
  • type -p htop (double quotes are not needed here) searches for an executable htop in the $PATH and returns the full path to it. It fails if no such executable exists and there's no alias nor function named htop (in which case it returns an empty string, but doesn't fail).

Putting it all together, it searches for an executable named htop, assings the the full path to it to $location, and if it can't be found (and there's no alias or function defining it), it runs sudo apt install -y htop, which on some systems (that use apt to manage packages) tries to install the htop package with root privileges, answering yes to any questions.

choroba
  • 231,213
  • 25
  • 204
  • 289
1

In short, the exit status of the assignment is the exit status of the command substitution, and the exit status of the command substitution is the exit status fo type.

type -p htop has an exit status of 0 if htop is a command that can be executed, with the output being the full path to the command.

The idea here is that location is assigned the full path to htop if it exists, and if it doesn't, then sudo apt install -y htop is run to install it. (With the slight problem, alluded to in the comments, that location remains if htop needs to be installed.)

chepner
  • 497,756
  • 71
  • 530
  • 681