From 7ea37440b1d079aaee451b05b548e81890168c6e Mon Sep 17 00:00:00 2001 From: Igor Pecovnik Date: Tue, 5 Nov 2024 17:00:47 +0100 Subject: [PATCH] ZSH: move functions from JSON to separate modules --- tools/json/config.system.json | 18 ++---------- tools/modules/system/manage_zsh.sh | 47 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 tools/modules/system/manage_zsh.sh diff --git a/tools/json/config.system.json b/tools/json/config.system.json index cef50210..56244259 100644 --- a/tools/json/config.system.json +++ b/tools/json/config.system.json @@ -214,14 +214,7 @@ "id": "SY008", "description": "Change shell system wide to BASH", "about": "This will switch system wide shell to BASH", - "command": [ - "export BASHLOCATION=$(grep /bash$ /etc/shells | tail -1)", - "sed -i \"s|^SHELL=.*|SHELL=${BASHLOCATION}|\" /etc/default/useradd", - "sed -i \"s|^DSHELL=.*|DSHELL=${BASHLOCATION}|\" /etc/adduser.conf", - "apt_install_wrapper apt-get -y purge armbian-zsh zsh-common zsh tmux", - "update_skel", - "awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534 || $3 == 0) print $1}' /etc/passwd | xargs -L1 chsh -s $(grep /bash$ /etc/shells | tail -1)" - ], + "command": [ "manage_zsh disable" ], "status": "Stable", "author": "@igorpecovnik", "condition": "[[ $(cat /etc/passwd | grep \"^root:\" | rev | cut -d\":\" -f1 | cut -d\"/\" -f1| rev) == \"zsh\" ]]" @@ -230,14 +223,7 @@ "id": "SY009", "description": "Change shell system wide to ZSH", "about": "This will switch system wide shell to ZSH", - "command": [ - "export ZSHLOCATION=$(grep /zsh$ /etc/shells | tail -1)", - "sed -i \"s|^SHELL=.*|SHELL=${ZSHLOCATION}|\" /etc/default/useradd", - "sed -i \"s|^DSHELL=.*|DSHELL=${ZSHLOCATION}|\" /etc/adduser.conf", - "apt_install_wrapper apt-get -y install armbian-zsh zsh-common zsh tmux", - "update_skel", - "awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534 || $3 == 0) print $1}' /etc/passwd | xargs -L1 chsh -s $(grep /zsh$ /etc/shells | tail -1)" - ], + "command": [ "manage_zsh enable" ], "status": "Stable", "author": "@igorpecovnik", "condition": "[[ $(cat /etc/passwd | grep \"^root:\" | rev | cut -d\":\" -f1 | cut -d\"/\" -f1| rev) == \"bash\" ]]" diff --git a/tools/modules/system/manage_zsh.sh b/tools/modules/system/manage_zsh.sh new file mode 100644 index 00000000..97876ef8 --- /dev/null +++ b/tools/modules/system/manage_zsh.sh @@ -0,0 +1,47 @@ +module_options+=( + ["manage_zsh,author"]="@igorpecovnik" + ["manage_zsh,ref_link"]="" + ["manage_zsh,feature"]="manage_zsh" + ["manage_zsh,desc"]="Set system shell to BASH" + ["manage_zsh,example"]="manage_zsh enable|disable" + ["manage_zsh,status"]="Active" +) +# +# @description Set system shell to ZSH +# +function manage_zsh() { + + local bash_location=$(grep /bash$ /etc/shells | tail -1) + local zsh_location=$(grep /zsh$ /etc/shells | tail -1) + + if [[ "$1" == "enable" ]]; then + + sed -i "s|^SHELL=.*|SHELL=/bin/zsh|" /etc/default/useradd + sed -i -E "s|(^\|#)DSHELL=.*|DSHELL=/bin/zsh|" /etc/adduser.conf + + # install + apt_install_wrapper apt-get -y install armbian-zsh zsh-common zsh tmux + + update_skel + + # change shell for root + usermod --shell "/bin/zsh" root + # change shell for others + sed -i 's/bash$/zsh/g' /etc/passwd + + else + + sed -i "s|^SHELL=.*|SHELL=/bin/bash|" /etc/default/useradd + sed -i -E "s|(^\|#)DSHELL=.*|DSHELL=/bin/bash|" /etc/adduser.conf + + # remove + apt_install_wrapper apt-get -y remove armbian-zsh zsh-common zsh tmux + + # change shell for root + usermod --shell "/bin/bash" root + # change shell for others + sed -i 's/zsh$/bash/g' /etc/passwd + + fi + +}