在 Hamilton C 中保存和恢复局部变量 Shell

Save and restore local variables in Hamilton C Shell

我尝试将所有变量状态保存在文件中的某个点,并在需要时恢复所有保存的变量。我不知道如何实现 RestoreState 并保持变量类型,也许有人已经做到了?

# script.csh
local a,b

@ a = 1
@ b = "hello"

proc SaveState ()
    local > backup.txt
endproc # SaveState

proc RestoreState ()
    # If file backup.txt exists
    if (-e backup.txt) then
        echo -- "----- Restore saved state -----"
    end # if
endproc # RestoreState

SaveState
@ b = "world"
RestoreState
# list variables, should print 1 and "hello"
local

编辑: "1" == 1 所以没有必要保留变量类型

我找到了一个解决方案,方法是使用 sed 并以 source 命令可以读取的格式保存 backup.txt 文件:

proc SaveState ()
    local | sed 's/^^\([a-zA-Z0-9_]*\)[\t ]*\(.*\)$/@  = ""/' > backup.txt
endproc # SaveState

proc RestoreState ()
    # If file backup.txt exists
    if (-e backup.txt) then
        source backup.txt
    end # if
endproc # RestoreState