fix(installer): run chsh with sudo if user has privileges

This fixes the error in Google Cloud Shell, where a password prompt
appears when running `chsh` but the user (hello) does not have a
password.

If ran with `sudo`, the `chsh` command happens without a password
prompt.
This commit is contained in:
Marc Cornellà 2022-01-11 16:18:00 +01:00
parent 9c84c344d7
commit b7a59e6d5c
No known key found for this signature in database
GPG key ID: 0314585E776A9C1B

View file

@ -317,7 +317,7 @@ EOF
"$YELLOW" "$RESET" "$YELLOW" "$RESET"
read -r opt read -r opt
case $opt in case $opt in
y*|Y*|"") echo "Changing the shell..." ;; y*|Y*|"") ;;
n*|N*) echo "Shell change skipped."; return ;; n*|N*) echo "Shell change skipped."; return ;;
*) echo "Invalid choice. Shell change skipped."; return ;; *) echo "Invalid choice. Shell change skipped."; return ;;
esac esac
@ -355,11 +355,20 @@ EOF
if [ -n "$SHELL" ]; then if [ -n "$SHELL" ]; then
echo "$SHELL" > ~/.shell.pre-oh-my-zsh echo "$SHELL" > ~/.shell.pre-oh-my-zsh
else else
grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
fi fi
# Actually change the default shell to zsh echo "Changing your shell to $zsh..."
if ! chsh -s "$zsh"; then
# Check if user has sudo privileges and run `chsh` or `sudo chsh`
if LANG= sudo -l -U "$USER" 2>/dev/null | grep -q "is not allowed to run"; then
chsh -s "$zsh" "$USER" # run chsh normally
else
sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt
fi
# Check if the shell change was successful
if [ $? -ne 0 ]; then
fmt_error "chsh command unsuccessful. Change your default shell manually." fmt_error "chsh command unsuccessful. Change your default shell manually."
else else
export SHELL="$zsh" export SHELL="$zsh"