我在 BASH 脚本下执行某些文件时遇到问题。
我想要的是,在运行脚本时。SH,以检查运行它的目录是否正确。在这种情况下,它是/ROOT/OPENSOURCE .如果不是,则询问用户是否要将目录移动到正确的位置。通过另一个脚本/OPENSOURCE/MODULES/MOVE.SH执行此操作。
我有这个变量来获取脚本启动目录:
spath="$( cd "$( dirname $0 )" && pwd )"
现在,由于脚本不会安装在正确的目录中,我需要运行 MOVE。SH,位于开源内部的模块目录中。我做不到。
法典:
# 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
我该怎么做?
我不确定我是否 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