0

So I have a bash script which will scan a directory and run a function on a selected input.

    echo -n "
-------------------------------------------------------
            `hostname` Menu
-------------------------------------------------------

Choose your desired Host:
`
i=1
for D in /var/www/*.*/;
    do   
        if [[ -d $D ]]; then
            Hosts[$i]=$D
            echo $i') ' $D
            i=$(($i+1))
        fi
    done
    `
"
read -p "Select: " Input
    Directory="${Hosts[$Input]}";
    ChangePerms;

It seems that for some reason, the key is not setting from the array properly.

Choose your desired Host:
1)  /var/www/example1.com/
2)  /var/www/example2.com/
+ read -p 'Select: ' Input
Select: 1
+ Directory=
+ ChangePerms
+ true

What am I missing? All help is much appreciated!

EDIT: As a side note, I can run the commands separately and get results.

root@web005:/# i=1
root@web005:/# for D in /var/www/*.*/;
>     do
>         if [[ -d $D ]]; then
>             Hosts[$i]=$D
>             echo $i') ' $D
>             i=$(($i+1))
>         fi
>     done
1)  /var/www/example1.com/
2)  /var/www/example2.com/
root@web005:/# read -p "Select: " Input
Select: 1
root@web005:/# Directory="${Hosts[$Input]}";
root@web005:/# echo $Directory
/var/www/example1.com/
root@web005:/#

Here is the working code for those who get stuck like me:

echo -n "
-------------------------------------------------------
            `hostname` Menu
-------------------------------------------------------

Choose your desired Host:

"
i=1
for D in /var/www/*.*/;
    do   
        if [[ -d $D ]]; then
            Hosts[$i]=$D
            echo $i') ' $D
            i=$(($i+1))
        fi
    done
read -p "Select: " Input
    Directory="${Hosts[$Input]}";
    ChangePerms;

2 Answers 2

5

Your problem is:

`
i=1
for D in /var/www/*.*/;
    do   
        if [[ -d $D ]]; then
            Hosts[$i]=$D
            echo $i') ' $D
            i=$(($i+1))
        fi
    done
`

which executes in a subshell. Therefore, the remainder of your script has no idea what Hosts is. (in other words, Hosts is empty in the main body of your script). Fill Hosts before you present your menu.

Sign up to request clarification or add additional context in comments.

1 Comment

Genius! I would upvote if I had the reputation to do so! Thank you! Edited code with working example.
1

Your quoting seems odd to me. This seems to work:

#!/bin/sh
echo -n "
-------------------------------------------------------
            `hostname` Menu
-------------------------------------------------------

Choose your desired Host:
"
i=1
for D in /tmp/www/*.*/ ; do   
        if [[ -d $D ]]; then
                Hosts[$i]=$D
                echo $i') ' $D
            i=$(($i+1))
        fi
    done
read -p "Select: " Input
Directory="${Hosts[$Input]}";
echo $Directory

1 Comment

This was the simplest edit to make the answer by @david-c-rankin work! Thank you! I had the funny quotation, because I used to have a static case statement but I wanted this to be portable across all of my servers!

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.