The original post was deleted because even though the bash/dash issue was resolved, primary backup objectives were not achieved/addressed:
Objectives: Designate partitions by number in order to sequentially execute multiple choice backups specified by comma delimited input at prompt (parse the input for multiple choice), entering authentication password only once.
Example: assign ubuntu as "1", android as "2", and windows as "3". Choose ubuntu and windows for backup at input prompt as follows: 1,3. Script should parse the multiplpe inputs and execute backups for ubuntu and windows.
Choose ubuntu and android and windows at input prompt as follows: 1,2,3. Parse input and execute backups for ubuntu, android and windows.
The command line (ommitted here) will specify the backup instructions.
Running the script at prompt:
'/backup.sh'
- ubuntu
- android
- windows
- Quit Choose backup:
(comma delimited number(s) chosen to be parsed and backup(s) sequentially executed)
Question remains how to modify for the objectives previously stated?
#!/bin/bash
PS3='Choose backup:'
backups=( "ubuntu" "android" "windows" "Quit" )
select pick in "${backups[@]}"; do
case $pick in
"ubuntu")
echo "backing up $pick"
sudo fsck -y -f /dev/sda2 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"android")
echo "backing up $pick"
sudo fsck -y -f /dev/sda3 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"windows")
echo "backing up $pick"
sudo fsck -y -f /dev/sda4 ;
sleep 1 ;
#sudo (commandline ommitted) ;
;;
"Quit")
echo "User requested exit"
exit
;;
*) echo "invalid option $REPLY";;
esac
done
Alternatively, could input be stored in an array and backups executed like this:
#!/bin/bash
IFS=$'\t' read -p "Execute Backup (COMMA Separated): " -a WHAT
for (how to reference array element 0?) "${WHAT[@]}"
do
sudo (backup command line here)
for (how to reference array element 2?) "${WHAT[@]}"
do
sudo (backup command line here)
for (how to reference array element 3?) "${WHAT[@]}"
do
sudo (backup command line here)
done
How to call the elements in an array and what is the syntax to separate the various for---do backup routines?
https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Jun 26 '23 at 17:47IFS=',' read -r -a array
… and then loop over array elements withfor element in "${array[@]}"; do …; done
… and inside thefor
loop, evaluate each element with like[ "$element" == "ubuntu" ] && command for ubuntu
… Like that :) – Raffa Jun 27 '23 at 17:28