f



"for" loop inside "ssh"

Hello,

I try to use "ssh" command and run "for" loop in remote server.

I mean something like:

ssh remote_server -l remote_user "for file in `ls *.sql`; do; echo $file; done"

Now I get:
file: Undefined variable.


OS: Sun Solaris
Shell: ksh


Thank you.
0
shulamitmi3
3/10/2014 1:01:01 AM
comp.unix.shell 15484 articles. 2 followers. Post Follow

4 Replies
841 Views

Similar Articles

[PageSpeed] 35

On 2014-03-10, shulamitmi3@gmail.com <shulamitmi3@gmail.com> wrote:
> Hello,
>
> I try to use "ssh" command and run "for" loop in remote server.
>
> I mean something like:
>
> ssh remote_server -l remote_user "for file in `ls *.sql`; do; echo $file; done"

Are you not aware that your `ls *.sql` (an antiquated notation representing
$(ls *.sql)) is being expanded locally?

You had better single quote the script.

  ssh remote_server -l remote_user 'for file in `ls *.sql`; do; echo $file; done'

Also, are you allergic to "ssh remote_user@remote_server ..."?

> Now I get:
> file: Undefined variable.

That's because, like `ls *.sql`, the notation $file is also expanded within
double quotes. Your local shell tries to interpolate $file into your command
before ssh is even launched.
0
Kaz
3/10/2014 1:01:01 AM
On 10.03.2014 09:29, shulamitmi3@gmail.com wrote:
> Hello,
> 
> I try to use "ssh" command and run "for" loop in remote server.

That same question had already been topic here two days ago.

> 
> I mean something like:
> 
> ssh remote_server -l remote_user "for file in `ls *.sql`; do; echo $file; done"

BTW; I wonder why people - since decades, and still - prefer to write

   for file in `ls *.sql`

instead of

   for file in *.sql


Janis

> 
> Now I get:
> file: Undefined variable.
> 
> 
> OS: Sun Solaris
> Shell: ksh
> 
> 
> Thank you.
> 

0
Janis
3/10/2014 8:46:02 AM
Kaz Kylheku wrote:

> On 2014-03-10, shulamitmi3@gmail.com <shulamitmi3@gmail.com> wrote:
>> I try to use "ssh" command and run "for" loop in remote server.
>>
>> I mean something like:
>>
>> ssh remote_server -l remote_user "for file in `ls *.sql`; do; echo $file;
>> done"
> 
> Are you not aware that your `ls *.sql` (an antiquated notation
> representing $(ls *.sql)) is being expanded locally?
> 
> You had better single quote the script.
> 
>   ssh remote_server -l remote_user 'for file in `ls *.sql`; do; echo
>   $file; done'

Even better, they had replaced “`ls *.sql`” with “*.sql” unless they 
preferred to see errors if filenames contain one of the characters in 
“$IFS”.

The single quotes are still necessary, though, because “$file” would be 
expanded locally otherwise.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
0
Thomas
3/10/2014 2:45:30 PM
Thomas 'PointedEars' Lahn wrote:

> Kaz Kylheku wrote:
>> On 2014-03-10, shulamitmi3@gmail.com <shulamitmi3@gmail.com> wrote:
>>> I try to use "ssh" command and run "for" loop in remote server.
>>>
>>> I mean something like:
>>>
>>> ssh remote_server -l remote_user "for file in `ls *.sql`; do; echo
>>> $file; done"
>> 
>> Are you not aware that your `ls *.sql` (an antiquated notation
>> representing $(ls *.sql)) is being expanded locally?
>> 
>> You had better single quote the script.
>> 
>>   ssh remote_server -l remote_user 'for file in `ls *.sql`; do; echo
>>   $file; done'
> 
> Even better, they had replaced “`ls *.sql`” with “*.sql” unless they
> preferred to see errors if filenames contain one of the characters in
> “$IFS”.
> 
> The single quotes are still necessary, though, because “$file” would be
> expanded locally otherwise.

That said, this can be simplified to

  ssh remote_user@remote_server 'printf "%s\n" *.sql'
  
or

  ssh remote_user@remote_server 'ls -d *.sql'

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
0
Thomas
3/10/2014 2:51:21 PM
Reply: