2013-11-02 38 views
1

我在運行.sh文件的gvim工具欄中添加了一個按鈕。 .sh文件運行scons以在/ build子目錄中構建我的C++應用程序並運行它。問題是當應用程序運行時,其當前工作目錄是包含.sh文件(而不是applications/build子目錄)的文件夾! 那麼如何從.sh文件運行內置的C++應用程序可執行文件(linux),以便其工作目錄將是包含可執行文件的文件夾?在其自己的工作目錄中運行可執行文件

+0

在你的shell腳本中,運行它之前,你不能只是'cd'到包含可執行文件的目錄嗎? – pobrelkey

+0

不,我試過:) – Qualphey

+0

「不」 - 你想捍衛爲什麼明顯的解決方案不適合你的用例嗎? – Thanatos

回答

2

只是

cd $(dirname "$0") 
./exec_test 

注意,你需要./exec_test,不exec_test除非目錄實際上已經在PATH

1

下面是類似的例子(我不使用scons

我將我的工具欄圖標添加到:

:amenu ToolBar.mytool :!/home/me/code/misc/foo.sh "%" 

對我來說,當我點擊這個時,vim在與vim相同的工作目錄中運行腳本。

foo.sh包含:

#!/bin/bash 

set -e 

# You should see the name of your file. 
# It might just be "my_file.c" 
echo "$1" 
# This will tell you where your script is current cd'd to. 
pwd 

# `cd` to where the file passed on the command line is: 
cd "$(dirname "$1")" 

# Look for "CMakeLists.txt" 
# You only need this loop if your build file/program might be up a few directories. 
# My stuff tends to be: 
#/- project root 
# CMakeLists.txt 
# src/ 
#  foo.c 
#  bar.c 
while true; do 
    # We found it. 
    if [[ -e "CMakeLists.txt" ]]; then 
     break 
    fi 
    # We didn't find it. If we're at the root, just abort. 
    if [[ "`pwd -P`" = "/" ]]; then 
     echo "Couldn't find CMakeLists.txt." >&2 
     exit 1 
    fi 
    cd .. 
done 

# I do builds in a separate directory. 
cd build && make 

你會替換CMakeLists.txtSConstruct,最後cd build && makescons,或適當scons東西。

相關問題