簡體   English   中英

如何在 bash 腳本中調用 Python 腳本?

[英]How to call a Python script in a bash script?

我在/etc/init.d中有一個簡單的腳本,命名為example (我在 OpenWrt 上):

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {        
        mkdir /tmp/test
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

我能夠在啟動時啟動它(我驗證test目錄是在tmp中創建的)。 但是如何調用位於/mnt的 python 程序? 我試過了:

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {        
        python3 /mnt/program.py
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

但我不得不重置設備,因為它無法啟動......為什么? 我究竟做錯了什么?

在運行之前驗證它們是否存在

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {
        if type python3
        then
            if [ -r /mnt/program.py ]
            then
                python3 /mnt/program.py # or use /path/to/python3
            else
                echo "error" >&2
            fi
        else
            echo "no python3" >&2
        fi
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

檢查您的 python 安裝和 python 路徑,以提取要調用的二進制文件,python 或 python3。 您不需要 bash 腳本中的函數 start()、stop()。

#!/bin/bash
python /mnt/program.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM