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

时间:2010-08-04 10:50:31

标签: eclipse batch-file shell

我对脚本非常陌生,但我厌倦了手动编译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 个答案:

答案 0 :(得分:7)

将这些命令插入批处理文件之上:

%~d0
cd %~p0

第一个命令将当前驱动器更改为从%0参数中提取的驱动器;

第二个将当前目录更改为从%0参数中提取的路径。

答案 1 :(得分:0)

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

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

更新:这样您就可以直接添加cygwin脚本,而无需使用bat文件。