在Hamilton C Shell中保存和还原局部变量

问题描述

我尝试将所有变量状态保存在文件中的某个点,并在需要时恢复所有已保存的变量。我看不到如何实现--- - hosts: localhost tasks: - azure_rm_securitygroup: resource_group: rg-cs-ansible name: nsg-cs-web rules: - name: 'allow_rdp' protocol: Tcp destination_port_range: 3389 access: Allow priority: 1001 direction: Inbound - name: 'allow_web_traffic' protocol: Tcp destination_port_range: - 80 - 443 access: Allow priority: 1002 direction: Inbound - name: 'allow_powershell_remoting' protocol: Tcp destination_port_range: - 5985 - 5986 并保持变量类型,也许有人已经做到了?

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 ,因此不必保留变量类型

解决方法

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

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

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