0

I'm trying to create a script that allows a user to choose backup destinations from the mounted volumes.

While I had a first draft working it had repetition which I've tried to remove by rebuilding in a nested for loop, however by the end of the script my variables become undefined and i can't see what mistake I've made.

why are my variables defined at line 19 but not at lines 27-29?

I am a beginner, apologies if the answer is obvious.

#!/bin/bash

VOLUMES=$(ls /volumes)

WORKING_DRIVE=
MASTER_DRIVE=
CLONE_DRIVE=

echo
for BACKUP_DESTINATION in 'Working Drive' 'Master Drive' 'Clone Drive'
do
    for VOLUME_VARIABLE in "${WORKING_DRIVE}" "${MASTER_DRIVE}" "${CLONE_DRIVE}"
    do
        echo "Select your ${BACKUP_DESTINATION}"
        echo
        select VOLUME_VARIABLE in $VOLUMES
        do
            echo
            echo "${BACKUP_DESTINATION}: $VOLUME_VARIABLE"
            echo
            break
        done
        break
    done
done

echo "Working Drive is: ${WORKING_DRIVE}"
echo "Master Drive is: ${MASTER_DRIVE}"
echo "Clone Drive is: ${CLONE_DRIVE}"
4
  • Do you expect echo "${BACKUP_DESTINATION}: $VOLUME_VARIABLE" to assign a variable? echo is used to write to screen, not to set variables. Commented Jan 23, 2021 at 18:43
  • Where do you assign values to $WORKING_DRIVE, $MASTER_DRIVE and $CLONE_DRIVE? Commented Jan 23, 2021 at 18:46
  • Also, you have both a for VOLUME_VARIABLE in loop and a for VOLUME_VARIABLE in loop that try to set VOLUME_VARIABLE in conflicting ways. Additionally, both of those "loops" have unconditional break commands, so they won't actually loop. The logic of this script... doesn't make sense. Commented Jan 23, 2021 at 18:50
  • @Gordan Davisson, my thinking was that the 2nd for loop would provide the undefined variables for the select command, and through the select command the user would define the variables? The echo command on line 17 (i thought) showed that the variable was defined. I inserted the break commands as the loops otherwise were continuing more than once for each destination. Commented Jan 23, 2021 at 19:42

1 Answer 1

0

You can do something like this:

#!/bin/bash

VOLUMES=$(ls /volumes)
DATA=''

function selectDestination() {
    echo "Select your ${1}"
    echo
    select VOLUME_VARIABLE in $VOLUMES
    do
        echo
        echo "${1}: $VOLUME_VARIABLE"
        echo
        break
    done

    DATA=${VOLUME_VARIABLE}
}

echo

selectDestination 'Working Drive'
WORKING_DRIVE=${DATA}
selectDestination 'Master Drive'
MASTER_DRIVE=${DATA}
selectDestination 'Clone Drive'
CLONE_DRIVE=${DATA}


echo "Working Drive is: ${WORKING_DRIVE}"
echo "Master Drive is: ${MASTER_DRIVE}"
echo "Clone Drive is: ${CLONE_DRIVE}"
Sign up to request clarification or add additional context in comments.

3 Comments

VOLUMES=$(ls /volumes)VOLUMES=(/volumes/*) and select VOLUME_VARIABLE in $VOLUMESselect VOLUME_VARIABLE in "${VOLUMES[@]}"
thank you @Lucas Belfanti, this works the way I want script to work. I don't know the function command yet but i will learn it. is my nested loop idea a completely dead end?
Well, you could use your nested loop idea, but you cannot assign a value to a different value on each iteration. You should use some kind of collection, like an array. Also if your question is answered, please mark it as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.