differentiate between spaces and new-lines

Hi -

Here's the problem I'm encountering......My script logs in to remote FTP through password-less "sftp" and must get the list of sub-directories under a given path. I know many of the unix commands don't work in "sftp" login so I cannot know which one is a "directory" and which one is a "file". So I thought of copying the output of ls -l command in a file at local unix server and then going thru it to decide which ones are directories.

Having told my end of the story here's what I'm doing?

 
echo "ls -l $remote_ftp_path" > $batch_file
list_of_sub-dir=`sftp -b $batch_file $USER_ID@$IP_ADDRESS`

What happens?
Value of "list_of_sub-dir" variable has all the info about sub-dir, permissions, size, etc in one single line (as expected):

sftp> ls -l /remote/ftp/path drwxr-xr-x 2 user group 4096 Jun 13 15:55 remote_dir_1 -rw-r--r-- 1 user group 5090607 Jun 21 08:38 remote_file_1 -rw-r--r-- 1 user group 6917238 Jun 21 08:37 remote_file_2

and so on....

which I want in below format (after stripping "sftp> ls -l /remote/ftp/path")

 
drwxr-xr-x 2 user group 4096 Jun 13 15:55 remote_dir_1
-rw-r--r-- 1 user group 5090607 Jun 21 08:38 remote_file_1
-rw-r--r-- 1 user group 6917238 Jun 21 08:37 remote_file_2

Then I had a crazy idea of translating (only) spaces (and not new-lines) into commas

echo $list_of_sub-dir | tr " " ","

here's what I get instead

sftp>,ls,-l,/remote/ftp/path,drwxr-xr-x,2,user,group,4096,Jun,13,15:55,remote_dir_1,-rw-r--r--,1,user,group,5090607,Jun,21,08:38,remote_file_1,-rw-r--r--,1,user,group,6917238,Jun,21,08:37,remote_file_2

Can anyone please help me to get this? I really don't know what should I do to somehow preserve "\n" (new-lines) in the variable and not confuse them with "spaces"?!

-dips

From what you've posted, it's just a matter of correct quoting:

$ list_of_subdir="This
> is
> a
> Test"
$ echo $list_of_subdir
This is a Test
$ echo "$list_of_subdir"
This
is
a
Test
$

The background is this: without the quotes, every token separated by one or more whitespaces is given as an argument to echo, which then outputs them separated by exactly 1 space. With the quotes, however, the whole variable is passed as a single argument, including the newlines.

2 Likes

Thanks a ton!! Pludi.

I was being so silly!! :o

-dips