Using arrays in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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






share|improve this question






















  • 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















up vote
0
down vote

favorite












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






share|improve this question






















  • 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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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






share|improve this question














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








share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 22:09









amphibient

4,42993469




4,42993469










asked Oct 20 '17 at 22:04









Kimberlie Davis

82




82











  • 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

















  • 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
















why not use select path in /var/www/sites/*; do [...]; done?
– DopeGhoti
Oct 20 '17 at 22:19




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





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




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




@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





@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 Answers
2






active

oldest

votes

















up vote
2
down vote













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





share|improve this answer




















  • 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











  • Okay, I get it. That worked - thanks!!
    – Kimberlie Davis
    Oct 24 '17 at 21:30

















up vote
0
down vote













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.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399448%2fusing-arrays-in-shell-script%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    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





    share|improve this answer




















    • 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











    • Okay, I get it. That worked - thanks!!
      – Kimberlie Davis
      Oct 24 '17 at 21:30














    up vote
    2
    down vote













    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





    share|improve this answer




















    • 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











    • Okay, I get it. That worked - thanks!!
      – Kimberlie Davis
      Oct 24 '17 at 21:30












    up vote
    2
    down vote










    up vote
    2
    down vote









    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





    share|improve this answer












    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 20 '17 at 22:22









    DopeGhoti

    40.7k54979




    40.7k54979











    • 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











    • Okay, I get it. That worked - thanks!!
      – Kimberlie Davis
      Oct 24 '17 at 21:30
















    • 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











    • Okay, I get it. That worked - thanks!!
      – Kimberlie Davis
      Oct 24 '17 at 21:30















    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





    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




    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





    @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













    Okay, I get it. That worked - thanks!!
    – Kimberlie Davis
    Oct 24 '17 at 21:30




    Okay, I get it. That worked - thanks!!
    – Kimberlie Davis
    Oct 24 '17 at 21:30












    up vote
    0
    down vote













    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.






    share|improve this answer
























      up vote
      0
      down vote













      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.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 10 at 10:42









        Kusalananda

        105k14209326




        105k14209326



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399448%2fusing-arrays-in-shell-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?