From da443aaa8f0e5210a0917e322ec0c262d62282a6 Mon Sep 17 00:00:00 2001 From: Konano Date: Wed, 25 Oct 2023 14:36:22 +0800 Subject: [PATCH] add confirm_action --- init_priv.sh | 44 +++++++++++--------------------------------- init_user.sh | 20 +++++--------------- utils.sh | 13 +++++++++++++ 3 files changed, 29 insertions(+), 48 deletions(-) diff --git a/init_priv.sh b/init_priv.sh index b5baa4f..ef744c5 100755 --- a/init_priv.sh +++ b/init_priv.sh @@ -17,9 +17,7 @@ fi configure_tuna() { [ $abroad -eq 1 ] && return - read -p "要切换 Tuna 源吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要切换 Tuna 源吗?" "N"; then python3 $scriptdir/files/oh-my-tuna.py # 如果 uid 不为 0 则 sudo 再跑一次 if [[ $EUID -ne 0 ]]; then @@ -29,9 +27,7 @@ configure_tuna() { } configure_upgrade() { - read -p "要运行 apt upgrade 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要运行 apt upgrade 吗?" "N"; then sudo apt update sudo apt upgrade -y sudo apt autoremove -y @@ -39,9 +35,7 @@ configure_upgrade() { } configure_install() { - read -p "要运行 apt/pip3 install 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要运行 apt/pip3 install 吗?" "N"; then sudo apt update sudo apt install tmux git curl htop net-tools tcptraceroute tar unzip -y sudo apt install hexyl bat neofetch fd-find fzf pigz -y @@ -61,9 +55,7 @@ configure_install() { configure_nano() { [ -f ~/.nanorc ] && return - read -p "要配置 nano 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 nano 吗?" "Y"; then sudo apt install nano -y cp $scriptdir/files/.nanorc ~/.nanorc fi @@ -72,9 +64,7 @@ configure_nano() { configure_vim() { [ -f ~/.vimrc ] && return - read -p "要配置 vim 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 vim 吗?" "Y"; then sudo apt install vim -y cp $scriptdir/files/.vimrc ~/.vimrc fi @@ -83,9 +73,7 @@ configure_vim() { configure_zsh() { [ -f ~/.zshrc ] && return - read -p "要配置 zsh 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 zsh 吗?" "Y"; then sudo apt install zsh -y rm ~/.oh-my-zsh -r @@ -105,9 +93,7 @@ configure_zsh() { configure_tmux() { [ -f ~/.tmux.conf ] && return - read -p "要配置 tmux 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 tmux 吗?" "Y"; then sudo apt install tmux -y rm ~/.tmux -r @@ -125,9 +111,7 @@ configure_tmux() { configure_fail2ban() { [ -f /etc/fail2ban/jail.local ] && return - read -p "要配置 fail2ban 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 fail2ban 吗?" "N"; then sudo apt install fail2ban sudo mkdir -p /etc/fail2ban sudo cp $scriptdir/files/jail.local /etc/fail2ban/jail.local @@ -140,9 +124,7 @@ configure_fail2ban() { configure_iptables() { [ -f /etc/network/if-pre-up.d/iptables-load ] && return - read -p "要配置 iptables 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 iptables 吗?" "N"; then sudo cp $scriptdir/files/iptables-load /etc/network/if-pre-up.d/iptables-load sudo chmod +x /etc/network/if-pre-up.d/iptables-load sudo sh /etc/network/if-pre-up.d/iptables-load @@ -150,18 +132,14 @@ configure_iptables() { } configure_hostname() { - read -p "要修改 hostname 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要修改 hostname 吗?" "N"; then read -p "hostname: " new_hostname echo $new_hostname | sudo tee /etc/hostname >/dev/null fi } configure_timedate() { - read -p "要修改 时区 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要修改 时区 吗?" "N"; then sudo timedatectl set-timezone Asia/Shanghai fi } diff --git a/init_user.sh b/init_user.sh index ee56b55..8ce2234 100755 --- a/init_user.sh +++ b/init_user.sh @@ -13,9 +13,7 @@ update_file $scriptdir/files/ys-simple.zsh-theme ~/.oh-my-zsh/themes/ys-simple.z configure_ssh() { [ -f ~/.ssh/authorized_keys ] && return - read -p "要配置 authorized_keys 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 authorized_keys 吗?" "Y"; then mkdir -p ~/.ssh cp $scriptdir/files/authorized_keys ~/.ssh/authorized_keys fi @@ -24,9 +22,7 @@ configure_ssh() { configure_ssh_keygen() { [ -f ~/.ssh/id_ed25519.pub ] && return - read -p "要生成 sshkey 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要生成 sshkey 吗?" "N"; then mkdir -p ~/.ssh read -p "sshkey 的名字: " sshkeyname ssh-keygen -t ed25519 -C $sshkeyname @@ -36,9 +32,7 @@ configure_ssh_keygen() { configure_git() { [ -f ~/.gitconfig ] && return - read -p "要配置 git 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要配置 git 吗?" "N"; then cp $scriptdir/files/.gitconfig ~/.gitconfig fi } @@ -46,9 +40,7 @@ configure_git() { configure_miniconda() { [ -d ~/.miniconda3 ] && return - read -p "要安装 miniconda 吗?[N]: " response - - if [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要安装 miniconda 吗?" "Y"; then wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O $tempdir/miniconda.sh bash $tempdir/miniconda.sh -b -p ~/.miniconda3 rm $tempdir/miniconda.sh @@ -62,9 +54,7 @@ configure_miniconda() { configure_nodejs() { [ -d ~/.nvm/.git ] && return - read -p "要安装 nodejs 吗?[Y]: " response - - if [[ -z "$response" ]] || [[ $response =~ ^[Yy]$ ]]; then + if confirm_action "要安装 nodejs 吗?" "Y"; then git_clone nvm-sh/nvm ~/.nvm --depth=1 \. $scriptdir/files/install-nvm.sh \. ~/.nvm/nvm.sh diff --git a/utils.sh b/utils.sh index 23c69a8..68c59c2 100755 --- a/utils.sh +++ b/utils.sh @@ -40,6 +40,19 @@ update_file() { fi } +confirm_action() { + local prompt="$1" + local default_response="$2" + + read -p "$prompt [$default_response]: " response + + if [[ -z "$response" ]]; then + response="$default_response" + fi + + [[ $response =~ ^[Yy]$ ]] +} + # ============================================================ check_google_access