Need to access array after exit from SSH in bash script
Clash 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
bash shell-script ssh find
add a comment |Â
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
bash shell-script ssh find
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 withfind
directly.
â Kusalananda
Jul 3 at 14:46
add a comment |Â
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
bash shell-script ssh find
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
bash shell-script ssh find
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 withfind
directly.
â Kusalananda
Jul 3 at 14:46
add a comment |Â
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 withfind
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
add a comment |Â
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.
1
I think ssh runs a shell anyway, so you don't need thesh -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. Includingsh -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 dofind /home/ubuntu/user/someFolder
, or you can dosh -c 'cd ..; find . ...'
â William Pursell
Jul 3 at 14:48
@WilliamPursell Worked! Really appreciated alot.
â Shubham Kumar Rohit
Jul 3 at 14:52
 |Â
show 2 more comments
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.
add a comment |Â
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.
1
I think ssh runs a shell anyway, so you don't need thesh -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. Includingsh -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 dofind /home/ubuntu/user/someFolder
, or you can dosh -c 'cd ..; find . ...'
â William Pursell
Jul 3 at 14:48
@WilliamPursell Worked! Really appreciated alot.
â Shubham Kumar Rohit
Jul 3 at 14:52
 |Â
show 2 more comments
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.
1
I think ssh runs a shell anyway, so you don't need thesh -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. Includingsh -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 dofind /home/ubuntu/user/someFolder
, or you can dosh -c 'cd ..; find . ...'
â William Pursell
Jul 3 at 14:48
@WilliamPursell Worked! Really appreciated alot.
â Shubham Kumar Rohit
Jul 3 at 14:52
 |Â
show 2 more comments
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.
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.
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 thesh -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. Includingsh -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 dofind /home/ubuntu/user/someFolder
, or you can dosh -c 'cd ..; find . ...'
â William Pursell
Jul 3 at 14:48
@WilliamPursell Worked! Really appreciated alot.
â Shubham Kumar Rohit
Jul 3 at 14:52
 |Â
show 2 more comments
1
I think ssh runs a shell anyway, so you don't need thesh -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. Includingsh -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 dofind /home/ubuntu/user/someFolder
, or you can dosh -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
 |Â
show 2 more comments
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Jul 6 at 9:49
answered Jul 3 at 15:58
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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