调用不同的脚本后返回主菜单

调用不同的脚本后返回主菜单

我有一个基于菜单的脚本,需要额外的 shell 脚本。挑战在于,附加 shell 脚本完成后,它不是返回主菜单,而是完全退出。

代码是这样的:

options=("Deploy code" "Backup a directory or file" "Transfer file SCP" "Start tool b" "Quit")
select opt in "${options[@]}"; do
   case $REPLY in
      1) command1; break;;
      2) sh ./backup-script.sh; break;;
      3) test1; break;;
      4) bla2; break;;
      5) break 2 ;;
      *) echo "Invalid option selected" >&2
   esac
done

backup-script.sh 的代码为:

#!/bin/bash

# define params
read -p "Provide full path of file(s) or folder(s) you want to create a backup of: " param_backup
backed_up_path=$(echo "${param_backup}_bkp_$(date +"%Y-%m-%d_%H-%M-%S")");

# execute backup with params
echo 
echo "Attempting to create backup of specified file(s) or folder(s)..."
if cp -prvf ${param_backup} ${backed_up_path} 
    then
        echo
        printf "\033[1;32mSUCCESS: Backup created!\033[0m\n"
    else
        echo 
        printf "\033[1;31mERROR: There was an error and the backup could not be performed.\033[0m\n"
        printf "\033[1;31m       Please try again and make sure that the source path is valid or exists!\033[0m\n"
fi
echo

尝试获取脚本,然后使用返回函数,但它没有帮助(或者我做错了什么)。

知道如何在 backup-script.sh 运行完成后返回主菜单吗?

谢谢你!

答案1

通过使用以下命令使其按预期工作:

backup () { 
   ./backup_script.sh
   read -rsn1 -p "Press any key to go back to main menu"
   clear
}

options=("Deploy code" "Backup a directory or file" "Transfer file SCP" "Start tool b" "Quit")
select opt in "${options[@]}"; do
   case $REPLY in
      1) command1; ;;
      2) backup; ;;
      3) test1; ;;
      4) bla2; ;;
      5) break 2 ;;
      *) echo "Invalid option selected" >&2
   esac
done

相关内容