[linux-l] bash read interactive

Volker Grabsch vog at notjusthosting.com
Mi Okt 31 11:24:09 CET 2007


On Wed, Oct 31, 2007 at 10:58:47AM +0100, Ivan F. Villanueva B. wrote:
> Hallo,
> warum funktioniert folgendes auf der Konsole nicht?
> 
>     [ivan at samsung]% bash -c "for i in * ; do read -e NAME ; mv $i $NAME ; done" 
>     a
>     mv: missing destination file operand after `a.txt'
>     Try `mv --help' for more information.

Wozu soll das gut sein? Wieso schreibst du nicht direkt:

    for i in * ; do read -e NAME ; mv $i $NAME ; done

Welchen Sinn hat es, das ganze nochmal durch "bash -c" zu jagen?

Außerdem hast du falsch gequotet. Wenn du mit "..." quotest, dann
werden die Variablen schon *vorher* ersetzt. Deine Variable "$i"
war vermutlich schon vor dem Aufruf auf 'a.txt' gesetzt, und
die Variable $NAME hatte noch keinen Wert.

So war dein Aufruf:

    bash -c "for i in * ; do read -e NAME ; mv $i $NAME ; done"

äquivalent zu:

    bash -c "for i in * ; do read -e NAME ; mv a.txt  ; done"

was dann besagte Fehlermeldung erzeugt. Du solltest stattdessen
mit '...' quoten:

    bash -c 'for i in * ; do read -e NAME ; mv $i $NAME ; done'

Außerdem rate ich dir dringend, $i und $NAME ordentlich zu quoten,
sowohl dein Shell-Script als auch der "bash -c"-Befehl werden
große Probleme bei Dateinamen mit Leerzeichen haben. Besser wäre
daher:

    bash -c 'for i in * ; do read -e NAME ; mv "$i" "$NAME" ; done'

Oder noch einfacher:

    for i in * ; do read -e NAME ; mv "$i" "$NAME" ; done

> Ich wollte schnell viele Dateien unbenennen, aber es geht leider bei mir nicht
> so schnell :-(

Alles, was man "schnell hintippt" ist zunächst buggy. Egal, ob
ein C-Programm oder ein Shell-Script. ;-)


Gruß,

    Volker

-- 
Volker Grabsch
---<<(())>>---
Administrator
NotJustHosting GbR



Mehr Informationen über die Mailingliste linux-l