loop and list with bash
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > loop and list with bash




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    loop and list with bash  
Eric Belhomme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

Hi,

I'm trying to write a little bash (version 2.05a.0 on GNU/Linux Debian
woody) script that list à directory tree to change user/rights permission :


#!/bin/bash

for item in `ls -AlQ /mnt/data/divx | grep -v total`
do
echo $item
done

I would get into the variable $item something like that :
-rwxr-xr-x    1 rico     staff    270193540 sep  4  2003 "a_file.txt"

But in reality each line generated by ls command is splitted into 9 list
element, so I get for each loop iteration :
-rwxr-xr-x
1
rico
staff
...

So what sould I do to get only 1 list item for each line reported by ls
command ?

regards,

--
Rico





[ Post a follow-up to this message ]



    Re: loop and list with bash  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

2004-09-30, 08:55(+00), Eric Belhomme:
[...]
> #!/bin/bash
>
> for item in `ls -AlQ /mnt/data/divx | grep -v total`
> do
>     echo $item
> done
>
> I would get into the variable $item something like that :
> -rwxr-xr-x    1 rico     staff    270193540 sep  4  2003 "a_file.txt"
[...]

IFS=$'\n'
set -f
for item in $(ls -AlQ /mnt/data/divx | tail +2); do
printf '%s\n' "$item"
done

Note that the -Q option doesn't help bash in anyway to handle
filenames with special characters in them.

--
Stephane





[ Post a follow-up to this message ]



    Re: loop and list with bash  
Eric Belhomme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

Stephane CHAZELAS <this.address@is.invalid> wrote in
news:slrnclnni6.23k.stephane.chazelas@spam.is.invalid:

> IFS=$'\n'

damn ! this single sentence resolve my problem ! What's that ? what does it
mean ?

> set -f

and that ?

> Note that the -Q option doesn't help bash in anyway to handle
> filenames with special characters in them.
>
Sure, but it helps me to handle filenames with spaces...

thanks for your help ;)
regards,

--
Rico






[ Post a follow-up to this message ]



    Re: loop and list with bash  
Eric Belhomme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

Stephane CHAZELAS <this.address@is.invalid> wrote in
news:slrnclns6l.23k.stephane.chazelas@spam.is.invalid:

thanks for this very instructive course )

As I wonder you're french, maybe can you suggest a good book for bash
scripting (and sed/awk use) ?
 
> [...]
>
> How?
>
ls -QAl ./ | cut -d "\"" -f 2

And I just get file name with spaces

--
Rico





[ Post a follow-up to this message ]



    Re: loop and list with bash  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

2004-09-30, 07:37(-04), Bill Marcum:
[...][vbcol=seagreen] 
[...][vbcol=seagreen]
> Check Google for "comp.unix.shell Frequently Asked Questions".
>
> ls -AlQ /mnt/data/divx | grep -v total | while read item
> or
> ls -AlQ /mnt/data/divx | grep -v total |\
>  while read permission links owner group size month day year_or_time filename[/vbc
ol]
[...]

With that in mind that any variable assigned inside the while
loop will be lost afterwards.

Also note that the second one doesn't work correctly for
symbolic links.

Also note the "-r" option to read otherwise read does
extra-processing on the read input.

Also note that every command within the loop won't have their
stdin connected to the terminal anymore (or whatever it was
before) but from the pipe.

{ ls -ALlQ /mnt/data/divx | tail +2 | \
while read <&4 -r permission links owner group \
size month day year_or_time filename
do ...
done 4<&0 <&3 3<&-; } 3<&0

(untested)

--
Stephane





[ Post a follow-up to this message ]



    Re: loop and list with bash  
Paul Jarc


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 12:47 PM

Eric Belhomme <eric.belhomme_NOSPAM@free.fr.invalid> wrote:
> I'm trying to write a little bash (version 2.05a.0 on GNU/Linux Debian
> woody) script that list à directory tree to change user/rights permission :[/vbcol
]

Is "chmod -R" what you're looking for?
[vbcol=seagreen]
> So what sould I do to get only 1 list item for each line reported by ls
> command ?

for item in /mnt/data/divx/* /mnt/data/divx/.[!.]* /mnt/data/divx/..?*; 
do
lsdata=`ls -ld "$item"` && ...
done


paul





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:20 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register