Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
shell script problem mit for schleife bei leerzeichen
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German)
View previous topic :: View next topic  
Author Message
SMS-King
n00b
n00b


Joined: 29 Apr 2005
Posts: 6

PostPosted: Fri Apr 29, 2005 2:50 pm    Post subject: shell script problem mit for schleife bei leerzeichen Reply with quote

Da ich meine Festplatte von NTFS auf ext3 umstellen möchte, muss ich die darauf befindlichen Daten auf eine andere Festplatte sichern, da ich in der Vergangenheit bei solchen aktionen immer mal wieder dateien verloren habe möchte ich die daten nach dem kopieren noch mal auf gleichheit prüfen, dazu hab ich ein shell script geschrieben welches mit hilfe von md5sum die checksummen vergleicht.
Hier erst mal was ich schon hab:
Code:

#!/bin/sh

cd $1
for file in * ; do
  if [ -e "$1$file" ] && [ -e "$2$file" ]; then
    if [ "`md5sum $1$file | cut -d ' ' -f 1`" == "`md5sum $2$file | cut -d ' ' -f 1`" ]; then
      echo -e "$1$file\t$2$file\t[ \033[01;32mok \033[01;37m]\033[01;32m :-) Die Files sind gleich\033[01;37m"
    else
      echo -e "$1$file\t$2$file\t[ \033[01;31m!! \033[01;37m]\031[01;32m :-( Die Files sind ungleich\033[01;37m"
    fi
  else
    echo -e "$1$file\t\t\t[ \033[01;31m!! \033[01;37m]\031[01;32m :-( Der File fehlt\033[01;37m"
  fi

done


Das funktioniert auch, aber vergleicht halt nur die dateien des angegebenen verzeichnisses und nicht die der unterverzeichnisse.

wenn ich * in der for schleife mit `find` ersetze geht er zwar in die unterverzeichnisse, bekommt aber probleme mit leerzeichen:

Code:


#for file in * ; do echo $file; done
datei mit leerzeichen
verzeichnis

----------------------------------------

#for file in `find` ; do echo $file; done
.
./verzeichnis
./verzeichnis/datei
./datei
mit
leerzeichen

---------------------------------------

#for file in `\ls --quoting=c -R` ; do echo $file; done
".":
"datei
mit
leerzeichen"
"verzeichnis"
"./verzeichnis":
"datei"



der letzte versuch schlug leider auch fehl, da for auch quoting ignoriert.

Ich hoffe ihr könnt mir helfen.
Back to top
View user's profile Send private message
Lensman
Tux's lil' helper
Tux's lil' helper


Joined: 13 Jun 2004
Posts: 137

PostPosted: Fri Apr 29, 2005 3:36 pm    Post subject: Reply with quote

Spontan würde ich sagen, dass du die Variable IFS auf z.B. : setzt und die Ausgabe von find so modifizierst (mit der -printf option afaik), dass die einzelnen Dateien/Verzeichnisse durch : getrennt werden, also z.B. "datei_a:Verzeichnis mit Leerzeichen:datei b:" usw. IFS enthält die Zeichen, an denen getrennt wird und Standardmäßig ist da halt das Leerzeichen mit drin :-)

[Edit:] So ist es, wie ich das meinte:
Code:
IFS=":";
for i in `find . -type f -printf "%p:" `; do echo "$i"; done;
Back to top
View user's profile Send private message
SMS-King
n00b
n00b


Joined: 29 Apr 2005
Posts: 6

PostPosted: Fri Apr 29, 2005 4:42 pm    Post subject: Reply with quote

Danke für die Hilfe, das hat funktioniert.
Falls noch jemand das script haben will:
Code:

#!/bin/sh

IFS=":"

gesammt=0
gleich=0
ungleich=0
fehlt=0

cd $1
for file in `find . -type f -printf "%p:" ` ; do
        gesammt=`expr $gesammt + 1`
        if [ -e "$1$file" ] && [ -e "$2$file" ]; then
            if [ "`md5sum $1$file | cut -d ' ' -f 1`" == "`md5sum $2$file | cut -d ' ' -f 1`" ]; then
                echo -e "$1$file\t$2$file\t[ \033[01;32mok \033[01;37m]\033[01;32m :-) Die Files sind gleich\033[01;37m"
                gleich=`expr $gleich + 1`
            else
                echo -e "$1$file\t$2$file\t[ \033[01;31m!! \033[01;37m]\033[01;31m :-( Die Files sind ungleich\033[01;37m"
                ungleich=`expr $ungleich + 1`
            fi
        else
                echo -e "$1$file\t\t\t[ \033[01;31m!! \033[01;37m]\033[01;31m :-( Der File fehlt\033[01;37m"
                fehlt=`expr $fehlt + 1`
        fi
done

echo "--------------------------------------------"
echo -e "Files insgesammt: \t$gesammt"
echo -e "Gleiche Files: \t\t$gleich"
echo -e "Ungleiche Files: \t$ungleich"
echo -e "Fehlende Files: \t$fehlt"
echo "--------------------------------------------"


Achtung leider wurden einige zeilen umgebrochen
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum