![다음 스크립트(5,7행)에 대해 자세히 설명해주세요. [닫기]](https://linux55.com/image/59154/%EB%8B%A4%EC%9D%8C%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8(5%2C7%ED%96%89)%EC%97%90%20%EB%8C%80%ED%95%B4%20%EC%9E%90%EC%84%B8%ED%9E%88%20%EC%84%A4%EB%AA%85%ED%95%B4%EC%A3%BC%EC%84%B8%EC%9A%94.%20%5B%EB%8B%AB%EA%B8%B0%5D.png)
#!/bin/bash
# Declare array
declare -a ARRAY
# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1
let count=0
while read LINE; do
ARRAY[$count]=$LINE
((count++))
done
echo Number of elements: ${#ARRAY[@]}
# echo array's content
echo ${ARRAY[@]}
# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
5행 ( exec 10<&0
)과 7행 ( ) exec < $1
은 무엇을 합니까 ?
답변1
4행과 6행의 주석이 모든 것을 말해줍니다. 파일 디스크립터 10에는 표준 입력(임의)이 할당되고, 첫 번째 인수로 이름이 지정된 파일의 내용이 stdin에 할당됩니다.
파일 끝에서 이 작업이 취소됩니다.
스크립트 개발자는 이로 인해 다음 양식이 호출되지 않을 것이라고 생각할 수 있습니다.
echo -e '5\n6' | yourscript
(인수 없이) 스크립트가 표준 입력에서 읽는 것을 방지하지만 그렇지 않습니다(즉, 출력은 다음과 같습니다).
yourscript: line 7: $1: ambiguous redirect
Number of elements: 2
5 6