Èåìá bash iteration

Giorgos Keramidas keramida at ceid.upatras.gr
Wed Oct 18 02:54:06 EEST 2006


On 2006-10-18 02:21, basOS G <noxelia at gmail.com> wrote:
> Ãåéá êáé ÷áñá.
> Å÷ù êáôé áðïñéåò ãéá ôéò äõíáôïôçôåò ôïõ âash.
>
> Èåëù íá scanarw oëá ôá áñ÷åéá êáé ôïõò öáêåëïõò åíïò êáôáëüãïõ êáé
> áíáëïãá ìå êÜðïéåò óõíèçêåò íå åêôåëÝóù áëëáãÝò óôá ïíüìáôá ôïõò. Ôï
> ðñüâëçìá åßíáé ðùò èá ìðïñåé ôï myprog.sh íá scanarei ïëïõò ôïõò
> öáêåëïõò.

Ìå êüðï.

> 1ç ðñïóåããéóç)
> Äïêéìáóá êáé åöôéáîá ìéá ëéóôá ôùí õðï åîÝôáóç áñ÷åßùí ìå
>
>     find /targetroot/ > filelist.txt
>
> Åðåîåñãáóôçêá ôï filelist.txt êáé åâáëá êáèå ãñáììç áíÜìåóá óå "  ð.÷.
> "/targetroot/file 1.txt"
> "/targetroot/file 2.txt"
>
> Óôç óõííå÷åéá ðåñáóá óáí ïñéóìá óôï myprog.sh "`cat filelist.txt`"
> káé äïêéìáóá
> for var in $1 ;do .... .
> Äå äïõëåøå. Óáí delimiter èåùñåé êáé ôï êåíï êáé ôï \n oðüôå áí ôá
> ïíïìáôá ôùí áñ÷åéùí å÷ïõí êåíá õðáñ÷åé ðñüâëçìá. Äå îåñù áí ìðïñåé íá
> äïõëÝøåé ìå áõôïí ôïí ôñüðï.

Ìçí ôï êÜíåéò Ýôóé, ãéáôß áöÞíåéò ôï shell íá êÜíåé expansion óôï $1, ìå
ôá áðïôåëÝóìáôá ðïõ åßäåò.  Ìéá ëýóç åßíáé íá äéáâÜæåéò ìå bash ìßá-ìßá ôéò
ãñáììÝò ôïõ áñ÷åßïõ `filelist.txt':

    $ cat -n files.txt
         1  foo bar
         2  foo bar
         3  foo foo
    $ while read line ; do \
          if test -f "${line}" ; then \
              echo "\`${line}' file exists" ; \
          else \
              echo "\`${line}' file not found" ; \
          fi ; \
      done < files.txt
    `foo bar' file not found
    `foo baz' file not found
    `foo foo' file not found
    $

Ðñüóåîå ðùò áíáôáêåõèýíïíôáò ôçí åßóïäï ÏËÏÕ ôïõ while loop, ôï `read'
åðéóôñÝöåé ìßá-ìßá ôéò ãñáììÝò ôïõ `files.txt'.

> Äçëáäç áí õðÜñ÷åé ôñüðïò íá äéáâÜæåé ôï script ôá ðåñéå÷üìåíá åíïò
> áñ÷åßïõ êåéìÝíïõ ãñáììÞ ðñïò ãñáììÞ ÷ùñßò íá ÷ùñßæåé ôéò ëÝîåéò ôçò
> ãñáììÞò.  ð.÷. ï÷é "/targetroot/file" "1.txt" "/targetroot/file"
> "2.txt" áëëá "/targetroot/file 1.txt" "/targetroot/file 2.txt"

Äåò ðáñáðÜíù.

> 2ç ðñïóåããéóç)
> Áíáñùôéåìáé áí õðáñ÷åé ôñüðïò ìå êÜðïéá åíôïëç íá scanarei ôï
> myprog.sh ìåóá óôï targetroot (ôï ïðïßï ðéèáíùí èá äÝ÷åôáé ùò üñéóìá)
> êáé íá åðåîåñãÜæåôáé ôï êáèå áñ÷åéï.

Íáé.  ØÜîå óôá archives áõôÞò ôçò ëßóôáò[1], óôá archives ôçò
freebsd-questions ëßóôáò[2], êáé óôï documentation ôçò äéáíïìÞò óïõ, ãéá
ôéò åíôïëÝò find(1) êáé xargs(1).

Õóôåñá ðñïóðÜèçóå íá êáôáëÜâåéò ãéáôß ôï ðáñáêÜôù ðáñÜäåéãìá ëåéôïõñãåß
üðùò öáßíåôáé óå áõôü ôï email:

    giorgos at gothmog:/tmp$ mkdir 1
    giorgos at gothmog:/tmp$ cd 1
    giorgos at gothmog:/tmp/1$ touch 'foo bar' 'foo baz' 'foo foo'
    giorgos at gothmog:/tmp/1$ find .
    .
    ./foo bar
    ./foo baz
    ./foo foo
    giorgos at gothmog:/tmp/1$ find . -type f
    ./foo bar
    ./foo baz
    ./foo foo
    giorgos at gothmog:/tmp/1$ find . -type f -print | xargs ls -ld
    ls: ./foo: No such file or directory
    ls: ./foo: No such file or directory
    ls: ./foo: No such file or directory
    ls: bar: No such file or directory
    ls: baz: No such file or directory
    ls: foo: No such file or directory
    giorgos at gothmog:/tmp/1$ find . -type f -print0 | xargs -0 ls -ld
    -rw-rw-r--  1 giorgos  wheel  0 Oct 18 02:50 ./foo bar
    -rw-rw-r--  1 giorgos  wheel  0 Oct 18 02:50 ./foo baz
    -rw-rw-r--  1 giorgos  wheel  0 Oct 18 02:50 ./foo foo
    giorgos at gothmog:/tmp/1$

Áí êáôáëÜâåéò ãéáôß äåí ðáßæåé óùóôÜ ç ðñïôåëåõôáßá åíôïëÞ (áõôü ôï
îÝñåéò Þäç, ëïãéêÜ) áëëÜ êáé ãéáôß äïõëåýåé óùóôÜ ç ôåëåõôáßá åíôïëÞ,
ôüôå åßóáé óå êáëü äñüìï :)




More information about the Linux-greek-users mailing list