windows - 定义在 Windows 和 Linux(Windows bash 或 Cygwin)上工作的通用别名?

标签 windows bash batch-file cygwin alias

我在 Windows 10 操作系统中使用“Window Command shell”和“Bash shell”。 真正让我烦恼的是,我必须为每个 shell 定义别名(doskey 和别名),即使我使用相同的命令和文件夹别名。 例如, 别名 np='D:_MyProgram_IDEditor\Notepad++\notepad++.exe' doskey np=D:_MyProgram_IDEditor\Notepad++\notepad++.exe $*

因此,我想知道如何定义在两个 shell 中都适用的通用定义。 目前我的想法是这样...

  1. 定义通用定义文本文件
  2. 制作一个读取“通用定义文件”并定义“命令中的 doskey”和“Bash shell 中的别名”的脚本。

是否有好的方法或相关工作?

示例)

1. define common definition text file (common-define.txt)

"SET"   "DIR_PROGRAM"   "D:\OneDrive\_MyProgram"
"CD" "dev"              "D:\OneDrive\_MyProgram"
"ALIAS" "tt"            "tt.bat"


2. read it and define "doskey in commands", and "alias in shell" (autoload-batch.bat)

FOR /f "tokens=1,2,3* delims= " %%A IN ( common.define.txt
) DO (
if "%%~A"=="###"    echo %%A, %%B, %%C
if "%%~A"=="SET"    set %%~B=%%~C
if "%%~A"=="SETX"   setx %%~B %%~C
if "%%~A"=="CD"     echo @doskey %%~B=cd /d %%~C>>gen_doskey.cmd
if "%%~A"=="ALIAS"  echo @doskey %%~B=%%~C $*>>gen_doskey.cmd
)

reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d %~dp0\gen_doskey.cmd /f

这是 Windows 版本。我正在开发linux版本。 我的想法是这样做,有什么好的方法吗?

最佳答案

native Linux,翻译为 doskey

我相信我已经成功创建了一个批处理 + bash 多语言脚本。 使用 bash shell 保存此内容,以便行仅以 LF 终止,而不是 Windows 风格的 CRLF。看起来 cmd 比 Windows 的 bash 更能接受 Unix 风格的行终止。

: <<'BASH_PORTION'
@goto batch
BASH_PORTION

# aliases
alias hello="echo Hello world!"
alias ls="dir"

: <<'BATCH_PORTION'
:batch
@echo off & setlocal
for /f "tokens=2* delims== " %%I in (
    'findstr "^alias" "%~f0"'
) do doskey %%~I=%%~J
exit /b
BATCH_PORTION

然后要获取脚本源,请在 bash 脚本中执行 source scriptfile.bat 。事实上,这就是当您启动新的 bash session 时 ~/.bashrc 和 ~/.profile 的来源方式。在 cmd shell 中,您只需执行 scriptfile.bat .

仔细放置定界符多行注释会产生奇迹。 bash 解释器看到 : <<'BASH_PORTION'作为多行注释的开头和 BASH_PORTION作为它的结束。我的想法来自this answer 。使用这些 heredoc 注释,可以强制 bash 忽略 cmd 仅批处理行。并且批处理处理以 : 开头的任何行作为标签,所以第一行没有效果。

解决了欺骗每个解释器忽略对方命令的问题后,剩下的就是定义别名了。别名必须在 bash 端使用双引号定义,然后可以使用 for /f 删除引号。在批处理端循环 aliasdoskey 取代.


原生 doskey,针对 Linux 进行了翻译

或者,您可以像平常一样创建一个 doskey 格式的宏文件,例如:

hi=echo Hello $*
bye=echo Goodbye $*

要将宏加载到 cmd 控制台,您只需

doskey /macrofile="path\to\macrofile"

...如果您愿意,可以将其放入自动运行值中的“HKLM\SOFTWARE\Microsoft\Command Processor”中。

要在 Linux 子系统中加载宏,需要进行一些翻译和源操作。像这样的东西会进入你的~/.bashrc:

t=$(mktemp -p /dev/shm)
awk -v RS='\r\n' -F\= '{print $1"(){ "$2"; }"}'</mnt/c/Users/path/to/macros.doskey >$t
source $t
rm -f $t
unset t

不幸的是,我无法使用 eval 找到解决方案定义函数或别名——我猜是因为 eval生成一个新的 shell,该 shell 不会将环境更改传递到调用范围。所以临时文件似乎是必需的。


添加文件路径翻译的示例

使用第二种方法,如果您想将 Windows 文件系统路径转换为 ​​Linux 表示法,可以通过添加几个 gsub 来实现。命令。以下面的 ma​​cros.doskey 为例:

hi=echo Hello $*
bye=echo Goodbye $*
desktop=pushd "c:\Users\username\desktop" && dir && popd

在 Windows 端,您仍然会以相同的方式获取它。

doskey /macrofile="path\to\macros.doskey"

在 Linux 方面,您可以将以下内容放入 .bashrc 或类似文件中,使用 source filename 对其进行评估。 :

alias dir='ls --color=auto'
t=$(mktemp -p /dev/shm)
awk -v RS='\r\n' -F\= '
        {
                gsub(/[A-Za-z]:\\/, "/mnt/&")
                gsub(/\\|:/,"/")
                print $1"(){ "$2"; }"
        }
'</mnt/c/path/to/macros.doskey >$t
source $t
rm -f $t
unset t

这会将所有反斜杠替换为正向,将 c:\替换为/mnt/c/等,并使用经过处理的数据创建函数。需要注意的一件大事是,宏文件中的驱动器盘符需要与/mnt 中出现的大小写相同。另外,我使用 gawk 测试了这些脚本。默认情况下,许多 Linux 发行版使用 mawk作为包含的 awk 解释器。我尝试使脚本与 awk 无关,但我不确定是否成功。无论如何,从您选择的发行版存储库中安装 gawk 并删除 mawk 可能是个好主意。 mawk 是 awk 的简约实现,缺少很多功能。

关于windows - 定义在 Windows 和 Linux(Windows bash 或 Cygwin)上工作的通用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53753457/

相关文章:

python - 导入错误 : No module named 'tensorflow.contrib.lite.python.tflite_convert'

windows - 如何在 Tomcat 中安装 Solr?

c++ - 计算单色 BMP 的 BITMAPINFOHEADER biCompression 值

bash - 为什么 `ls | cat` != `ls` ?

shell - 批量设置/p 不工作

c++ - Win32 中的 RedrawWindow 和 UpdateWindow 有什么区别?

linux - echo $variable 在 cron 中不起作用

bash - ubuntu/macos 中的单行打印文件校验和

windows - 使用批处理文件打开 Internet 选项,打开连接选项卡,按确定

windows - 批处理文件范围