运行bash脚本,该脚本基于python脚本名

时间:2020-05-01 12:38:07

标签: python bash

我对在python文件中运行bash脚本有疑问。首先,让我解释一下情况。我有多个python文件:对于马里的每个城市,我都可以用python创建天气预报(因此已创建了多个python文件:例如gao.py,bamba.py等)。每个python中的步骤之一文件,用于运行bash脚本,该脚本创建.wav格式的音频文件,并将其放置在/ converted文件夹中。现在,我的问题如下:

我该如何更改bash脚本,即当运行gao.py时,将创建一个文件夹/ converted / gao,而当运行例如bamba.py时,将创建一个转换后的文件夹/ bamba。 ?

这是当前的bash脚本:


#!/bin/bash

if [ ! -d converted/gao ]
then
 mkdir converted/gao;
fi;

但是,当我运行上面的脚本时,每个城市都会将他的文件放置在conversion / gao中,这不是我想要的。我希望有人知道如何解决此问题。

1 个答案:

答案 0 :(得分:1)

使用__ file__访问python中脚本的文件名。然后将其作为参数传递给bash脚本。之后,使用$ 1访问bash脚本中的参数。 如下示例:

Python脚本hello.py

import os

script_filename = __file__
# function to execute the shell script
os.system("./bashfile.sh {}".format(script_filename))

Bash脚本脚本bashfile.sh

#!/bin/bash

script_name=$1
# now use the script_name as you like in your script.
echo $script_name
相关问题