从外壳文件创建批处理文件



我必须将以下shell文件转换为批处理文件:

#!/bin/bash
#
# Generates and compiles a lexer or parser and runs it (this means
# that the lexer/parser must contain a main method)
#
# Example usage: antlr4-run.sh Lexer.g4
DIR="$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)"
ANTLR_JAR=$DIR/lib/antlr-4.2.2-complete.jar
BASENAME="${1%.*}"
OUTDIR=out
echo "Generating..."
java -jar $ANTLR_JAR $1 -o $OUTDIR || exit 1
echo "Compiling..."
(cd $OUTDIR; javac -cp .:$ANTLR_JAR ${BASENAME}*.java) || exit 1
## run (try parser first, then lexer, then plain)
echo "Input (end with ^D):"
if [[ -a $OUTDIR/${BASENAME}Parser.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}Parser) || exit 1 
elif [[ -a $OUTDIR/${BASENAME}Lexer.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}Lexer) || exit 1 
elif [[ -a $OUTDIR/${BASENAME}.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}) || exit 1 
fi
exit 1

你能帮我举一个下面几行的例子吗?

DIR="$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)"

我知道我必须在批处理文件中定义一个变量,并设置目录 ="..."例如,$是一个commandoline parameter......但我不知道如何在批处理文件中写这一行:/

您应该将变量名称 DIR 更改为其他名称(Win 不区分大小写,dir 是命令)。但是,这是等效的(检查 [MS.文档]:批量参数了解更多详情):

set _DIRNAME=%~dp0

请注意,它不会删除尾随反斜杠 ("\")。

更多:如果尾随的"\"是一个问题,这是可以做的:

set _DIRNAME=%~dp0
set _DIRNAME=%_DIRNAME:~0,-1%

这是一种解决方法(gainarie),但它有效。

@EDIT0

此外,从(最后一个)评论中发布代码。

@EDIT1

由于该方法不健壮(例如,如果扩展名存在于路径中的其他地方,它也将被剥离),因此还发布了一个有效的替代方案,只需要一行:

脚本.bat

@echo off
setlocal enabledelayedexpansion
set _EXT=%~x1
set _NAME_EXT=%~1
set _NAME=!_NAME_EXT:%_EXT%=!
echo Name (env vars): %_NAME%
:: This does the trick, and it's waaay better in every possible way
echo Name (bat args): %~d1%~p1%~n1

输出

e:WorkDevStackOverflowq029928513>script.bat "e:WorkDevStackOverflowq029928513a.bat"
Name (env vars): e:WorkDevStackOverflowq029928513a
Name (bat args): e:WorkDevStackOverflowq029928513a
e:WorkDevStackOverflowq029928513>script.bat "c:dir_bata.bat"
Name (env vars): c:dir_bata
Name (bat args): c:dir_bata
e:WorkDevStackOverflowq029928513>script.bat "c:dir.bata.bat"
Name (env vars): c:dira
Name (bat args): c:dir.bata
e:WorkDevStackOverflowq029928513>script.bat "c:dir.batcha.bat"
Name (env vars): c:dircha
Name (bat args): c:dir.batcha
e:WorkDevStackOverflowq029928513>script.bat "c:dir.batcha.bat1"
Name (env vars): c:dir.batcha
Name (bat args): c:dir.batcha
e:WorkDevStackOverflowq029928513>script.bat "e:WorkDevStackOverflowq029928513 a.bat"
Name (env vars): e:WorkDevStackOverflowq029928513 a
Name (bat args): e:WorkDevStackOverflowq029928513 a

相关内容

  • 没有找到相关文章

最新更新