I am having a menu based script which calls for an additional shell script. The challenge is that, after the additional shell script is finished, instead of returning to the main menu, it exits completely.
The code is something like this:
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 is having the code:
#!/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
Tried sourcing the script and then using return function, but it didn't helped (or I have done something wrong).
Any idea on how to return to the main menu after the backup-script.sh is finishing running?
Thank you!
backup-script.shas abashscript (see its first line), do not call it withsh. In many casesshis not the same asbash. Either usebashto call it or (better) make the script executable withchmod a+x backup-script.shand then just invoke it as./backup-script.sh.breakout of theselectloop. I think perhaps you are misunderstanding the role ofbreakhere (i.e. thinking it is breaking out of thecaseconstruct - bash case...esac does not "drop through" in the same way as in some languages).