Unix Shell - loop and list with bash

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > loop and list with bash





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author loop and list with bash
Eric Belhomme

2004-10-02, 9:13 pm

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
Stephane CHAZELAS

2004-10-02, 9:13 pm

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
Eric Belhomme

2004-10-02, 9:13 pm

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

Eric Belhomme

2004-10-02, 9:13 pm

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
Stephane CHAZELAS

2004-10-02, 9:13 pm

2004-09-30, 07:37(-04), Bill Marcum:
[...]
[...][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

[...]

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
Paul Jarc

2004-10-03, 7:47 am

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 :


Is "chmod -R" what you're looking for?

> 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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com