gpt4 book ai didi

linux - 是否有等同于 $SHLVL 的 Windows?

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:37 26 4
gpt4 key购买 nike

我最近运行了一个在其中运行“cmd”的第 3 方脚本,从而增加了我的 cmd 窗口的嵌套深度(使我的历史记录和 DOSKey 宏在此过程中不可用)。所以我想知道是否有等效的 $SHLVL 或其他方法来确定这种情况?我想我可以向上箭头查看我的历史记录是否仍然存在,但有一天我可能需要脚本中的这个。

当我查看“set”的输出时,似乎没有任何不同。谢谢!

最佳答案

编辑更改代码以列出 PID。

编辑 2 更改代码以将级别输出为 %errorlevel%

参见 https://learn.microsoft.com/en-us/windows/console/getconsoleprocesslist

CountConsoleProcess.vb

imports System.Runtime.InteropServices 
Public Module MyApplication

Public Declare Function GetConsoleProcessList Lib "Kernel32" (ByRef ProcessList as Integer, ByVal Count as Integer) As Integer

Public Sub Main ()
Dim ProcessList(0 To 9) As Integer
Dim Count As Integer
Dim Ret As Integer
Dim x as Integer
Dim ColItems as Object
Dim objWMIService As Object
objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Count = 10
'subtract one to account for this program
Ret = GetConsoleProcessList(ProcessList(0), 10) - 1
Console.Writeline("Level = " & Ret)

For x = Ret to 1 step -1
colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & ProcessList(x))
For Each objItem in colItems
Console.writeline("PID : " & objItem.ProcessID & " Command line : " & objItem.CommandLine)
Next
Next
Environment.ExitCode = Ret
End Sub
End Module

CountConsoleProcess.bat

C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\CountConsoleProcess.vb" /debug:full /out:"%~dp0\CountConsoleProcess.exe" /target:exe 
pause

ConsoleTest.Bat

REM Showing error level
"C:\Users\User\Desktop\ConsoleTest.bat"
Echo %errorlevel%
pause

并使用(在执行 cmd/k 之后,它们是两个正在运行的 CMD)。第一个是当前的 PID。

Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Windows\system32>cmd /k echo hello world
hello world

C:\Windows\system32>"C:\Users\User\Desktop\ConsoleTest.bat"

C:\Windows\system32>"C:\Users\User\Desktop\Console.exe"
Level = 2
PID : 6848 Command line : "C:\Windows\System32\cmd.exe"
PID : 7824 Command line : cmd /k echo hello world

C:\Windows\system32>Echo 2
2

C:\Windows\system32>

并将当前文件夹添加到路径

_AddThisFolderToPath.bat

Setx path "%path%;%~dp0"
Pause

关于linux - 是否有等同于 $SHLVL 的 Windows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203092/

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