在Eclipse中打开的.bat文件在错误的目录中打开。

4
我是一个全新的脚本编写者,但我已经厌倦了手动为Android编译我的本地代码,所以我编写了一个小的.sh脚本和一个.bat文件,在cygwin中运行此脚本。这两个文件都放置在项目的根目录下,.sh文件中设置了NDK目录,然后通过运行.bat文件来编译我的本地代码。
问题在于我想要动态执行它,所以我使用%CD%获取当前目录(应该是项目文件夹),并在该目录中启动.sh文件。
以下是两个文件:
.bat 文件:
@echo off

::Used to surpess a warning about dos directory format.
set CYGWIN=nodosfilewarning

C:\cygwin\bin\bash --login -i %CD%\./compile.sh

pause

.sh:

#!/bin/bash

#Run this script through compileNative.bat This will compile the native code of this Project
#IF this file is changed under windows use "tr -d '\r' < edited.sh > final.sh " to remove the bad line endings.
#Keep both this and the .bat file in the project root.
# Set this to the base NDKDir
NDKDIR=C:/Android/NDK/


#Get the base dir so we can change the directory to the base dir.
BASEDIR=`dirname $0`

echo 
echo Compiling Native code. Refresh Workspace after this is done!
echo 
#Change to the directory of the project, change this is if the project movies.
cd $BASEDIR

#Run the ndk build file. Change this to the correct location.
$NDKDIR./ndk-build

当我在Windows中从文件夹打开.bat文件时,它可以正常运行。但是当我从Eclipse中运行它时,似乎%CD%会给出"C:/Eclipse"。更让我烦恼的是,它早上一直可以运行,但突然间就出现了这个问题。
所以我的问题是,我是否使用了错误的%CD%方式,或者为什么会发生这种情况。显然这不是一个大问题。但这是一个我似乎无法解决的小烦恼。
希望能得到一些帮助。
2个回答

9

在批处理文件的顶部插入以下命令:

%~d0
cd %~p0

第一条命令将当前驱动器更改为从%0参数中提取的驱动器; 第二条命令将当前目录更改为从%0参数中提取的路径。

谢谢你的回答。这对我解决这个eclipse bug非常有帮助:https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429 - SpaceTrucker
1
问题的绝佳解决方案。 - avanderw

0

我建议为此创建一个外部工具配置(绿色运行图标旁边的红色框或锁定图标)。外部工具可以启动脚本,带有预定义的环境变量或工作目录。

要动态设置这些目录(例如基于当前选择的项目),您可以使用平台变量,例如${container_loc}或${resource_loc} - 使用“变量...”按钮获取可用变量列表。

更新:通过这种方式,您可能可以直接添加cygwin脚本,而无需使用bat文件。


今天我终于有机会测试了你的建议。它非常好用,现在甚至不需要打开新窗口,但我可以在Eclipse控制台中看到它。 我保留了bat文件,因为我不想花太多时间在这上面。但我想你是对的,没有bat文件也是可以实现的。非常感谢! - Pandoro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接