Kirjoittaja Aihe: for ja tiedoston nimi[Ratkaistu]  (Luettu 3153 kertaa)

JJK

  • Käyttäjä
  • Viestejä: 359
  • Ubuntu Mate 22.04
    • Profiili
for ja tiedoston nimi[Ratkaistu]
« : 26.06.08 - klo:21.36 »
Sain pitkän tuhrauksen jälkeen käännettyä ensimmaisen ohjelman lähdekoodista.  ;D

Nyt sitten pitäisi ajaa sillä kaikki kansion tiedostot läpi. Mutta tiedostonimi tuottaa hieman ongelmia.

Eli kansiossa on
Lainaus
$ ls
11.dxf  12.dxf  13.dxf

Sitten aikani komentorivin kanssa puuhattuani olen saanut tällaisen komennon kirjoitettua. Mutta se kaipaa pientä viilausta.
Koodia: [Valitse]
for i in *.dxf; do vec2web "$i" "$i".png; done
Ja tuloksena on
Lainaus
ls
11.dxf  11.dxf.png  12.dxf  12.dxf.png  13.dxf  13.dxf.png

Eli noista png kuvien nimistä pitäis saada noi .dxf:t pois
« Viimeksi muokattu: 26.06.08 - klo:23.05 kirjoittanut JJK »

Petri Järvisalo

  • Käyttäjä
  • Viestejä: 579
    • Profiili
    • Kotisivut
Vs: for ja tiedoston nimi
« Vastaus #1 : 26.06.08 - klo:22.55 »
Koodia: [Valitse]
for i in *.dxf ; do j=`echo $i | sed 's/.dxf//g'`; vec2web "$i" "$j".png; done;
otetaas vaikka sedillä pois .dfx.
Lisää [ratkaistu] ketjun ensimmäisen viestin otsikkoon, kun ongelma on ratkennut.

JJK

  • Käyttäjä
  • Viestejä: 359
  • Ubuntu Mate 22.04
    • Profiili
Vs: for ja tiedoston nimi
« Vastaus #2 : 26.06.08 - klo:23.05 »
Koodia: [Valitse]
for i in *.dxf ; do j=`echo $i | sed 's/.dxf//g'`; vec2web "$i" "$j".png; done;
otetaas vaikka sedillä pois .dfx.

Joo just sain ratkaistua tuonne toiseen topiciin antamasi esimerkin avulla
http://forum.ubuntu-fi.org/index.php?topic=19483.msg144860#msg144860

Nyt toimii.   :)  Kiitti.

mgronber

  • Käyttäjä
  • Viestejä: 1458
    • Profiili
Vs: for ja tiedoston nimi
« Vastaus #3 : 27.06.08 - klo:00.39 »
Koodia: [Valitse]
for i in *.dxf ; do j=`echo $i | sed 's/.dxf//g'`; vec2web "$i" "$j".png; done;
otetaas vaikka sedillä pois .dfx.

Tai vaihtoehtoisesti homma voidaan tehdä hieman lyhyemmin ja yksinkertaisemmin.

Koodia: [Valitse]
$ for f in *.dxf; do vec2web "$f" "${f%.*}.png"; done

JJK

  • Käyttäjä
  • Viestejä: 359
  • Ubuntu Mate 22.04
    • Profiili
Vs: for ja tiedoston nimi
« Vastaus #4 : 27.06.08 - klo:09.08 »
Lainaus
Tai vaihtoehtoisesti homma voidaan tehdä hieman lyhyemmin ja yksinkertaisemmin.

Koodia: [Valitse]
$ for f in *.dxf; do vec2web "$f" "${f%.*}.png"; done


Tarkoittaako tuo "${f%.*}", että muuttujasta f leikataan pisteestä eteenpäin muut pois?
Olisi ihan mielenkiintoista ymmärtää mitä tekee, että tämä ei mee ihan copy pasteksi.

mgronber

  • Käyttäjä
  • Viestejä: 1458
    • Profiili
Vs: for ja tiedoston nimi
« Vastaus #5 : 27.06.08 - klo:11.15 »
Tarkoittaako tuo "${f%.*}", että muuttujasta f leikataan pisteestä eteenpäin muut pois?
Olisi ihan mielenkiintoista ymmärtää mitä tekee, että tämä ei mee ihan copy pasteksi.

Ei vaan muuttujan f lopusta leikataan lyhin mallia ".*" vastaava pätkä pois eli käytännössä leikataan pois viimeisestä pisteestä alkaen loppun. Jos prosenttimerkkejä olisi kaksi niin silloin lopusta poistettaisiin pisin mallia vastaava pätkä eli ensimmäisestä pisteestä loppuun saakka.

Kannattaa lukea bashin manuaalista Parameter Expansion -alaotsikon alainen teksti kokonaan. Alla on lainattuna muutama näppärä ominaisuus jotka ainakin on hyvä tuntea.

Koodia: [Valitse]
$ man bash
[...]
EXPANSION
[...]
   Parameter Expansion
[...]
       ${parameter:offset}
       ${parameter:offset:length}
              Substring Expansion.  Expands to up to length characters of parameter  starting  at
              the  character specified by offset.  If length is omitted, expands to the substring
              of parameter starting at the character specified by offset.  length and offset  are
              arithmetic  expressions (see ARITHMETIC EVALUATION below).  length must evaluate to
              a number greater than or equal to zero.  If offset evaluates to a number less  than
              zero,  the  value  is  used  as  an  offset from the end of the value of parameter.
              Arithmetic expressions starting with a - must be separated by whitespace  from  the
              preceding  : to be distinguished from the Use Default Values expansion.  If parame‐
              ter is @, the result is length  positional  parameters  beginning  at  offset.   If
              parameter  is  an array name indexed by @ or *, the result is the length members of
              the array beginning with ${parameter[offset]}.  A negative offset is taken relative
              to one greater than the maximum index of the specified array.  Note that a negative
              offset must be separated from the colon by at least one space to avoid  being  con‐
              fused  with  the  :-  expansion.  Substring indexing is zero-based unless the posi‐
              tional parameters are used, in which case the indexing starts at 1.
[...]
       ${parameter#word}
       ${parameter##word}
              The  word  is  expanded to produce a pattern just as in pathname expansion.  If the
              pattern matches the beginning of the value of parameter, then  the  result  of  the
              expansion  is  the  expanded  value of parameter with the shortest matching pattern
              (the ‘‘#’’ case) or the longest matching pattern (the  ‘‘##’’  case)  deleted.   If
              parameter  is  @  or *, the pattern removal operation is applied to each positional
              parameter in turn, and the expansion is the resultant list.   If  parameter  is  an
              array variable subscripted with @ or *, the pattern removal operation is applied to
              each member of the array in turn, and the expansion is the resultant list.

       ${parameter%word}
       ${parameter%%word}
              The word is expanded to produce a pattern just as in pathname  expansion.   If  the
              pattern  matches  a  trailing  portion of the expanded value of parameter, then the
              result of the expansion is the expanded value of parameter with the shortest match‐
              ing  pattern  (the  ‘‘%’’  case)  or the longest matching pattern (the ‘‘%%’’ case)
              deleted.  If parameter is @ or *, the pattern removal operation is applied to  each
              positional  parameter in turn, and the expansion is the resultant list.  If parame‐
              ter is an array variable subscripted with @ or *, the pattern removal operation  is
              applied  to  each  member  of the array in turn, and the expansion is the resultant
              list.

       ${parameter/pattern/string}
              The pattern is expanded to produce a pattern just as in pathname expansion.  Param‐
              eter  is  expanded  and  the longest match of pattern against its value is replaced
              with string.  If Ipattern begins with /, all matches of pattern are  replaced  with
              string.   Normally  only the first match is replaced.  If pattern begins with #, it
              must match at the beginning of the expanded value of parameter.  If pattern  begins
              with  %, it must match at the end of the expanded value of parameter.  If string is
              null, matches of pattern are deleted and the / following pattern  may  be  omitted.
              If  parameter  is  @ or *, the substitution operation is applied to each positional
              parameter in turn, and the expansion is the resultant list.   If  parameter  is  an
              array  variable  subscripted  with @ or *, the substitution operation is applied to
              each member of the array in turn, and the expansion is the resultant list.
[...]
« Viimeksi muokattu: 27.06.08 - klo:12.00 kirjoittanut mgronber »