gpt4 book ai didi

windows - 将shell函数移植到cmd.exe : Is it possible to automatically source scripts on startup?

转载 作者:IT王子 更新时间:2023-10-29 00:54:31 26 4
gpt4 key购买 nike

我正在移植一个 Linux 工具集,它经常使用 shell 函数来提供某些功能。当您启动一个新的 shell 并包括诸如更改工作目录之类的内容时,这些功能会自动获取,这对于独立程序来说几乎是不可能的,因为子进程无法更改其父进程的环境。

例如,有一个函数 cdbm 可以将工作目录更改为之前加入书签的目录。现在我想在 Windows 上做同样的事情,但我受困于 cmd.exe。据我所知,脚本可以移植到 jscript、vbscript 或普通批处理,这应该不是问题。但是我如何确保它们在启动时自动获取源代码并存在于 shell 环境中?

最佳答案

根据help cmd:

If /D was NOT specified on the command line, then when CMD.EXE starts, itlooks for the following REG_SZ/REG_EXPAND_SZ registry variables, and ifeither or both are present, they are executed first.    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun        and/or    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

作为测试,在 regedit 中,我在上面显示的 HLM 分支中创建了一个名为“AutoRun”的新 key ,其字符串值为“echo Hi”。当我启动一个新的 cmd 实例时,我得到:

Microsoft Windows [Version 6.0.6000]Copyright (c) 2006 Microsoft Corporation.  All rights reserved.HiC:\Users\Username>

您可以输入要运行的脚本的名称(我会输入一个完全指定的脚本路径或一个带有环境变量的路径,例如“%HOMEPATH%\scripts\scriptname”(包括引号如果名称中有空格)。

编辑:注册表项有一些副作用。 help 就是一个例子。如果我有上面的 echo 命令,例如,在自动运行中,当我键入 help vol 时,我会在帮助文本的正上方看到一个“嗨”。执行 vol/?,但不会那样做。

关于windows - 将shell函数移植到cmd.exe : Is it possible to automatically source scripts on startup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1434550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com