Skip to main content
added 88 characters in body
Source Link
amphibient
  • 12.8k
  • 19
  • 69
  • 88

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

#! /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

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

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
Source Link

Using arrays in shell script

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