2017-01-11 31 views
0

私はこのスクリプトを使用しています。私はsession.mvwファイルの2行目を置き換えるようにしたいので、入力として「名前」を入力します。 session.mvwファイルは{GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/222.rst"} となりますが、代わって {GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/${name}.rst"}となります。中括弧で囲まれた変数二重引用符tcl

puts "Enter your name: " 
#flush stdout set name [gets stdin] 

    set in [open session.mvw r] 

    # get the path to the parent directory 
    set hostDir [file dirname session.mvw] 
    set tempfile "$hostDir/temp2.txt" 

    # open/create a temp file 
    set out [open $tempfile w] 
    set count 0 

    while { [eof $in] != 1 } { 
     gets $in line 
     #set firstItem [lindex $line 0] incr count 
     # a match has been found... 
     if {$count == 2 } { 
      puts $out {GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/${name}.rst"} 
     } elseif {$count == 3} { 
      puts $out {GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/${name}.rst"} 
      } else { 
      puts $out $line 
      } 
     } 
    close $in 
    close $out 
    close $hostDir 

    # over-write the existing file 
    #file rename -force $tempfile session_file.mvw 
+1

変数置換は、内部ブレースを発生しません印刷し、どちらもこの

puts $out "{GRAPHIC_FILE_1 = \"E:/ge work/hyperview scripting/${name}.rst\"}" 

。それを 'puts $ out 'に変更してください。GRAPHIC_FILE_1 = \" E:/ ge work/hyperview scripting/$ {name} .rst \ "" ' – Dinesh

+0

222.rst "}私の次のコマンドは、次のハイパービューソフトウェアがその行を認識できるように中括弧が必要であることに依存します –

+0

それで、{GRAPHIC_FILE_1 =" E:/ ge work/hyperview scripting/222.rst "} 'これは222を入力として出力します –

答えて

0

Tclのは、それが中括弧内の置換を行いません原則を得ました。それは通常、正確に正しいことです。しかし、この場合、もう少し何かが必要です。

puts $out [subst {GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/${name}.rst"}] 

(注:あなたがsubstでのTclコードを生成している場合、あなたはおそらく間違ったことをやっているわけではありません。例これらの種類については、はそれらSUBST itutionsがないんsubstコマンドがあります...これは、あなたがこのケースでやっていることですが、まだそれはすべての読者への警告だと)

0

あなたが値周りにカッコを持つようにしたい場合は、あなたがこの

puts $out [list "GRAPHIC_FILE_1 = \"E:/ge work/hyperview scripting/${name}.rst\""] 
を行うことができます

または文字列

{GRAPHIC_FILE_1 = "E:/ge work/hyperview scripting/222.rst"}

関連する問題