2014-01-08 137 views
0

我在BASH腳本下執行某些文件時遇到問題。如何從另一個腳本內執行腳本?

我想要的是,在運行SCRIPT.SH時,檢查它運行的目錄是否是正確的。在這種情況下,它是/ ROOT/OPENSOURCE。如果不是,則詢問用戶是否想將目錄移動到正確的位置。通過另一個腳本/OPENSOURCE/MODULES/MOVE.SH執行此操作。

我有這個變量拿到劇本推出DIR:

spath="$(cd "$(dirname $0)" && pwd)" 

現在,因爲該腳本將不正確的目錄進行安裝,我需要運行MOVE.SH這是在內部OPENSOURCE模塊目錄。我無法完成這件事。

代碼:

# check if script is on correct directory 
if [ "$rpath" = "$spath" ]; then 
    Colors; 
    echo ${RedF}[x] [WAIT]${YellowF} Checking directory: ${Reset}; 
    sleep 1 
    echo ${BlueF}[*] [Directory]:${GreenF} OK ${Reset}; 
else 
    Colors; 
    echo ${RedF}[x] [WAIT]${YellowF} Checking directory: ${Reset}; 
    sleep 1 
    echo ${BlueF}[*] [Directory]:${RedF} FAILED: This may cause a script malfunction ${Reset}; 
    read -p "[!] Move script directory to right directory {y|n}:" pass 
    if test "$pass" = "y" 
    then 
    echo ${BlueF}[*] [Directory]: ${GreenF}Ok. Moving script to new directory${Reset}; 
    sleep 1 
    ---- COMMAND WILL BE HERE ---- 
    else 
    echo ${BlueF}[*] [Directory]: ${YellowF}Ok not moving ${Reset}; 
    fi 
fi 

我該怎麼辦呢?

回答

0

我不知道我100%明白這個問題,但我想你可能只是在尋找神祕的「。」。命令,它將運行另一個腳本。

例子:

test.sh:

#!/bin/bash 
echo running other script $1 
. $1 
echo done 

test2.sh:

#!/bin/bash 
echo I am the other script 

運行:

> ./test.sh test2.sh 
running other script test2.sh 
I am the other script 
done 
+0

不是我一直在尋找,但我已經修好了;) –

相關問題