Need to access array after exit from SSH in bash 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 need to access array which is dynamically created.



First look into the code:



ssh username@11.22.333.44 <<'ENDSSH'
cd /home/ubuntu/user/someFolder
array=(`find . -name 'something*'`)
len=$#array[*]

i=0
while [ $i -lt $len ]; do
let i++
done
echo "$array[*]" #here I can access array values
ENDSSH
#Just above i have exited from ssh & now I need to access array values but not getting.
echo "$array[*]" #here I'm unable to get array values
exit


So, My concern is to access array after ENDSSH.



Regards & Thanks,
Shubham Kumar







share|improve this question

















  • 2




    That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
    – Andy Dalton
    Jul 3 at 14:35










  • @AndyDalton So is there any way by which I can access outside remote host?
    – Shubham Kumar Rohit
    Jul 3 at 14:35






  • 1




    You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
    – William Pursell
    Jul 3 at 14:35










  • @WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
    – Shubham Kumar Rohit
    Jul 3 at 14:36







  • 1




    What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
    – Kusalananda
    Jul 3 at 14:46














up vote
0
down vote

favorite












I need to access array which is dynamically created.



First look into the code:



ssh username@11.22.333.44 <<'ENDSSH'
cd /home/ubuntu/user/someFolder
array=(`find . -name 'something*'`)
len=$#array[*]

i=0
while [ $i -lt $len ]; do
let i++
done
echo "$array[*]" #here I can access array values
ENDSSH
#Just above i have exited from ssh & now I need to access array values but not getting.
echo "$array[*]" #here I'm unable to get array values
exit


So, My concern is to access array after ENDSSH.



Regards & Thanks,
Shubham Kumar







share|improve this question

















  • 2




    That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
    – Andy Dalton
    Jul 3 at 14:35










  • @AndyDalton So is there any way by which I can access outside remote host?
    – Shubham Kumar Rohit
    Jul 3 at 14:35






  • 1




    You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
    – William Pursell
    Jul 3 at 14:35










  • @WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
    – Shubham Kumar Rohit
    Jul 3 at 14:36







  • 1




    What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
    – Kusalananda
    Jul 3 at 14:46












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to access array which is dynamically created.



First look into the code:



ssh username@11.22.333.44 <<'ENDSSH'
cd /home/ubuntu/user/someFolder
array=(`find . -name 'something*'`)
len=$#array[*]

i=0
while [ $i -lt $len ]; do
let i++
done
echo "$array[*]" #here I can access array values
ENDSSH
#Just above i have exited from ssh & now I need to access array values but not getting.
echo "$array[*]" #here I'm unable to get array values
exit


So, My concern is to access array after ENDSSH.



Regards & Thanks,
Shubham Kumar







share|improve this question













I need to access array which is dynamically created.



First look into the code:



ssh username@11.22.333.44 <<'ENDSSH'
cd /home/ubuntu/user/someFolder
array=(`find . -name 'something*'`)
len=$#array[*]

i=0
while [ $i -lt $len ]; do
let i++
done
echo "$array[*]" #here I can access array values
ENDSSH
#Just above i have exited from ssh & now I need to access array values but not getting.
echo "$array[*]" #here I'm unable to get array values
exit


So, My concern is to access array after ENDSSH.



Regards & Thanks,
Shubham Kumar









share|improve this question












share|improve this question




share|improve this question








edited Jul 3 at 16:15









Kusalananda

101k13199312




101k13199312









asked Jul 3 at 14:30









Shubham Kumar Rohit

62




62







  • 2




    That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
    – Andy Dalton
    Jul 3 at 14:35










  • @AndyDalton So is there any way by which I can access outside remote host?
    – Shubham Kumar Rohit
    Jul 3 at 14:35






  • 1




    You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
    – William Pursell
    Jul 3 at 14:35










  • @WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
    – Shubham Kumar Rohit
    Jul 3 at 14:36







  • 1




    What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
    – Kusalananda
    Jul 3 at 14:46












  • 2




    That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
    – Andy Dalton
    Jul 3 at 14:35










  • @AndyDalton So is there any way by which I can access outside remote host?
    – Shubham Kumar Rohit
    Jul 3 at 14:35






  • 1




    You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
    – William Pursell
    Jul 3 at 14:35










  • @WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
    – Shubham Kumar Rohit
    Jul 3 at 14:36







  • 1




    What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
    – Kusalananda
    Jul 3 at 14:46







2




2




That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
– Andy Dalton
Jul 3 at 14:35




That array existed in the memory of the shell that ran on the remote host, not in the shell on the local host.
– Andy Dalton
Jul 3 at 14:35












@AndyDalton So is there any way by which I can access outside remote host?
– Shubham Kumar Rohit
Jul 3 at 14:35




@AndyDalton So is there any way by which I can access outside remote host?
– Shubham Kumar Rohit
Jul 3 at 14:35




1




1




You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
– William Pursell
Jul 3 at 14:35




You are constructing array on the remote host. You'll need to transmit the data back to the local host. Probably the easiest thing to do is to serialize it and write it to stdout, then have the script that is invoking ssh read the output stream.
– William Pursell
Jul 3 at 14:35












@WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
– Shubham Kumar Rohit
Jul 3 at 14:36





@WilliamPursell If you can provide any sample code or something will be best for me. However I can not do scp from remote host to my local as scp needs public ip.
– Shubham Kumar Rohit
Jul 3 at 14:36





1




1




What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
– Kusalananda
Jul 3 at 14:46




What are you wanting to do with those pathnames? If you want to do something with them on the remote host, don't turn them into a string and send them back and forth, just run whatever you need to run on the remote host with find directly.
– Kusalananda
Jul 3 at 14:46










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Since you're comfortable using backticks and find to construct the array, you clearly aren't worried too much about serializing the data robustly (for example, whitespace in the output of find is going to be split into distinct element of the array, which may not be desired), so just do:



array=($( ssh username@11.22.333.44 sh -c "find . -name 'something*'"))


and build the array on the local host.






share|improve this answer

















  • 1




    I think ssh runs a shell anyway, so you don't need the sh -c
    – ilkkachu
    Jul 3 at 14:40










  • sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
    – William Pursell
    Jul 3 at 14:45











  • But I need to run change directory after ssh username@11.22.333.44 and need to find.
    – Shubham Kumar Rohit
    Jul 3 at 14:47










  • You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
    – William Pursell
    Jul 3 at 14:48










  • @WilliamPursell Worked! Really appreciated alot.
    – Shubham Kumar Rohit
    Jul 3 at 14:52

















up vote
0
down vote













From one of your comments, it sounds as if what you're intending to do is to create a list of pathnames of files that you'd like to transfer from the remote system to the local machine using scp. The issue with this is that you passing pathnames back and forth between the two systems which introduces the risk that filenames that includes whitespace characters may be mangled.



If you're looking for a way to transfer all the files that matches something* from somewhere under /home/ubuntu/user/someFolder on the remote machine, you may use rsync like this:



rsync -av --include='*/' --include='something*' --exclude='*' 
--prune-empty-dirs
username@11.22.333.44:/home/ubuntu/user/someFolder/ ./target


This would find and transfer all files matching the pattern and the directory structure that they live in to the local machine under the path ./target.



The --include and --exclude patterns are applied from left to right and the first match is what matters:




  • --include='*/': Look into all subdirectories (empty directories, i.e. directories with no matching filenames, are not transferred due to --prune-empty-dirs).


  • --include='something*': The pattern that would match the things we're actually interested in.


  • --exclude='*': Ignore everything else.





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%2f453236%2fneed-to-access-array-after-exit-from-ssh-in-bash-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



    accepted










    Since you're comfortable using backticks and find to construct the array, you clearly aren't worried too much about serializing the data robustly (for example, whitespace in the output of find is going to be split into distinct element of the array, which may not be desired), so just do:



    array=($( ssh username@11.22.333.44 sh -c "find . -name 'something*'"))


    and build the array on the local host.






    share|improve this answer

















    • 1




      I think ssh runs a shell anyway, so you don't need the sh -c
      – ilkkachu
      Jul 3 at 14:40










    • sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
      – William Pursell
      Jul 3 at 14:45











    • But I need to run change directory after ssh username@11.22.333.44 and need to find.
      – Shubham Kumar Rohit
      Jul 3 at 14:47










    • You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
      – William Pursell
      Jul 3 at 14:48










    • @WilliamPursell Worked! Really appreciated alot.
      – Shubham Kumar Rohit
      Jul 3 at 14:52














    up vote
    2
    down vote



    accepted










    Since you're comfortable using backticks and find to construct the array, you clearly aren't worried too much about serializing the data robustly (for example, whitespace in the output of find is going to be split into distinct element of the array, which may not be desired), so just do:



    array=($( ssh username@11.22.333.44 sh -c "find . -name 'something*'"))


    and build the array on the local host.






    share|improve this answer

















    • 1




      I think ssh runs a shell anyway, so you don't need the sh -c
      – ilkkachu
      Jul 3 at 14:40










    • sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
      – William Pursell
      Jul 3 at 14:45











    • But I need to run change directory after ssh username@11.22.333.44 and need to find.
      – Shubham Kumar Rohit
      Jul 3 at 14:47










    • You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
      – William Pursell
      Jul 3 at 14:48










    • @WilliamPursell Worked! Really appreciated alot.
      – Shubham Kumar Rohit
      Jul 3 at 14:52












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Since you're comfortable using backticks and find to construct the array, you clearly aren't worried too much about serializing the data robustly (for example, whitespace in the output of find is going to be split into distinct element of the array, which may not be desired), so just do:



    array=($( ssh username@11.22.333.44 sh -c "find . -name 'something*'"))


    and build the array on the local host.






    share|improve this answer













    Since you're comfortable using backticks and find to construct the array, you clearly aren't worried too much about serializing the data robustly (for example, whitespace in the output of find is going to be split into distinct element of the array, which may not be desired), so just do:



    array=($( ssh username@11.22.333.44 sh -c "find . -name 'something*'"))


    and build the array on the local host.







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jul 3 at 14:39









    William Pursell

    2,00911111




    2,00911111







    • 1




      I think ssh runs a shell anyway, so you don't need the sh -c
      – ilkkachu
      Jul 3 at 14:40










    • sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
      – William Pursell
      Jul 3 at 14:45











    • But I need to run change directory after ssh username@11.22.333.44 and need to find.
      – Shubham Kumar Rohit
      Jul 3 at 14:47










    • You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
      – William Pursell
      Jul 3 at 14:48










    • @WilliamPursell Worked! Really appreciated alot.
      – Shubham Kumar Rohit
      Jul 3 at 14:52












    • 1




      I think ssh runs a shell anyway, so you don't need the sh -c
      – ilkkachu
      Jul 3 at 14:40










    • sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
      – William Pursell
      Jul 3 at 14:45











    • But I need to run change directory after ssh username@11.22.333.44 and need to find.
      – Shubham Kumar Rohit
      Jul 3 at 14:47










    • You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
      – William Pursell
      Jul 3 at 14:48










    • @WilliamPursell Worked! Really appreciated alot.
      – Shubham Kumar Rohit
      Jul 3 at 14:52







    1




    1




    I think ssh runs a shell anyway, so you don't need the sh -c
    – ilkkachu
    Jul 3 at 14:40




    I think ssh runs a shell anyway, so you don't need the sh -c
    – ilkkachu
    Jul 3 at 14:40












    sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
    – William Pursell
    Jul 3 at 14:45





    sh -c "cmd" makes it easier to understand the quoting. ssh user@host cmd with "quoted string" runs cmd with "quoted" and "string" as separate, unquoted arguments. ssh user@host sh -c "cmd with 'quoted string'" runs the command as expected. ssh user@host "cmd with 'quoted string'" I believe is exactly the same, but IMO looks really weird. YMMV. Including sh -c is purely a style thing.
    – William Pursell
    Jul 3 at 14:45













    But I need to run change directory after ssh username@11.22.333.44 and need to find.
    – Shubham Kumar Rohit
    Jul 3 at 14:47




    But I need to run change directory after ssh username@11.22.333.44 and need to find.
    – Shubham Kumar Rohit
    Jul 3 at 14:47












    You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
    – William Pursell
    Jul 3 at 14:48




    You don't actually need to cd. You can just as easily do find /home/ubuntu/user/someFolder, or you can do sh -c 'cd ..; find . ...'
    – William Pursell
    Jul 3 at 14:48












    @WilliamPursell Worked! Really appreciated alot.
    – Shubham Kumar Rohit
    Jul 3 at 14:52




    @WilliamPursell Worked! Really appreciated alot.
    – Shubham Kumar Rohit
    Jul 3 at 14:52












    up vote
    0
    down vote













    From one of your comments, it sounds as if what you're intending to do is to create a list of pathnames of files that you'd like to transfer from the remote system to the local machine using scp. The issue with this is that you passing pathnames back and forth between the two systems which introduces the risk that filenames that includes whitespace characters may be mangled.



    If you're looking for a way to transfer all the files that matches something* from somewhere under /home/ubuntu/user/someFolder on the remote machine, you may use rsync like this:



    rsync -av --include='*/' --include='something*' --exclude='*' 
    --prune-empty-dirs
    username@11.22.333.44:/home/ubuntu/user/someFolder/ ./target


    This would find and transfer all files matching the pattern and the directory structure that they live in to the local machine under the path ./target.



    The --include and --exclude patterns are applied from left to right and the first match is what matters:




    • --include='*/': Look into all subdirectories (empty directories, i.e. directories with no matching filenames, are not transferred due to --prune-empty-dirs).


    • --include='something*': The pattern that would match the things we're actually interested in.


    • --exclude='*': Ignore everything else.





    share|improve this answer



























      up vote
      0
      down vote













      From one of your comments, it sounds as if what you're intending to do is to create a list of pathnames of files that you'd like to transfer from the remote system to the local machine using scp. The issue with this is that you passing pathnames back and forth between the two systems which introduces the risk that filenames that includes whitespace characters may be mangled.



      If you're looking for a way to transfer all the files that matches something* from somewhere under /home/ubuntu/user/someFolder on the remote machine, you may use rsync like this:



      rsync -av --include='*/' --include='something*' --exclude='*' 
      --prune-empty-dirs
      username@11.22.333.44:/home/ubuntu/user/someFolder/ ./target


      This would find and transfer all files matching the pattern and the directory structure that they live in to the local machine under the path ./target.



      The --include and --exclude patterns are applied from left to right and the first match is what matters:




      • --include='*/': Look into all subdirectories (empty directories, i.e. directories with no matching filenames, are not transferred due to --prune-empty-dirs).


      • --include='something*': The pattern that would match the things we're actually interested in.


      • --exclude='*': Ignore everything else.





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        From one of your comments, it sounds as if what you're intending to do is to create a list of pathnames of files that you'd like to transfer from the remote system to the local machine using scp. The issue with this is that you passing pathnames back and forth between the two systems which introduces the risk that filenames that includes whitespace characters may be mangled.



        If you're looking for a way to transfer all the files that matches something* from somewhere under /home/ubuntu/user/someFolder on the remote machine, you may use rsync like this:



        rsync -av --include='*/' --include='something*' --exclude='*' 
        --prune-empty-dirs
        username@11.22.333.44:/home/ubuntu/user/someFolder/ ./target


        This would find and transfer all files matching the pattern and the directory structure that they live in to the local machine under the path ./target.



        The --include and --exclude patterns are applied from left to right and the first match is what matters:




        • --include='*/': Look into all subdirectories (empty directories, i.e. directories with no matching filenames, are not transferred due to --prune-empty-dirs).


        • --include='something*': The pattern that would match the things we're actually interested in.


        • --exclude='*': Ignore everything else.





        share|improve this answer















        From one of your comments, it sounds as if what you're intending to do is to create a list of pathnames of files that you'd like to transfer from the remote system to the local machine using scp. The issue with this is that you passing pathnames back and forth between the two systems which introduces the risk that filenames that includes whitespace characters may be mangled.



        If you're looking for a way to transfer all the files that matches something* from somewhere under /home/ubuntu/user/someFolder on the remote machine, you may use rsync like this:



        rsync -av --include='*/' --include='something*' --exclude='*' 
        --prune-empty-dirs
        username@11.22.333.44:/home/ubuntu/user/someFolder/ ./target


        This would find and transfer all files matching the pattern and the directory structure that they live in to the local machine under the path ./target.



        The --include and --exclude patterns are applied from left to right and the first match is what matters:




        • --include='*/': Look into all subdirectories (empty directories, i.e. directories with no matching filenames, are not transferred due to --prune-empty-dirs).


        • --include='something*': The pattern that would match the things we're actually interested in.


        • --exclude='*': Ignore everything else.






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 6 at 9:49


























        answered Jul 3 at 15:58









        Kusalananda

        101k13199312




        101k13199312






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453236%2fneed-to-access-array-after-exit-from-ssh-in-bash-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?