diff options
author | Marc Cornellà <hello@mcornella.com> | 2022-01-11 16:18:00 +0100 |
---|---|---|
committer | Marc Cornellà <hello@mcornella.com> | 2022-01-11 16:45:36 +0100 |
commit | b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3 (patch) | |
tree | 1d03f04bf58433ae7409ca14e485a768b7eaba70 /tools/install.sh | |
parent | 9c84c344d762b200de7acc794b9a0e7832144e7a (diff) | |
download | zsh-b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3.tar.gz zsh-b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3.tar.bz2 zsh-b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3.zip |
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.
Diffstat (limited to 'tools/install.sh')
-rwxr-xr-x | tools/install.sh | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/tools/install.sh b/tools/install.sh index 5009bd586..d3be1ace4 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -317,7 +317,7 @@ EOF "$YELLOW" "$RESET" read -r opt case $opt in - y*|Y*|"") echo "Changing the shell..." ;; + y*|Y*|"") ;; n*|N*) echo "Shell change skipped."; return ;; *) echo "Invalid choice. Shell change skipped."; return ;; esac @@ -355,11 +355,20 @@ EOF if [ -n "$SHELL" ]; then echo "$SHELL" > ~/.shell.pre-oh-my-zsh 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 - # Actually change the default shell to zsh - if ! chsh -s "$zsh"; then + echo "Changing your shell to $zsh..." + + # 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." else export SHELL="$zsh" |