0

I'm trying to store the pathway to a website in an array, but am having trouble.

It is working in the for loop - when I echo the contents of the array, it prints correctly.

In the If/Else, though, I am trying to store a specific part of the array in another variable, and that is not working. When I echo the specific part of the array, it prints nothing.

Here is the code:

#! /bin/sh

SITES=()
i=0

for d in /var/www/sites/*/;
do (
    PATHWAY=$d
    SITES+=($PATHWAY)
    echo "$i: $PATHWAY"
);  let "i++"
done

read -p 'Enter the number of the website you want to restore. Enter "exit" to quit. ' url

if [ "$url" = "exit" ]
then
    exit 0

else
    RESTORE_URL=${SITES[url]}
fi
  • why not use select path in /var/www/sites/*; do [...]; done? – DopeGhoti Oct 20 '17 at 22:19
  • You are missing a $ on url. Should be: RESTORE_URL=${SITES[$url]}. Also I guess it's not functionally any different but you should use declare -a SITES instead of SITES=() – Jesse_b Oct 20 '17 at 22:20
  • Does /bin/sh support arrays? – Jeff Schaller Oct 20 '17 at 22:37
  • @Jesse_b I had tried it with both of those, previously, and it still did not work. – Kimberlie Davis Oct 20 '17 at 22:52
  • @JeffSchaller, I guess it depends on what OS is being used. /bin/sh is normally a symlink to bash though. @Kimberlie, try again. Your issue is that url doesn't have a $. – Jesse_b Oct 20 '17 at 23:35
2

This sounds like a good place to use a select statement:

select path in /var/www/sites/* EXIT; do
    if [[ "EXIT" = "$path" ]]; then
        exit 0
    else
        : do stuff here referencing $path
    fi
done
  • I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server. – Kimberlie Davis Oct 20 '17 at 22:55
  • 1
    @KimberlieDavis: that's almost exactly what select does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array. – dave_thompson_085 Oct 21 '17 at 8:16
0

Your loop (with my indentation):

for d in /var/www/sites/*/; do
    (   PATHWAY=$d
        SITES+=($PATHWAY)
        echo "$i: $PATHWAY"  )

    let "i++"
done

Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.

Ether change the (...) to {...;} or remove them completely.

A better alternative for the script as a whole would be to use select as DopeGhoti explains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.