0

I'm new to bash, and i'm trying to write a script that checks if sudo is installed. And if not, ask if the user wants to install it.

But i'm getting this error (note that x is what i type in response to read on line 8)

line 9: x: command not found
line 11: x: command not found

I searched for this problem and found this answer, but it does not bring up my problem.

And this is my code:

#!/bin/bash

# Checking for sudo
if ! dpkg-query -l sudo &> /dev/null; then
    echo -e "sudo is not installed"
    echo -e "sudo is required to continue"
    echo -e "install sudo? (y/n)"
    read
    if "$REPLY" = "y"; then
        su -c 'apt install sudo -y' root
    elif "$REPLY" = "n"; then
        echo -e "aborting setup"
        exit
    else
        echo -e "unknown response, aborting setup"
        exit
    fi
fi
TazTheManiac
  • 392
  • 1
  • 8
  • See https://stackoverflow.com/questions/18544359/how-to-read-user-input-into-a-variable-in-bash – Piccolo Aug 08 '18 at 00:02
  • @jeremysprofile I’m using this now ‘read -p “install sudo (y/n): “ REPLY’. But I’m still getting the same error. – TazTheManiac Aug 08 '18 at 00:20
  • 1
    You need square brackets around the test expression in the `if` and `elif` statements (i.e. `if [ "$REPLY" = "y" ]; then`). See [my answer here](https://stackoverflow.com/questions/49849957/bash-conditional-based-on-exit-code-of-command) for more explanation of what's going on (although that question is about the opposite problem to what you're having). – Gordon Davisson Aug 08 '18 at 00:24
  • @that other guy If you think my question is a duplicate, could you link to the original? – TazTheManiac Aug 08 '18 at 00:25
  • @Gordon Davisson Thank you, that worked! – TazTheManiac Aug 08 '18 at 00:31

0 Answers0