
Files.txt
다음 내용을 포함하는 파일이 있습니다 .
TestApp/Resources/Supporting Files/main.m
TestApp/Resources/Supporting Files/AppDelegate.h
TestApp/Resources/Supporting Files/AppDelegate.m
다음과 같이 파일 및 디렉터리 이름을 가져와 다른 프로세스에 전달하고 있습니다.
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
그러나 이로 인해 다음과 같은 결과가 나왔습니다.
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
나에게 필요한 것은 이것이다:
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
나는 \
다음 Files.txt
과 같이 공백 앞에 in을 붙이려고 했습니다.
TestApp/Resources/Supporting\ Files/main.m
그리고 %20
다음과 같이:
TestApp/Resources/Supporting%20Files/main.m
운이 없군요!
답변1
for
루프가 반복됩니다단어선이 아닌- 항상 당신의 말을 인용하세요
"$variables"
(언제 그렇게 하지 말아야 할지 정확히 알지 않는 한).
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
답변2
필드 구분자를 설정해야 합니다.
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
산출:
[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
설명: http://en.wikipedia.org/wiki/Internal_field_separator
변수 $IFS
는 입력이 토큰으로 분할되는 방식을 정의하며 기본값은 공백, 탭 및 줄 바꿈입니다. 개행에서만 분할하고 싶기 때문에 $IFS
변수를 임시로 변경해야 합니다.