简体   繁体   English

从另一个bash脚本运行的bash脚本运行python

[英]Run python from bash script that is run by another bash script

I currently have a folder structure that will contain a few python scripts which need to be fired from a certain folder but I would like to write a global script that runs each python script via a seperate script in each folder. 我目前有一个文件夹结构,其中将包含一些需要从某个文件夹中触发的python脚本,但是我想编写一个全局脚本,该脚本通过每个文件夹中的单独脚本来运行每个python脚本。

-Obtainer -获取者
--Persona -女主角
---Arthur -亚瑟
----start.sh ----start.sh
--Initialise.sh --Initialise.sh

-Persona -Persona
--Arthur -亚瑟
---lib --- lib
----pybot ---- pybot
-----pybot.py -----pybot.py

When I run initialise I am aiming to make initialise run "start.sh" Arthur is the bot and there will be more folders with different names and initialise with find and fire each start.sh. 当我运行初始化程序时,我的目标是使初始化程序运行“ start.sh”。Arthur是自动程序,并且会有更多名称不同的文件夹,并使用find并启动每个start.sh进行初始化。

In initialise.sh I have: 在initialize.sh中,我有:

#!/bin/bash
. ./Persona/Arthur/start.sh

In start.sh I have: 在start.sh中,我有:

#!/bin/bash
python ../../../Persona/Arthur/lib/pybot/pybot.py

I get this error: 我收到此错误:

python: can't open file '../../../Persona/Arthur/lib/pybot/pybot.py': [Errno 2] No such file or directory

However if I run the start.sh itself from its directory it runs fine. 但是,如果我从其目录运行start.sh本身,它将运行良好。 This is because I assume it's running it from the proper shell and consequently directory. 这是因为我假设它是从正确的shell运行的,因此也从目录运行它。 Is there a way to make the main script run the start.sh in it's own shell like it is being run by itself? 有没有办法让主脚本在自己的外壳中像自己在运行一样运行start.sh? The reason why is because the pybot.py saves a bunch of files to where the start script is and because there will be more than one bot I need them to save in each seperate folder. 之所以这样,是因为pybot.py将一堆文件保存到启动脚本所在的位置,并且因为有多个机器人,所以我需要将它们保存在每个单独的文件夹中。

In the first place, do not source when you mean calling it, 首先,当您要调用它时不要来源

#!/bin/bash
. ./Persona/Arthur/start.sh

Don't do this. 不要这样

Your script has a number of issue. 您的脚本有很多问题。 It won't work because of your current working directory is uncertain. 由于您当前的工作目录不确定,因此无法使用。 You'd better have your script derive the path to relieve yourself from the hustle of abs paths or relative paths. 您最好让您的脚本派生路径,以使自己摆脱Abs路径或相对路径的困扰。

The general code could be 通用代码可以是

script_dir=`dirname "${BASH_SOURCE[0]}"`

then you can use this to derive the path of your target file, 然后您可以使用它来导出目标文件的路径,

#!/bin/bash
script_dir=`dirname "${BASH_SOURCE[0]}"`
"$script_dir/Persona/Arthur/start.sh"

Your python invocation becomes: 您的python调用变为:

#!/bin/bash
script_dir=`dirname "${BASH_SOURCE[0]}"`
python "$script_dir/../../../Persona/Arthur/lib/pybot/pybot.py"

This should work out properly. 这应该可以正常工作。

Regarding BASH_SOURCE , check out https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html 关于BASH_SOURCE ,请查看https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html


If you want the directory of start.sh to be cwd, you should call cd : 如果要将start.sh的目录设置为cwd,则应调用cd

#!/bin/bash
script_dir=`dirname "${BASH_SOURCE[0]}"`
cd "$script_dir"
python "$script_dir/../../../Persona/Arthur/lib/pybot/pybot.py"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM