【问题标题】:How to get unix style prompt in windows (batch)如何在 Windows 中获取 unix 样式提示(批处理)
【发布时间】:2020-05-31 09:10:03
【问题描述】:

我正在尝试制作显示 unix/linux 提示符的 dos shell。代码是:

@echo off
:hi
set tmpdrv=%cd:~0,2%

if %homedrive% == %tmpdrv% (
  set drvmatch=1
) else (
  set drvmatch=0
)
set "MYSTRING=abcdef!%%^^()^!"
set MYSTRING=%homepath%
:strlen
(echo "%MYSTRING%" & echo.) | findstr /O . | more +1 | (set /P RESULT= & call exit /B %%RESULT%%)
set /A STRLENGTH=%ERRORLEVEL%-5
set tmppath1=%cd:~2%
CALL set tmppath2=%%tmppath1:~0,%STRLENGTH%%%
if %homepath% == %tmppath2% (
  set homematch=1
) else (
  set homematch=0
)

set homepathmatch=0
if %homepath% == %tmppath2% (
    if %homedrive% == %tmpdrv% (
    set homepathmatch=1
    )
)

if %homepathmatch%==1 (
    REM set /a STRLENGTH=%STRLENGTH%+2
        CALL set newpath=%%tmppath1:~%STRLENGTH%%%
    set newpath1=~%newpath%
)
set newpath2=%newpath1:\=/%
:check_Permissions

 net session >nul 2>&1
 if %errorLevel% == 0 (
 set "username2=root"
 ) else (
 set username2=%username%
 )
set /p %command%="%username2%@%computername%:%newpath2%$ "
%command%
goto :hi

我希望它像在 unix/linux 中一样显示目录名称,但它显示~。我的代码有什么问题?你能帮我改进一下吗?

【问题讨论】:

  • PROMPT 命令用于进行该设置。我没有看到你在代码中使用它。
  • 另外,在设置此变量时:set /p %command%="%username2%@%computername%:%newpath2%$ " 您尝试扩展 %command%,但尚未定义命令 - 所以您实际上尝试将字符串分配给空变量 - 这总是会失败

标签: linux shell batch-file unix


【解决方案1】:

prompt 的具体情境子串修改可能不太对,但是设置Prompt 的方法是。

@ECHO OFF & Setlocal EnableDelayedExpansion & TITLE %~n0
cls & MODE 1000 & CD %userprofile%\desktop


    For /F "tokens=* Delims=\" %%A In ("%userprofile%") Do (
        Set "C_Prompt=%%A"
        Set "C_Prompt=!C_Prompt:\=!"
        Set "C_Prompt=!C_Prompt:Users=!"
    For %%A in (a,b,c,d,e,f,g,h,i) Do (Set "C_Prompt=!C_Prompt:%%A:=!")
    )
    Set DirPath=:!CD:\=/!
    For %%A in (a,b,c,d,e,f,g,h,i) Do (Set "DirPath=!DirPath:%%A:=!")
    Set "C_Prompt=!C_Prompt!@!computername!!DirPath! $$ "

prompt !C_Prompt!

Start /B cmd.exe

以上内容不会使用工作目录刷新命令提示符,但是会导致命令按预期运行而不会出现扩展问题。

This linked Macro will refresh the Prompt when the working Directory changes, however the act of expanding commands within a user defined variable can result in unexpected results.

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 2015-12-11
    • 1970-01-01
    • 2011-04-19
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2013-05-13
    相关资源
    最近更新 更多