Linux

期望腳本中的貓在字元串末尾添加新行

  • June 12, 2017

我在expect腳本中有以下內容

spawn cat version
expect -re 5.*.*
set VERSION $expect_out(0,string)
spawn rpm --addsign dist/foo-$VERSION-1.i686.rpm

cat命令正在正確獲取版本,但它似乎正在添加一個新行。因為我希望輸出如下:

dist/foo-5.x.x-1.i686.rpm

但在開始時包含以下錯誤:

cannot access file dist/foo-5.x.x
-1.i686.rpm

為什麼expect要在命令輸出中添加新行,cat有什麼方法可以不這樣做或修復 cat 命令的輸出?

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"

引用自:https://unix.stackexchange.com/questions/370667