如何从另一个脚本中执行脚本?

时间:2014-01-08 05:42:36

标签: bash

我在BASH脚本下执行某些文件时遇到了问题。

我想要的是,在运行SCRIPT.SH时,检查运行它的目录是否正确。在这种情况下,它是/ ROOT / OPENSOURCE。如果不是,则询问用户是否要将目录移动到正确的位置。通过另一个脚本/OPENSOURCE/MODULES/MOVE.SH。

执行此操作

我有这个变量来启动脚本启动目录:

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

既然脚本不会安装在正确的目录中,我需要运行MOVE.SH,它位于OPENSOURCE内的MODULES目录中。我不能完成这件事。

代码:

# 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

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我不确定我是否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