
Ich habe eine Datei Files.txt
mit folgendem Inhalt:
TestApp/Resources/Supporting Files/main.m
TestApp/Resources/Supporting Files/AppDelegate.h
TestApp/Resources/Supporting Files/AppDelegate.m
Ich ziehe die Datei- und Verzeichnisnamen wie folgt und übergebe sie an einen anderen Prozess.
files=$(cat Files.txt)
for item in $files ; do
dn=$(dirname $item)
printf $item
printf "\n"
printf $dn
printf "\n\n"
# passing to another process
done
Allerdings bekomme ich dadurch Folgendes:
TestApp/Resources/Supporting
TestApp/Resources
Files/main.m
Files
TestApp/Resources/Supporting
TestApp/Resources
Files/AppDelegate.h
Files
TestApp/Resources/Supporting
TestApp/Resources
Files/AppDelegate.m
Files
Was ich brauche ist Folgendes:
TestApp/Resources/Supporting Files/main.m
TestApp/Resources/Supporting Files
TestApp/Resources/Supporting Files/AppDelegate.h
TestApp/Resources/Supporting Files
TestApp/Resources/Supporting Files/AppDelegate.m
TestApp/Resources/Supporting Files
Ich habe versucht, ein Leerzeichen wie folgt vor \
„in Files.txt
“ zu setzen:
TestApp/Resources/Supporting\ Files/main.m
und mit %20
als:
TestApp/Resources/Supporting%20Files/main.m
ohne Erfolg!
Antwort1
for
Schleifen durchlaufenWörterkeine Linien- zitieren Sie immer Ihr
"$variables"
(es sei denn, Sie wissen genau, wann Sie es nicht tun sollen)
while read -r item ; do
dn=$(dirname "$item")
printf "%s\n" "$item"
printf "%s\n" "$dn"
# pass "$item" and "$dn" to another process
done < Files.txt
Antwort2
Sie müssen den Feldtrenner festlegen:
OIFS=$IFS
IFS=$'\n'
files=$(cat Files.txt)
for item in $files ; do
dn=$(dirname $item)
printf $item
printf "\n"
printf $dn
printf "\n\n"
# passing to another process
done
IFS=$OIFS
Ausgabe:
[me@localhost test]$ ./test.sh
TestApp/Resources/Supporting Files/main.m
TestApp/Resources/Supporting Files
TestApp/Resources/Supporting Files/AppDelegate.h
TestApp/Resources/Supporting Files
TestApp/Resources/Supporting Files/AppDelegate.m
TestApp/Resources/Supporting Files
Erläuterung: http://en.wikipedia.org/wiki/Interner_Feldtrenner
Die $IFS
Variable definiert, wie die Eingabe in Token aufgeteilt wird. Die Standardeinstellung ist Leerzeichen, Tabulator und Zeilenumbruch. Da Sie nur bei Zeilenumbrüchen aufteilen möchten, $IFS
muss die Variable vorübergehend geändert werden.