tcl脚本读写配置⽂件
s## tcl读写配置⽂件
#!/usr/tcl/bin/expect
proc getConfig {configFile Key {Comment “#”} {Temp “[”}} {
打开配置⽂件
set Value “0”
set err [catch {set fileid [open KaTeX parse error: Expected 'EOF', got '}' at position 14: configFile r]}errMsg] i…err == 1} { puts “errMsg : $errMsg”
return $Valueerror parse new
}
set End “END”
set sign “1”
set rowid 0
while {[eof $fileid] != 1} {
incr rowid ;# 记录⾏数, 从⼀开始
gets $fileid line
set commentpos [string first $Comment $line]
if { $commentpos == 0 } {
} else {
if { $commentpos != -1 } {
set line [string range $line 0 [expr $commentpos-1]]
}
set line [string trim $line]
if { $line == “” } {
continue
} else {
set Keypos [string first $Key $line] ;# 得到Key的位置
set Endpos [string first $End $line] ;# 得到END的位置
set arr(0) $rowid
set sign “0”
}
if { $Endpos != -1 && $sign == “0”} {
set arr(1) $rowid
}
}
}
}
set Value “KaTeX parse error: Expected group after '_' at position 9: {arr(0)}_{arr(1)}”
close $fileid
return $Value
}
proc getValue {configFile Array {Temp “#”} {Equal “=”}} {
set Temppos [string first “_” $Array]
set BeginPos [string range $Array 0 [expr $Temppos -1]]
set EndPos [string range $Array [expr $Temppos +1] end]
set Value “” ;# 记录过程返回的值
set End “END”
set err [catch {set fileid [open KaTeX parse error: Expected 'EOF', got '}' at position 14: configFile r]} errMsg] i…err == 1} {puts “errMsg : $errMsg”
return $Value
}
set rowid 0
set num 0 ;#记录当前⾏数,程序调试时打印调试信息使⽤的
while {[eof $fileid] != 1} {
incr rowid ;# 记录⾏数, 从⼀开始
gets $fileid line ;# 读出⼀⾏
if { $rowid > $BeginPos && $rowid < KaTeX parse error: Expected 'EOF', got '}' at position 8: EndPos } { set …num) KaTeX parse error: Expected 'EOF', got '}' at position 22: … incr num } } set num [a…x<$num} {incr x} {
#puts x);
append Value x) “#”
}
close $fileid
return $Value
}
proc findPair {configFile {begin “[”} {end “]”}} {
打开配置⽂件
set Value “0”
set err [catch {set fileid [open KaTeX parse error: Expected 'EOF', got '}' at position 14: configFile r]} errMsg] i…err == 1} {puts “errMsg : $errMsg”
return $Value
}
set End “END”
set null “”
set sign “1”
set rowid 0
arr (arr (
while {[eof $fileid] != 1} { ;# 读取⽂件内容
gets $fileid line
set commentpos [string first $configFile $line] ;# 得到注释符号的位置
if { $commentpos == 0 } {
} else {
if { $commentpos != -1 } { ;# ⾏中有注释符号,去掉注释
set line [string range $line 0 [expr $commentpos-1]]
}
set line [string trim $line] ;# 去掉两端的空格
if { $line == “” } {
continue
} else {
set Keypos [string first $begin $line] ;# 得到Key的位置
set Endpos [string first $end $line] ;# 得到END的位
set Value [string range $line [expr $Keypos + 1] [expr $Endpos - 1]]
if { $Value != $End && $Value != KaTeX parse error: Expected 'EOF', got '}' at position 5: null} { set arr(rowid) $Value incr rowid
}
}
}
}
append Value1 KaTeX parse error: Expected 'EOF', got '#' at position 8: rowid "#" set num [arr…x<$num} {incr x} {
append Value1 x) “#”
}
close $fileid
return $Value1
}
proc start {Array {Temp “#”} {Equal “=”}} { ;#这部分内容根据配置⽂件⾃⾏修改
set timeout 120
regexp – {((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}} $Array ip
regexp – {(?=username=).?(?=#)} KaTeX parse error: Expected '}', got 'EOF' at end of input: …exp -- {([^=]+)}
$usernameTemp username
regexp – {(?=passwd=).?(?=#)} KaTeX parse error: Expected '}', got 'EOF' at end of input: …exp -- {([^=]+)} $passwdTemp passwd
regexp – {(?=ck_cxbhlist=).*?(?=#)} KaTeX parse error: Expected '}', got 'EOF' at end of input: …exp -- {([^=]+)} KaTeX parse error: Expected 'EOF', got '#' at position 25: …mp ck_cxbhlist #p uts "passwd"
#puts “KaTeX parse error: Expected 'EOF', got '#' at position 11: username" #p uts "ip”
#puts “$ck_cxbhlist”
spawn ssh ip
expect {
“*yes/no” { send “yes\r” }
“*password:” { send “$passwd\r” }
}
expect "#*"
send "cd /home/webapp && ./test.sh\r"
send "ls\r"
expect eof
}
proc main { {Temp “#”} } { ;#遍历配置⽂件中所有的主对[111]xxx[END]
set val [findPair “test.ini”]
arr (username @
set Endpos [string first $Temp $val]
set num [string range $val 0 [expr $Endpos - 1]] ;#循环因⼦set val [string range $val [expr $Endpos + 1] end]
x<
for {set x 0} {num} {incr x} {
set Endpos [string first $Temp $val]
set Key [string range $val 0 [expr $Endpos - 1]]
set aaa [getConfig “test.ini” $Key]
set bbb [getValue “test.ini” $aaa]
set ccc [start $bbb]
set Endpos [string first $Temp $val]
set val [string range $val [expr $Endpos + 1] end]
}
}
set start [main]
exit
expect eof
下⾯配上配置⽂件内容
[111]
ip=122.
username=webapp
passwd=12345678
ck_cxbhlist=711
sada
sadasaa=
ssssd =sss
scpdest=/home/webapp/test/
scpsrc=test.sh
[END]
[222]
ip=122.
username=webapp
passwd=12345678
ck_cxbhlist=711
sada
sadasaa=
ssssd =sss
scpdest=/home/webapp/
scpsrc=test.sh
[END]
>>####分割线>>>###
tcl⾃学的,可以使⽤,⾃⾏改良,共同进步
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论