Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建这个非常小的Shell脚本的Windows等价物?_Shell_Batch File_Terminal_Command Prompt - Fatal编程技术网

如何创建这个非常小的Shell脚本的Windows等价物?

如何创建这个非常小的Shell脚本的Windows等价物?,shell,batch-file,terminal,command-prompt,Shell,Batch File,Terminal,Command Prompt,我试图在.bat文件中实现以下目标。设置路径后,它将执行第一个参数之后的所有参数 # Add your local node_modules bin to the path for this command export PATH="./node_modules/.bin:$PATH" # execute the rest of the command exec "$@" 我已经有了第一部分(我想),但不知道如何做第二部分,也没有成功地在谷歌搜索解决方案 REM Add your local

我试图在
.bat
文件中实现以下目标。设置路径后,它将执行第一个参数之后的所有参数

# Add your local node_modules bin to the path for this command
export PATH="./node_modules/.bin:$PATH"

# execute the rest of the command
exec "$@"
我已经有了第一部分(我想),但不知道如何做第二部分,也没有成功地在谷歌搜索解决方案

REM Add your local node_modules bin to the path for this command
SET PATH=.\node_modules\.bin;%PATH%

第一个命令行可以是:

@set "PATH=%~dp0node_modules\.bin;%PATH%"
@%*
此命令行在批处理文件的子目录
节点模块
的子目录
中的子目录
.bin
的路径开头添加到本地
环境变量,而不是当前目录

%~dp0
始终扩展到以反斜杠结尾的批处理文件目录路径。因此,
%~dp0
应该始终与一个文件夹/文件名连接在一起,而不使用额外的反斜杠,就像这里所做的那样

可以使用
%CD%\
而不是
%~dp0
将当前目录中子目录
节点模块
中的子目录
.bin
的路径添加到本地
路径
环境变量。但请注意,当前目录可能总是与批处理文件目录不同,因此在这里很可能不好

%CD%
扩展为不以反斜杠结尾的目录路径字符串,除非当前目录是驱动器的根目录,在这种情况下,
%CD%
扩展为驱动器号+冒号+反斜杠。因此,使用
%CD%
需要命令行:

@if not "%CD:~-1%" == "\" (set "PATH=%CD%\node_modules\.bin;%PATH%") else set "PATH=%CD%node_modules\.bin;%PATH%"
第二个命令行可以是:

@set "PATH=%~dp0node_modules\.bin;%PATH%"
@%*
这个很短的命令行会解释传递给批处理文件的所有参数,但参数0除外,它是Windows命令处理器在解析后执行的命令行。另见:

命令行开头的
@
会导致Windows command processor
cmd.exe
处理批处理文件在解析后不会输出命令行。命令
set
%*
的命令行不再需要在批处理文件顶部带有
@echo off
的行开始处使用
@

@echo off
set "PATH=%~dp0node_modules\.bin;%PATH%"
%*
打开,运行
call/?
,然后阅读解释如何在批处理文件中引用批处理文件参数的输出帮助

@echo off
set "PATH=%~dp0node_modules\.bin;%PATH%"
%*

另请参阅其中有Windows CMD和Linux Shell命令参考的部分。

我建议您不要将相对路径添加到
%PATH%
;使用
%CD%
而不是
。也许能帮你弄明白。祝你好运