-1

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'

  1. ubuntu
  2. android
  3. windows
  4. 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?

1 Answers1

0

This is not doing your homework, but rather giving you more to study :) ... Keep in mind that the following examples are far from perfect and are meant only to get you started learning, so do your homework.

Example # 1

This is a pointer to text globbing and non-short-circuiting case operator ;;& in bash:

echo -e "1) Ubuntu\n2) Android\n3) Windows\n4) quit"
read -r -p "Execute Backup (COMMA Separated): " pick
    case "${pick}" in
        *1*)
            echo "backing up Ubuntu ..."
        ;;&
        *2*)   
            echo "backing up Android ..."
        ;;&
        *3*)
            echo "backing up Windows ..."
        ;;
        "4")
        echo "User requested exit"
        exit
        ;;
        *[^1-3]) echo "invalid option ${pick}";;
    esac

Demonstration:

1) Ubuntu
2) Android
3) Windows
4) quit
Execute Backup (COMMA Separated): 1,2,3
backing up Ubuntu ...
backing up Android ...
backing up Windows ...

Example # 2

You can read the user input into an array and then process the array elements in a shell for loop and group similar tasks/jobs in a function for easily calling certain tasks at a certain point like for example:

#!/bin/bash

Create a function to show the menue and read the user comma delimited input into an array

function menu { echo -e "1) Ubuntu\n2) Android\n3) Windows\n4) quit" IFS=',' read -r -p "Execute Backup (COMMA Separated): " -a pick }

Create a function to process the user input

function process { for element in "${pick[@]}" do # Check for invalid input using a RegEx pattern if [[ "${pick[*]}" =~ [^(123 )] ]] then # If array has only one element "4" if [ "${#pick[@]}" = 1 ] && [ "$element" = "4" ] then echo "User requested exit" exit else echo "Invalid input: ${pick[*]}" main break fi elif [ "$element" = "1" ] then echo "backing up Ubuntu ..." elif [ "$element" = "2" ] then echo "backing up Android ..." elif [ "$element" = "3" ] then echo "backing up Windows ..." fi done }

Create the main function to call the above two

function main { menu; process; }

call the function "main"

main

Demonstration:

1) Ubuntu
2) Android
3) Windows
4) quit
Execute Backup (COMMA Separated): 1,2,3
backing up Ubuntu ...
backing up Android ...
backing up Windows ...
Raffa
  • 34,963