我有以下带有嵌入式Expect脚本的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运行脚本,看起来文件名变量就永远不会得到完整的文件名扩展,只有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
您实际上从未为该变量分配特定的文件名。您正在做的是将变量设置为全局模式。然后,将变量传递给时echo
,将扩展全局模式,因此您看到的文件名已被整理。但是,该变量从未设置为特定的文件名。
因此,您需要一种更好的获取文件名的方法。就像是:
#!/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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句