-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?

  • What if the user specified "4,1"? – Jos Jun 26 '23 at 17:24
  • 2
    Ah! Now I understand. It's homework. We don't do homework. Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process. – waltinator Jun 26 '23 at 17:47
  • Thank you for the info regarding https://shellcheck.net, even though it does not address the stated objectives for parsing input and sequentially executing backups. The user would logically specify a numbered backup or backups, and specify 4 for ending execution of the script. – Rolfbruge Jun 27 '23 at 14:47
  • IFS=',' read -r -a array … and then loop over array elements with for element in "${array[@]}"; do …; done … and inside the for loop, evaluate each element with like [ "$element" == "ubuntu" ] && command for ubuntu … Like that :) – Raffa Jun 27 '23 at 17:28
  • 1
    Thank you Raffa for your help as the text globbing example worked fine with a few modifications. Still trying to figure out the syntax to nest with in for loop, the code to test the contents of the array, so it can be tied to a backup command. Would appreciate any pointers as to placement and syntax. Thanks again as your input has been very helpful. – Rolfbruge Jun 30 '23 at 14:54
  • @Rolfbruge I added an example for reading input into an array and processing it ... I have also linked to more resources to help you understand how different elements work. – Raffa Jul 01 '23 at 13:15

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
  • 32,237