sh for + sed or awk or...

here's my problem, that i can't solve on my own

i have some directory "/some/stupid directory"
and i have some files "/some/stupid directory/file 1" and "/some/stupid directory/file 2"

i'm using
Code:
for fileName in `ls "/some/stupid directory/"`; do
  echo "*** file: $1/$fileName"
done
it give wrong output [which is as it should be in this case]:
Code:
*** file: /some/stupid directory/file
*** file: /some/stupid directory/1
*** file: /some/stupid directory/file
*** file: /some/stupid directory/2

if i try
Code:
for fileName in "`ls "/some/stupid directory/""`; do
  echo "*** file: $1/$fileName"
done
i get:
Code:
*** file: /some/stupid directory/file 1
/some/stupid directory/file 2
which is not what i wan, but it correct from point of sh view


I want to add doulb (or single) equote to each line ls outputs, before it's passed to for. I tried awk, but i'm noob in this, and failed many times....

i would like correct output to be:
Code:
*** file: /some/stupid directory/file 1
*** file: /some/stupid directory/file 2


I hope I gave good enough examples, and you did understand what i'm trying to do, if not let me know, i'll try to explain better.

Removing spaces from files with my Win****.sh script can't be used here, because this script will operate on files that i'm downloading/uploading with torrents

Thanks in advance
 
Have you considered using while instead of for?

Code:
for Dir in /some/dir1 /some/dir2 /some/dir3
do 
ls $Dir | while read fileName
do 
echo "*** file: $Dir/$fileName"
done
done
 
Thank you, friend....
this worked...
i preciously never used while, your great peace of code opens grand new perspective for me in sh :D

one little suggestion to you:
better double quote $Dir when you ls

Code:
for Dir in /some/dir1 /some/dir2 /some/dir3
do 
ls [red]"[/red]$Dir[red]"[/red] | while read fileName
do 
echo "*** file: $Dir/$fileName"
done
done


*going to edit many scripts*
 
The double quotes are only for whitespace encapsulation, so I only use them for that purpose.
 
The easy way

An easier way ...

Code:
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ | sed 's/^/*** file: /'
*** file: /usr/home/lbl/mail/
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$
 
Sure, but that will print all sub-directories too, which may not be what OP wants.
 
lbl said:
An easier way ...

Code:
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ | sed 's/^/*** file: /'
*** file: /usr/home/lbl/mail/
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$

I already see where i could use it....
But i still prefer DutchDaemon way


@ DutchDaemon the best part of your code is that it only requires me to modify 1 line in all my currents scripts, that used for :D
 
To remove subdirs ...

To remove the dirs ...

Code:
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ | grep -v '\/$' | awk '{ print "*** file: " $1 }'
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$

or ...

Code:
[lbl@atom0 ~]$ find /usr/home/lbl/mail/ -type f | awk '{ print "*** file: " $1 }'
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$

If you only want to get files from the specifik dir and not the subdirs you can use ...

Code:
[lbl@atom0 ~]$ find /usr/home/lbl/mail -type f -maxdepth 1 | sed "s/^/*** file: /"
*** file: /usr/home/lbl/mail/sent-mail
*** file: /usr/home/lbl/mail/saved-messages
[lbl@atom0 ~]$

/lbl
 
Back
Top