jgr*_*208 2 linux expect cat newlines output
我在expect
脚本中有以下内容
spawn cat version
expect -re 5.*.*
set VERSION $expect_out(0,string)
spawn rpm --addsign dist/foo-$VERSION-1.i686.rpm
Run Code Online (Sandbox Code Playgroud)
该cat
命令正在正确获取版本,但它似乎添加了一个新行。因为我希望输出如下:
dist/foo-5.x.x-1.i686.rpm
但是我在开头包含以下错误:
cannot access file dist/foo-5.x.x
-1.i686.rpm
Run Code Online (Sandbox Code Playgroud)
为什么expect
在cat
命令输出中添加一个新行,有什么方法可以不这样做或修复 cat 命令的输出?
您很可能在version
文件末尾有一个换行符,因此expect
添加换行符并不是因为它盲目地按照您的要求执行。恕我直言,在生成rpm
命令之前调整期望脚本以去除任何换行符会更容易,并且如果不同的编辑将终止换行符放回去,即使在您删除它之后,它也可以确保您不会再次遇到此问题.
即使version
文件中没有终止换行符,我仍然建议您添加一个strip()
调用(expect
当然,调整到正确的语法)来处理这种情况。
TCL 可以直接读取文件,没有以下复杂性spawn cat
:
#!/usr/bin/env expect
# open a (read) filehandle to the "version" file... (will blow up if the file
# is not found)
set fh [open version]
# and this call handily discards the newline for us, and since we only need
# a single line, the first line, we're done.
set VERSION [gets $fh]
# sanity check value read before blindly using it...
if {![regexp {^5\.[0-9]+\.[0-9]+$} $VERSION]} {
error "version does not match 5.x.y"
}
puts "spawn rpm --addsign dist/foo-$VERSION-1.i686.rpm"
Run Code Online (Sandbox Code Playgroud)