Rawstring
Rawstring

Reputation: 67

How to execute a script from within another script?

I'm having trouble executing some files under a BASH script.

What I want is, when running the SCRIPT.SH, to check if the directory from which it's running is the right one. Which in this case it's /ROOT/OPENSOURCE . If it's not, then it asks if the user wants to move the directory into the correct place. Doing this by another script /OPENSOURCE/MODULES/MOVE.SH.

I have this variable to get the script launching dir:

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

Now since the script will not be installed on the right directory, I need to run MOVE.SH which is in the MODULES directory inside OPENSOURCE. I can't get this done.

Code:

# 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

How can I do it ?

Upvotes: 0

Views: 128

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21354

I'm not sure I 100% understand the question, but I think you might just be looking for the mysterious "." command, which will run another script.

Example:

test.sh:

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

test2.sh:

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

Run it:

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

Upvotes: 1

Related Questions