![Bash 내의 예상 스크립트에 대한 파일 이름 확장](https://rvso.com/image/76421/Bash%20%EB%82%B4%EC%9D%98%20%EC%98%88%EC%83%81%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%20%EB%8C%80%ED%95%9C%20%ED%8C%8C%EC%9D%BC%20%EC%9D%B4%EB%A6%84%20%ED%99%95%EC%9E%A5.png)
예상 스크립트가 포함된 다음 bash 스크립트가 있습니다.
#!/bin/bash
if [ ! $# == 2 ]
then
echo "Usage: $0 os_base_version sn_version"
exit
fi
if [ -e /some/file/path/$10AS_26x86_64_$2_*.bin ]
then
filename="/some/file/path/$10AS_26x86_64_$2_*.bin"
echo ${filename}
else
echo "install archive does not exist."
exit
fi
{
/usr/bin/expect << EOD
set timeout 20
spawn "${filename}"
expect {
"Press Enter to view the End User License Agreement" {
send "\r"
exp_continue
}
"More" {
send " "
exp_continue
}
"Do you accept the End User License Agreement?" {
send "y\r"
}
}
interact
expect eof
EOD
}
폴더에는 다음과 같은 형식의 여러 파일이 있습니다.{x}0AS_26x86_64_{x.x.x}_{rev}.bin
스크립트를 실행하면 첫 번째 에코에서 올바른 파일 이름을 얻습니다. 그러나 이것을 사용하여 예상 스크립트에 전달하려고 하면 ${filename}
파일 이름 확장이 사라집니다.
샘플 출력:
# ./make.sh 2 1.2.3
/some/file/path/20AS_26x86_64_1.2.3_45678.bin
spawn /some/file/path/20AS_26x86_64_1.2.3_*.bin
couldn't execute "/some/file/path/20AS_26x86_64_1.2.3_*.bin": no such file or directory
while executing
"spawn /some/file/path/20AS_26x86_64_1.2.3_*.bin"
보시다시피 $filename
에코에서는 올바르게 표시되지만 예상 부분 내부에서는 표시되지 않습니다.
편집하다:
-x를 사용하여 스크립트를 실행하면 filename 변수가 전체 파일 이름 확장을 얻지 못하고 echo만 수행하는 것처럼 보입니다.
# ./make.sh 2 1.2.3
+ '[' '!' 2 == 2 ']'
+ '[' -e /some/file/path/20AS_26x86_64_1.2.3_45678.bin ']'
+ filename='/some/file/path/20AS_26x86_64_1.2.3_*.bin'
+ echo /some/file/path/20AS_26x86_64_1.2.3_45678.bin
/some/file/path/20AS_26x86_64_1.2.3_45678.bin
+ exit
답변1
실제로는 특정 파일 이름을 변수에 할당하지 않습니다. 당신이하고있는 일은 변수를 glob 패턴으로 설정하는 것입니다. 그런 다음 변수를 에 전달하면 echo
glob 패턴이 확장되어 파일 이름이 인쇄된 것을 볼 수 있습니다. 그러나 변수가 특정 파일 이름으로 설정된 적이 없습니다.
따라서 파일 이름을 얻는 더 나은 방법이 필요합니다. 다음과 같은 것 :
#!/bin/bash
## Make globs that don't match anything expand to a null
## string instead of the glob itself
shopt -s nullglob
## Save the list of files in an array
files=( /some/file/path/$10AS_26x86_64_$2_*.bin )
## If the array is empty
if [ -z $files ]
then
echo "install archive does not exist."
exit
## If at least one file was found
else
## Take the first file
filename="${files[0]}"
echo "$filename"
fi
{
/usr/bin/expect << EOD
set timeout 20
spawn "${filename}"
expect {
"Press Enter to view the End User License Agreement" {
send "\r"
exp_continue
}
"More" {
send " "
exp_continue
}
"Do you accept the End User License Agreement?" {
send "y\r"
}
}
interact
expect eof
EOD
}